Re: a Fortran precision question



www wrote:

I noticed in Fortran program, if a value is computed, the total digit
number (the sum of digit number before the decimal point and the digit
number after the decimal point) is 7. So if the number is larger, then
the digit number after the decimal point is less. For example:

0.123456789 in Fortran will be 0.1234568 //7 digits after decimal point
1.23456789 in Fortran will be 1.234568 //6 digits after decimal point
123.456789 in Fortran will be 123.4568 //4 digits after decimal point
123456.789 in Fortran will be 123456.8 //only 1 digit after decimal point

Above is what I saw. I am not sure my impression is correct or not. The
Fortran program is compiled in Fortran-90 compiler. If it is with
Fortran-77 compiler, then it is different story.

Thank you for your help.

Where to start......

Firstly, remember that (almost) all computers work in binary, not
decimals (I am told that the Fortran standards don't actually assume
that the computer is binary-based, but that's a side point).

_Most_ modern computers model the real numbers as floating point
numbers. These are an approximation to the (infinite) set of real
numbers. _Most_ computers (again, there are exceptions, but I doubt
these are relevant to you right now) implement IEEE floating point
arithmetic. This causes the numbers to be stored in the form
a * 2^b
where a and b are integers. This is the binary equivalent of
'scientific' notation
c * 10^d
with c and d integers. In IEEE arithmetic, a set number of bits are
devoted to the storage of a and b. This limits the number of significant
figures in the result - note that in the results you quote above, all
the answers (and indeed inputs) have the same number of _significant
figures_ (put another way, they all have the same value for c, and only
vary in the value of d).

IEEE defines two types of floating point numbers, single precision and
double precision. As the name implies, double precision numbers devote
approximately twice as much storage space to 'a' as single precision,
and hence calculations using them have twice as many significant figures
(of course, significant figures might not be correct figures, but that's
another issue ;-) ).

It so happens that, when you convert back to decimals, single precision
floating point numbers have approximately six significant _decimal_
digits, while double precision numbers will have 12 significant
_decimal_ digits. You don't show code, but I would suspect that the
variables you are outputting are declared as REAL. On most compilers,
that's going to cause them to be single precision values. Hence, when
you WRITE them, they come back with six or seven significant figures.

However, the I/O library itself throws another spanner in the works -
perhaps for your compiler, if the format of a WRITE is unspecified (for
formatted output), both double and single precision values will be
output as if they were single precision. I simply don't know. Vagaries
of I/O libraries & the associated interactions with hardware floating
point units are a notorious source of Heisenbugs.

There are many caveats to all of the above - and there are others on the
group who are far better qualified than I to explain the details.
However, I hope that gives you a rough idea of what's going on. If you
can make the question more specific, people will be able to offer more help.

Regards,

Richard
.



Relevant Pages

  • Re: Time Accuracy
    ... That means the 864,000 tenths of a second in one day can theoretically be handled by the available decimals. ... Perhaps I should have said unreliable digits rather than unreliable numbers. ... oddities of IEEE floating point representation. ... raised issues about the resolution of the Excel NOWfunction. ...
    (microsoft.public.excel.misc)
  • Re: Math issue
    ... <it is common to almost all computer software and hardware> ... There are implementations of the COBOL and other languages that do not suffer from this problem, because they use either scaled integer representations internally or true binary representations of numbers. ... We should be careful with descriptions like "digits beyond the 15th". ... If I re-format to number and expand the decimals ...
    (microsoft.public.excel)
  • Re: Randomness of digits within pi
    ... had 50 digits). ... Up to 800 million decimals after the point, I got a distribution of: ... output uniform distribution of output you should look for anomalies of ...
    (sci.math)
  • Re: Problem with IF Statement Using Complex Variables
    ... were barking up the wrong tree and therefore the suggestion, ... single precision, I've limited the input coefficients of the complex ... The input complex numbers are (limited to 7 decimals): ... The 1st number has 8 digits to the left of the decimal point and lost ...
    (comp.lang.fortran)
  • Re: Problems storing in a INTERVAL DAY(9) TO DAY field
    ... the actual digits. ... because it only gets one pair of digits (ndgts == 1) from dgts. ... there's also code in SQLCMD for dumping decimals and intervals ...
    (comp.databases.informix)