The following program will print floating-point numbers to standard output without losing any precision.
program main
use ISO_Fortran_env, only: &
stdout => OUTPUT_UNIT, &
compiler_version, &
compiler_options
! Explicit typing only
implicit none
! Variable declarations
integer, parameter :: SP = selected_real_kind(p=6, r=37)
integer, parameter :: DP = selected_real_kind(p=15, r=307)
real (SP) :: single
real (DP) :: double
single = 1.000001234567890_SP
double = 1.000001234567890_DP
write( stdout, '(e13.6e2)') single
write( stdout, '(e23.15e3)') double
write( stdout, '(/4a/)') &
' This file was compiled using ', compiler_version(), &
' using the options ', compiler_options()
end program main
Take note of how the kind parameters SP
and DP
are employed to control precision in a portable manner. This program yields:
0.100000E+01
0.100000123456789E+001
This file was compiled using GCC version 6.1.1 20160802 using the options -mtune=generic -march=x86-64 -std=f2008ts