Reputation: 1725
Is there a way to print extremely small double precision numbers with fixed format in Fortran? Somehow, the "E" gets truncated.
Changing the 'ES15' to 'ES18' in the code below doesn't help:
program print_double
implicit none
double precision :: x
x = 2.71818D-200
write(*,*) "fmt=* : x = ", x
write(*,fmt='(A,ES15.3)') " fmt=ES15.3: x = ", x
write(*,fmt='(A,E15.3)') " fmt= E15.3: x = ", x
end program print_double
Here's the output:
$ gfortran print_double.f90 -o print_double && ./print_double
fmt=* : x = 2.7181800000000000E-200
fmt=ES15.3: x = 2.718-200
fmt= E15.3: x = 0.272-199
$ ifort print_double.f90 -o print_double && ./print_double
fmt=* : x = 2.718180000000000E-200
fmt=ES15.3: x = 2.718-200
fmt= E15.3: x = 0.272-199
Note: this is for a code in which I check that the solution obtained through two different mathematical approaches agree. Most of the the time, the differences are small (i.e. well below machine precision), but this may not always be the case.
Upvotes: 2
Views: 700
Reputation: 21431
The default width for the magnitude of the exponent field, in the absence of a specification otherwise, is two. The E
or D
exponent letter is dropped if the exponent magnitude needs one character more than provided by the exponent field. (Note that this form, with the missing exponent letter, is perfectly acceptable for Fortran formatted input.)
If you want three character exponents, then specify three character exponents.
write(*,fmt='(A,ES15.3E3)') " fmt=ES15.3E3: x = ", x
write(*,fmt='(A,E15.3E3)') " fmt= E15.3E3: x = ", x
Upvotes: 2
Reputation: 721
You can specify the number of digits in the exponent using the Ew.dEe
descriptor. It also works for Scientific and Engineering formats.
write(*,fmt='(A,ES15.3E3)') " fmt=ES15.3E3: x = ", x
gives
fmt=ES15.3E3: x = 2.718E-200
Upvotes: 2