This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Floating point problems (accuracy).
- From: Phil Prentice <philp dot cheer at btinternet dot com>
- To: gcc-help at gnu dot org,gcc at gnu dot org
- Cc: mike at stec dot clara dot co dot uk
- Date: Tue, 18 Jan 2005 18:33:06 +0000
- Subject: Floating point problems (accuracy).
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.