Fortran Data Types Precision of floating point numbers


Example

Floating point numbers of type real cannot have any real value. They can represent real numbers up to certain amount of decimal digits.

FORTRAN 77 guaranteed two floating point types and more recent standards guarantee at least two real types. Real variables may be declared as

real x
double precision y

x here is a real of default kind and y is a real of kind with greater decimal precision than x. In Fortran 2008, the decimal precision of y is at least 10 and its decimal exponent range at least 37.

 real, parameter             :: single = 1.12345678901234567890
 double precision, parameter :: double = 1.12345678901234567890d0
 
 print *, single
 print *, double

prints

   1.12345684    
   1.1234567890123457

in common compilers using default configuration.

Notice the d0 in the double precision constant. A real literal containing d instead of e for denoting the exponent is used to indicate double precision.

! Default single precision constant
1.23e45
! Double precision constant
1.23d45

Fortran 90 introduced parameterized real types using kinds. The kind of a real type is an integer named constant or literal constant:

real(kind=real_kind) :: x

or just

real(real_kind) :: x

This statement declares x to be of type real with a certain precision depending on the value of real_kind.

Floating point literals can be declared with a specific kind using a suffix

1.23456e78_real_kind

The exact value of real_kind is not standardized and differs from compiler to compiler. To inquire the kind of any real variable or constant, the function kind() can be used:

print *, kind(1.0), kind(1.d0)

will typically print

4 8

or

1 2

depending on the compiler.

Kind numbers can be set in several ways:

  1. Single (default) and double precision:

    integer, parameter :: single_kind = kind(1.)
    integer, parameter :: double_kind = kind(1.d0)
    
  2. Using the intrinsic function selected_real_kind([p, r]) to specify required decimal precision. The returned kind has precision of at least p digits and allows exponent of at least r.

    integer, parameter :: single_kind = selected_real_kind( p=6, r=37 )
    integer, parameter :: double_kind = selected_real_kind( p=15, r=200 )
    
  3. Starting with Fortran 2003, pre-defined constants are available through the intrinsic module ISO_C_Binding to ensure that real kinds are inter-operable with the types float, double or long_double of the accompanying C compiler:

    use ISO_C_Binding
    
    integer, parameter :: single_kind = c_float
    integer, parameter :: double_kind = c_double
    integer, parameter :: long_kind = c_long_double
    
  4. Starting with Fortran 2008, pre-defined constants are available through the intrinsic module ISO_Fortran_env. These constants provide real kinds with certain storage size in bits

    use ISO_Fortran_env
    
    integer, parameter :: single_kind = real32
    integer, parameter :: double_kind = real64
    integer, parameter :: quadruple_kind = real128
    

If certain kind is not available in the compiler, the value returned by selected_real_kind() or the value of the integer constant is -1.