I also think, for free-form data files (in which spaces are used to delimit numbers), C or python-like formatting is nicer because it automatically adjusts the width when necessary. (I guess the F and G descriptors in Fortran was designed much ago for fixed-form data files, like Protein Data Bank (PDB) format etc.) I think a flexible format (like cf10.4
) would be useful if available (in addition to f0.4
, which is also very useful for free format).
program main
use iso_fortran_env, only: dp => real64
implicit none
real(dp) :: x
integer :: n
x = acos(-1.0_dp)
do n = -5, 9
print "('n = ', i3, ' x * 10**n = ', f10.4)", n, x * 10.0_dp**n
!! print "('n = ', i3, ' x * 10**n = ', g10.4)", n, x * 10.0_dp**n
!! print "('n = ', i3, ' x * 10**n = ', e10.4)", n, x * 10.0_dp**n
!! print "('n = ', i3, ' x * 10**n = ', es10.4)", n, x * 10.0_dp**n
enddo
end
Output:
format = f10.4
n = -5 x * 10**n = 0.0000
n = -4 x * 10**n = 0.0003
n = -3 x * 10**n = 0.0031
n = -2 x * 10**n = 0.0314
n = -1 x * 10**n = 0.3142
n = 0 x * 10**n = 3.1416
n = 1 x * 10**n = 31.4159
n = 2 x * 10**n = 314.1593
n = 3 x * 10**n = 3141.5927
n = 4 x * 10**n = 31415.9265
n = 5 x * 10**n = **********
n = 6 x * 10**n = **********
n = 7 x * 10**n = **********
n = 8 x * 10**n = **********
n = 9 x * 10**n = **********
format = g10.4
n = -5 x * 10**n = 0.3142E-04
n = -4 x * 10**n = 0.3142E-03
n = -3 x * 10**n = 0.3142E-02
n = -2 x * 10**n = 0.3142E-01
n = -1 x * 10**n = 0.3142
n = 0 x * 10**n = 3.142
n = 1 x * 10**n = 31.42
n = 2 x * 10**n = 314.2
n = 3 x * 10**n = 3142.
n = 4 x * 10**n = 0.3142E+05
n = 5 x * 10**n = 0.3142E+06
n = 6 x * 10**n = 0.3142E+07
n = 7 x * 10**n = 0.3142E+08
n = 8 x * 10**n = 0.3142E+09
n = 9 x * 10**n = 0.3142E+10
In the case of Python:
import numpy as np
x = np.pi
for n in range(-5, 10):
print( f"{n = :3d} {x * 10**n = :10.4f}" )
Output:
n = -5 x * 10**n = 0.0000
n = -4 x * 10**n = 0.0003
n = -3 x * 10**n = 0.0031
n = -2 x * 10**n = 0.0314
n = -1 x * 10**n = 0.3142
n = 0 x * 10**n = 3.1416
n = 1 x * 10**n = 31.4159
n = 2 x * 10**n = 314.1593
n = 3 x * 10**n = 3141.5927
n = 4 x * 10**n = 31415.9265
n = 5 x * 10**n = 314159.2654
n = 6 x * 10**n = 3141592.6536
n = 7 x * 10**n = 31415926.5359
n = 8 x * 10**n = 314159265.3590
n = 9 x * 10**n = 3141592653.5898