typestar

Real kinds in Fortran

One third at 32 and 64 bits: iso_fortran_env names the precisions portably.

program real_kinds
    ! iso_fortran_env names the kinds portably: real32, real64.
    use iso_fortran_env, only: real32, real64
    implicit none
    real(real32) :: single = 1.0_real32 / 3.0_real32
    real(real64) :: double = 1.0_real64 / 3.0_real64

    print *, "single: ", single
    print *, "double: ", double
    print *, "epsilon at 32 bits: ", epsilon(single)
    print *, "epsilon at 64 bits: ", epsilon(double)
    print *, "largest double:     ", huge(double)
end program real_kinds

How it works

  1. use iso_fortran_env, only: real32, real64 imports the kind constants.
  2. 1.0_real64 stamps the kind on the literal, not just the variable.
  3. epsilon and huge report each kind's precision and ceiling.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
456
Tokens
75
Three-star pace
60 tpm

At the three-star pace of 60 tokens a minute, this run takes about 75 seconds.

Type this snippet

Step 1 of 3 in Kinds & modules, step 22 of 27 in Language basics.

← Previous Next →