Welcome to AE Resources
Converted document
Consider the following code calculating some simple math:
PROGRAM basics
          IMPLICIT NONE
          REAL :: r, r2, r3
          INTEGER :: i, i2
          r = (3./4.)*(4./3.)+3./4.
          r2 = (3/4)*(4/3)
          r3 = (3/4)*(4/3) + 3./4.
          i = (3/4)*(4/3)
          i2 = (3./4.)*(4./3.)
          PRINT *, ’r=’,r,’r2=’,r2,’r3=’,r3,’i=’,i,’i2=’,i2
END PROGRAM
The output is:
r=   1.7500000 r2=   0.0000000 r3=   0.7500000 i= 0 i2= 1
From the mathematics, one expects r and r3 to provide the same answers of 1.75, however, they are not equal. Similarly, r2, i and i2 mathematically result in the whole number, 1. It is clear from the results that the computer does not provide these expected answers. This is because of the difference between integer and real declarations. r2 and r3 include numbers on the right hand side of the equation without a decimal point. Thus, these are interpreted as integer numbers and operations. So, 3/4 is truncated to 0 rather than 0.75.
i and i2 are declared as integers. Notice that if the numbers in the computation are defined as real (they have a decimal point), the answer expected is achieved, while if the values are treated as integers, then the 3/4 term yields 0. This feature is important as these computations can be used to generate integer counters from real numbers, which can save operation counts.
Notice that this behavior is different from some toolboxes such as MATLAB, which does not differentiate between integers and real variables.
DECLARING CONSTANTS
Constants, such as π (pi) or 1/2, can be defined once and then used consistently in the program. These constants can be declared in one of two ways. The constant can be defined in a line of code:
<variablename>=<constantvalue>
such as
REAL :: pi, half
pi= 4. * ATAN(1.)
half = 1./2.
Another way to initialize it is through a parameter statement:
<data_type>, PARAMETER :: variable_name=value, variable2=value
A variable defined as a PARAMETER is not allowed to be changed during the program execution. A complete declaration statement followed by a simple code is below.
PROGRAM pi
          IMPLICIT NONE
          REAL, PARAMETER :: pi = (4. * ATAN(1.))
          REAL :: angle, fourth
          fourth = 1./4.
          angle = SIN(pi * fourth)
          PRINT *, ’Sin of 45 degrees is ’, angle
END PROGRAM    
which returns
Sin of 45 degrees is    0.7071068
It is not a good idea to initialize a parameter using:
DATA_TYPE :: variable_name=value
such as
real :: temp = 23.
This is not good programming practice as it can lead to overwritten values.
PRECISION
For some computations that require many decimal places to maintain accuracy, it may be necessary to expand the content using double and quadruple (double double) precision. This is primarily for floating point operations, but it can impact other data types as well. The quadruple precision provides almost twice double precision, but it does compensate by reducing the exponent range.
The size of the variable influences the memory used to store each variable, so for large arrays the use of the precision can influence a code’s ability to run on small processors. It requires 4, 8, and 16 bytes to store single, double and quadruple floating point and complex numbers, respectively. Integer types require 1, 2, 4, and 8 for 8-, 16-, 32- and 64-bit integers, where a 32-bit integer is the default.
Compilers may be different in their precision definition, so it is a good practice to check the compiler documentation.
Precision can be coded into the source code, as well as modified in some instances during the compilation. Compilation with the “-r8” or “-double” option for example will double the size of the default REAL and COMPLEX data types. If variables have been defined with higher precision and if quadruple-precision floating point operations are available on the processor, this option will invoke quadruple precision. The effect of these compiler options on explicitly defined variables (i.e., using KIND) should be verified.
← Previous Page
← Previous Page