Re: Variable Precision Problem



fanodisc@xxxxxxxxx wrote:

I've got a problem with this fortran line.

VAR1 = -VAR2 * VAR3 / 2. / VAR4 * .3 / 21000000.

where all variables are REAL*8 and:

VAR2 = 1800.
VAR3 = 0.3174
VAR4 = 0.0191
(snip)

1) the result is different from the output of a scientific calculator

Most, if not all, calculators (scientific or not) do arithmetic in
decimal. Most computers do it in binary, including floating point.
Many expressions round differently in binary.

2) the result is different from the same code written in c++

As others have noted, your double precision (REAL*8) variables
have single precision values. Add a D0 or the appropriate
KIND suffix to constants.

In C, C++, and Java constants default to double precision unless
they have a trailing f. That is, 0.1 is double, 0.1f is float.
For values like 0.1 that can't be exactly represented in binary
the stored values are different.

K&R C, (before the ANSI standard) did all floating point math
in double precision, converting to float only to store in a
float (single precision) variable. ANSI will do single
precision evaluation, and added float constants with the f
suffix.

-- glen

.



Relevant Pages

  • Re: float vs. double?
    ... There have been repeated myths that float is faster than double. ... In antique machines, the ... Note that the floating-point ALU of Intel chips supports an 80-bit floating point number, ... is different precision being stored? ...
    (microsoft.public.vc.mfc)
  • Re: Floating Point Precision
    ... > I have a question about floating point precision in C. ... The relative error (loosely speaking, the precision) ... It is the difference between 1.0f and the "next" float value, ... a field known as "Numerical Analysis." ...
    (comp.lang.c)
  • Re: Interesting math
    ... Floating point number represents a real number with 6 digits precision. ... Floating point numbers are denoted by the keyword float. ...
    (alt.usage.english)
  • Re: Interesting math
    ... Floating point number represents a real number with 6 digits precision. ... Floating point numbers are denoted by the keyword float. ...
    (alt.usage.english)
  • Re: prePEP: Decimal data type
    ... a float (the D isn't part of the float syntax in Python, ... > #- context. ... The reason I say that they're rather different is that floating point and ... My personal opinion in the matter is that setting the precision high ...
    (comp.lang.python)