From: cochiti.lanl.gov!jlg@lanl.gov (Jim Giles)
Subject: Re: Numeric problem with Ada
Date: 19 Nov 91 00:23:10 GMT [thread overview]
Message-ID: <1991Nov19.002310.23171@beta.lanl.gov> (raw)
At least on the Sun/Sparc, C is compiled so that it _always_ uses
double precision for intermediate calculations - no matter how
explicitly you ask for just (float).
main()
{
float z;
int mz;
mz = 7;
z = 2.6 * mz - 0.2;
if ( z == 18.0 ) printf ("%2.6f\n", z);
}
This program always prints `18.000000' even if mz is set in a separately
compiled procedure. The reason is that the calculation in the assignment
statement to `z' is always done in double and then rounded to float when
the assignment is done. The same continues to be true even if the
expression is altered to:
z = (float) 2.6 * (float) mz - (float) 0.2;
So, that's why C always gives 18.000000 on Sun/Sparc. I suspect the
same may be true of other C implementations.
J. Giles
next reply other threads:[~1991-11-19 0:23 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
1991-11-19 0:23 Jim Giles [this message]
-- strict thread matches above, loose matches on Subject: below --
1991-11-27 16:19 Numeric problem with Ada CNAM.CNAM.FR!bortz
1991-11-21 8:57 Thomas Buenermann
1991-11-19 18:30 psinntp!vitro.com!v7.vitro.com!vaxs09
1991-11-19 14:05 Jean-Pierre Rosen
1991-11-14 9:53 Jan Kok
1991-11-13 19:48 CNAM.CNAM.FR!bortz
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox