Numerics
Kinds
Selecting Precision
iso_fortran_env exposes named kind constants. real32 is 32-bit (kind 4) and real64 is 64-bit (kind 8) on common platforms.
Program
Play the program to print the kind numbers behind real32 and real64.
kinds.f90
Replay: real traced execution (multi-file project)
program kinds_demo
use iso_fortran_env, only: real32, real64
implicit none
integer :: k32, k64
k32 = real32
k64 = real64
print '(I0, A, I0)', k32, " ", k64
end program kinds_demo
k32 ← 4
4integer :: k32, k645k32 = real326k64 = real64values this step4k32k64 ← 8
5k32 = real326k64 = real647print '(I0, A, I0)', k32, " ", k64values this step8k64print '(I0, A, I0)', k32, " ", k64
6 k64 = real647 print '(I0, A, I0)', k32, " ", k648end program kinds_demooutput4 8values this step4k328k64
Follow the Kinds
- The program imports
real32andreal64. k32 = real32stores4.k64 = real64stores8.- The print statement writes both numbers with one space between them.
- The output is
4 8. | name | stored value | | --- | --- | |k32| 4 | |k64| 8 | | stdout | 4 8 |
iso_fortran_env
Standard module exposing portable kind constants.
real32 / real64
Named constants for 32-bit and 64-bit real kinds.
only:
`only:` imports just the names you need.
Exercise: kinds.f90
Reproduce the output 4 8, then trace how real32 fills k32 with 4 and real64 fills k64 with 8.