[Developers] Fortran 77/90 formatted output "gotcha"
Jonathan Thornburg
jthorn at aei.mpg.de
Mon May 9 12:17:32 CDT 2005
Hi,
I've just run across a Fortran 77/90 "gotcha" which I'd like to warn
other Fortran programmers about. It's best expressed by an example:
program test_gformat
implicit none
double precision :: x
x = 1.234d-300
write (6, '(g25.10)') x
write (6, '(g25.10e3)') x
stop
end
When compiled with either g77 3.3.3 or ifort 8.0, this produces the
output
0.1234000000-299
0.1234000000E-299
Notice the first line -- that number will be read by most other
software as 0.1234 followed by some trailing junk, i.e. the exponent
has essentially been lost!
Since IEEE double precision has an exponent range of around 1e-308 to
1e308, the moral of the story is to always use the "e3" suffix on output
formats. I didn't know this before this past weekend...
ciao,
--
-- Jonathan Thornburg <jthorn at aei.mpg.de>
Max-Planck-Institut fuer Gravitationsphysik (Albert-Einstein-Institut),
Golm, Germany, "Old Europe" http://www.aei.mpg.de/~jthorn/home.html
"Washing one's hands of the conflict between the powerful and the
powerless means to side with the powerful, not to be neutral."
-- quote by Freire / poster by Oxfam
More information about the Developers
mailing list