Computational Physics - Department of Physics

(Axel Boer) #1

2.2 Representation of Integer Numbers 15


# General makefile for F90 - choose PROG = name of given program

# Here we define compiler options, libraries and the target
F90= f90
PROG= myprogram

# Here we make the executable file
${PROG} : ${PROG}.o
${F90} ${PROG}.o -o ${PROG}

# whereas here we create the object file

${PROG}.o : ${PROG}.f90
${F90} -c ${PROG}.f

Finally, for the sake of completeness, we list the corresponding Python code

http://folk.uio.no/mhjensen/compphys/programs/chapter02/python/program1.py
#!/usr/bin/env python


importsys, math


Read in a string a convert it to a float


r = float(sys.argv[1])
s = math.sin(r)
print"Hello, World! sin(%g)=%12.6e"% (r,s)


where we have used a formatted printout with scientific notation. In Python we do not need
to declare variables. Mathematical functions like thesinfunction are imported from themath
module. For further references to Python and its syntax, we recommend the text of Hans
Petter Langtangen [22]. The corresponding codes in Python are available at the webpage of
the course. All programs are listed as a directory tree beginning with programs/chapterxx.
Each chapter has in turn three directories, one for C++, one for Fortran and finally one for
Python codes. The Fortran codes in this chapter can be found in the directory programs/chap-
ter02/Fortran.


2.2 Representation of Integer Numbers


In Fortran a keyword for declaration of an integer isINTEGER (KIND=n), n = 2 reserves 2
bytes (16 bits) of memory to store the integer variable wheras n = 4 reserves 4 bytes (32 bits).
In Fortran, although it may be compiler dependent, just declaring a variable as INTEGER,
reserves 4 bytes in memory as default.
In C++ keywords areshort int, int, long int, long long int. The byte-length is
compiler dependent within some limits. The GNU C++-compilers (called by gcc or g++)
assign 4 bytes (32 bits) to variables declared byint andlong int. Typical byte-lengths
are 2, 4, 4 and 8 bytes, for the types given above. To see how many bytes are reserved for a
specific variable, C++ has a library function called sizeof(type)which returns the number
of bytes fortype.
An example of a program declaration is


Fortran: INTEGER (KIND=2) :: age_of_participant

Free download pdf