On Monday 03 March 2003 09:03, James Giles wrote:
> "Aleksandar Donev" <[log in to unmask]> wrote:
> ...
>
> > I am going to ask for IO advice/wisdom again. It has been said that to
> > avoid loss of precision in formatted IO, one should use explicit
> > formats. What is a good way to make formats for writing real numbers of
> > kind r_wp (may be single, may be double, etc.) to their *full*
> > precision, so that later reading them back into a Fortran program will
> > not loose much if any of the high-order precision bits. Of course, I
> > should use nonformatted IO for full precision, but since other
> > non-Fortran programs read my files as well, I cannot. I am assuming
> > exponential format is the way to go, but am not sure how to format it
> > exactly.
>
> Let b represent the number of bits in the binary floating-point form.
> To convert data to decimal in order that converting back again
> yields the same number you will have to generate d decimal digits
> where d is defined by the expression:
>
> 10^(d-1) > 2^b
>
> That is, d > log10(2^b) + 1.
>
> For IEEE single precision, b=24 and d > 8.22, hence you must generate
> 9 decimal digits. For IEEE double precision, b=53 and d > 16.95,
> so you must generate 17 decimal digits. The format specifications
> I use most often for this are G16.9 and G24.17 respectively. With this
> the exponent on double precision values are sometimes 3 digits long and
> the exponent field gets written without the letter E. To avoid this, you
> could decide to use G25.17E3 to make room for the extra exponent digit.
IEEE754 and successor standards mention the formulae James gave, and also
specify, under appropriate restrictions, that conversions between binary and
decimal should be reversible (no information lost).
If the formatted conversions in your run-time library meet the IEEE specs,
and you use the appropriate E or G formats, you should ordinarily not lose
any precision, at least not for numbers in the range 0.1 < x <= 1/EPSILON(x).
I have seen run-time libraries which drop the low order bit in double
precision, but g77, and others, appear to fulfill the promise of the IEEE
standard.
--
Tim Prince
|