Re: Variable Precision Problem
- From: glen herrmannsfeldt <gah@xxxxxxxxxxxxxxxx>
- Date: Fri, 21 Sep 2007 09:08:50 -0800
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.(snip)
VAR3 = 0.3174
VAR4 = 0.0191
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
.
- Follow-Ups:
- Re: Variable Precision Problem
- From: robin
- Re: Variable Precision Problem
- From: tholen
- Re: Variable Precision Problem
- References:
- Variable Precision Problem
- From: fanodisc
- Variable Precision Problem
- Prev by Date: Re: Variable Precision Problem
- Next by Date: Re: Hel needed - URGENT - math problems in fortran 90
- Previous by thread: Re: Variable Precision Problem
- Next by thread: Re: Variable Precision Problem
- Index(es):
Relevant Pages
|