Discussion:
Very small numbers & formatting
(too old to reply)
Chris Theis
2006-10-11 09:27:42 UTC
Permalink
Hello all,

I'm not a FORTRAN expert and I'm currently facing some behavior which
astonished me, so I would be grateful if you could shed some light on this.
We're developing a data analysis software in a language, which is OT here,
but we get data from software written in FORTRAN 77 (using GNU FORTRAN 77).
Just recently we found very numbers in the data which were written as
1.0614-238, which really astonished me. Our software balked because this was
seen as an expression but the original data content of the FORTRAN software
was 1.0614E-238. I wondered, but a quick check showed that FORTRAN is
actually able to read and interpret 1.0614-238 as 1.0614E-238 using the '*'
comment. However, with the non-specified '*' formatting I found that FORTRAN
is actually able to write out the number using proper scientific notation.
Consequently, I figured that the reason must be the format specifier in the
FORTRAN software which produces the data. Could anybody confirm this?

Finally, I wondered what would be the best solution (e.g. which formatting
specifier) to avoid such problems because in my opinion such an output
formatting is all but portable, especially if other programs or even other
FORTRAN software with specific formatting requirements is involved.

Thanks a lot
Chris
Steve Lionel
2006-10-11 10:45:08 UTC
Permalink
Post by Chris Theis
Just recently we found very numbers in the data which were written as
1.0614-238, which really astonished me.
In Fortran, the D, E and G edit descriptors each have a fixed width
specified for the expoinent part of a real number - the default is 2
(for E and D), but you can override this, for example, E16.7E3, where
the E3 says to reserve three digits.

The standard also says that if the actual exponent exeeds the allotted
number of digits by one, then the letter E or D is removed so that the
full exponent can be shown. Fortran formatted input can accept this,
but as you found, other software may not.

Yes, this does astonish people who have not encountered it before or
who are not familiar with one of the odder details of the Fortran
language.

Steve
Richard E Maine
2006-10-11 15:58:40 UTC
Permalink
Post by Steve Lionel
The standard also says that if the actual exponent exeeds the allotted
number of digits by one, then the letter E or D is removed so that the
full exponent can be shown.
The case in standard is slightly more restrictive than this. The removal
of the E or D applies only if you have *NOT* explicitly specified the
exponent width. If you explicitly specify the exponent width, then the
specified width is taken literally, with no deletion of the E allowed.
Note that a compiler that did such deletion for in the case of an
explicitly specified width would be in violation of the standard. That
would not constitute an extension. The standard specifies what the
compiler shall do for that case - fill the field with asterisks.

A compiler that failed to fill the field with asterisks would be
non-conforming and could concievably break standard-conforming code. For
example, it is plausible that standard-conforming Fortran code could
notice and special-case the field of asterisks.

Not that this comes up a lot. When people specify an explicit exponent
width, it is usually a width of 3, which can't get exceeded by the most
common floating point ranges. But quad precisions do sometimes require
more than 3 digits of exponent. And it is allowed to explicitly specify
a width of 2 (or even 1), although I don't recall seeing that done. It
is at least concievable that someone might explicitly specify 2 in order
to avoid the form without the letter.
--
Richard Maine | Good judgment comes from experience;
email: my first.last at org.domain| experience comes from bad judgment.
org: nasa, domain: gov | -- Mark Twain
Ron Shepard
2006-10-11 14:41:35 UTC
Permalink
Post by Chris Theis
Just recently we found very numbers in the data which were written as
1.0614-238, which really astonished me.
This is a standard form for formatted floating point data. Data was
read and written this way before many of the other languages in
popular use today were even invented, and it is ultimately the
responsibility of these newer languages to remain compatible with
the standard conventions. However, if you cannot fix the problem
the right way, then you have to either modify the fortran program
that writes the data, or you have to process the data file (e.g.
with another fortran program) to make it compatible with the other
language conventions. Others have pointed out the optional "e"
exponent width format descriptor which accomplishes this.

$.02 -Ron Shepard
Chris Theis
2006-10-11 15:17:37 UTC
Permalink
Post by Ron Shepard
Post by Chris Theis
Just recently we found very numbers in the data which were written as
1.0614-238, which really astonished me.
This is a standard form for formatted floating point data. Data was
read and written this way before many of the other languages in
popular use today were even invented, and it is ultimately the
responsibility of these newer languages to remain compatible with
the standard conventions. However, if you cannot fix the problem
the right way, then you have to either modify the fortran program
that writes the data, or you have to process the data file (e.g.
with another fortran program) to make it compatible with the other
language conventions. Others have pointed out the optional "e"
exponent width format descriptor which accomplishes this.
Ron, and Steve, thanks a lot for the insight.

Cheers
Chris
Loading...