Floating point problems (accuracy).
Phil Prentice
philp.cheer@btinternet.com
Tue Jan 18 20:47:00 GMT 2005
Hi
We are having a few problems regarding floating point inaccuracies under
Linux. Please see the code below.
/*
NOTE:- This code is an example which demonstrates the sort of problems
we are getting.
*/
main()
{
int i;
double x;
x = -5.765;
i = x * 100000;
printf("I = %d\n",i);
}
/*
We are porting code from a Sun Workstation to a Linux PC. We have noticed
inaccuracies of double values on the Linux PC. If we simply compile the
above program e.g "gcc t.c" when we run it we get -576499 when we were really
expecting -576500. We discovered that if we specify some flags:-
gcc -march=pentium4 -mfpmath=sse t.c
then suddenly the floating point values become more accurate (i.e. we get the
value -576500) and other related problems go away (our results agree with the
same tests running on the Sun).
These flags only seem to work on a pentium4 or an AMD-64, i.e the newer
processor chips. We would have liked the solution to be more generic.
Does any one know a better way of fixing this?
What was the solution for the older processor chips?
Is there a generic solution?
Thanks for all your help
Phil Prentice.
More information about the Gcc
mailing list