FPU, i386
--------- Received message begins Here ---------
Quoted 20 lines
Quoted 6 lines · Andrey Ulanov
Look at this:
$ cat test.c #include <stdio.h>
main()Quoted 11 lines
{ double h = 0.2; if(1/h == 5.0) printf("1/h == 5.0\n"); if(1/h < 5.0) printf("1/h < 5.0\n"); return 0; } $ gcc test.c
Quoted 13 lines
$ gcc -O test.c $ ./a.out 1/h == 5.0without -O, gcc initializes h to 0.2000000000000000111
Quoted 7 lines
$ ./a.out 1/h < 5.0 $I also ran same a.out under FreeBSD. It says "1/h == 5.0". It seems there is difference somewhere in FPU initialization code. And I think it should be fixed.
Nope. -O2 implies constant folding, and h is a constant. What you are compairing is runtime vs compile time values. 5.0 is compile time. 1/h where h is a constant is compile time (O2) and that would come out at 5.0 also
Been there done that... My solution (based on the problem I was working in) was to multiply both sides by the 10^<number of siginificant digits of the problem set>. Taking the simplistic approach:
if (int(1/h * 100) == int(5.0 * 100))
will give a "proper" result within two decimal places. This is still limited since there are irrational numbers within that range that COULD still come out with a wrong answer, but is much less likely to occur.
Exact match of floating point is not possible - 1/h is eleveated to a float.
If your 1/h was actually num/h, and num computed by summing .01 100 times I suspect the result would also be "wrong".
-------------------------------------------------------------------------
Jesse I Pollard, II
Email: [email protected]
Any opinions expressed are solely my own.
Quoted 47 lines · Jesse Pollard
--------- Received message begins Here ---------
Quoted 20 lines
Quoted 6 lines · Andrey Ulanov
Look at this:
$ cat test.c #include <stdio.h>
main()Quoted 11 lines
{ double h = 0.2; if(1/h == 5.0) printf("1/h == 5.0\n"); if(1/h < 5.0) printf("1/h < 5.0\n"); return 0; } $ gcc test.cQuoted 13 lines
$ gcc -O test.c $ ./a.out 1/h == 5.0without -O, gcc initializes h to 0.2000000000000000111
Quoted 7 lines
$ ./a.out 1/h < 5.0 $I also ran same a.out under FreeBSD. It says "1/h == 5.0". It seems there is difference somewhere in FPU initialization code. And I think it should be fixed.
Nope. -O2 implies constant folding, and h is a constant. What you are compairing is runtime vs compile time values. 5.0 is compile time. 1/h where h is a constant is compile time (O2) and that would come out at 5.0 also
Been there done that... My solution (based on the problem I was working in) was to multiply both sides by the 10^<number of siginificant digits of the problem set>. Taking the simplistic approach:
if (
int(1/h * 100)==int(5.0 * 100))
Quoted 8 lines
will give a "proper" result within two decimal places. This is still limited since there are irrational numbers within that range that COULD still come out with a wrong answer, but is much less likely to occur.
Exact match of floating point is not possible - 1/h is eleveated to a float.
If your 1/h was actually num/h, and num computed by summing .01 100 times I suspect the result would also be "wrong".
In most portable floating point work, you never use ==. Instead, the difference is compared to a small epsilon value and division is avoided by scaling.
john alvord
On Wed, Apr 17, 2002 at 09:40:48AM -0500, Jesse Pollard wrote:
...
Quoted 5 lines
Been there done that... My solution (based on the problem I was working in) was to multiply both sides by the 10^<number of siginificant digits of the problem set>. Taking the simplistic approach:
if (
int(1/h * 100)==int(5.0 * 100))
The common solution I've seen and used is
if (fabs(a-b) < e)
Set e according to your needs (1E-4 is good enough for many practical purposes, 1E-12 is better for my personal gut-feeling in many problems, 1E-16 and beyond does not make any sense what so ever (for double precision))
Signature · 6 lines
................................................................ : [email protected] : And I see the elder races, : :.........................: putrid forms of man : : Jakob Østergaard : See him rise and claim the earth, : : OZ9ABN : his downfall is at hand. : :.........................:............{Konkhra}...............:
Quoted 9 lines · Jesse Pollard
--------- Received message begins Here ---------
if (
int(1/h * 100)==int(5.0 * 100))will give a "proper" result within two decimal places. This is still limited since there are irrational numbers within that range that COULD still come out with a wrong answer, but is much less likely to occur.
Quoted 6 lines
Exact match of floating point is not possible - 1/h is eleveated to a float.
If your 1/h was actually num/h, and num computed by summing .01 100 times I suspect the result would also be "wrong".
why is exact match of floating point not possible ?
what i understand is if you do a " x/y " (where x and y are two integers ) division in hardware then you should always get the same value , then why can't we compare floating point "==" operation ?
I understand that in case of inrrational number it will not give a exact
value ......but division like 1/.2 is not irrational ! and it should always come to 5 !
rpm
rpm <[email protected]> writes:
|> I understand that in case of inrrational number it will not give a exact |> value ......but division like 1/.2 is not irrational ! and it should always |> come to 5 !
It is not about irrational number (of course, the result of dividing two rational number is always a rational number), but about finite precision. You simply cannot represent 0.2 finitely in base 2.
Andreas.
Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected] SuSE GmbH, Deutschherrnstr. 15-19, D-90429 Nürnberg Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5 "And now for something completely different."
Quoted 10 lines · rpm
Quoted 9 lines · Jesse Pollard
--------- Received message begins Here ---------
if (
int(1/h * 100)==int(5.0 * 100))will give a "proper" result within two decimal places. This is still limited since there are irrational numbers within that range that COULD still come out with a wrong answer, but is much less likely to occur.
Quoted 8 lines
Quoted 6 lines
Exact match of floating point is not possible - 1/h is eleveated to a float.
If your 1/h was actually num/h, and num computed by summing .01 100 times I suspect the result would also be "wrong".
why is exact match of floating point not possible ?
Because many (read most) numbers are not exactly representable in floating-point. The purpose of floating-point it to represent real numbers with a large dynamic range. The trade-off is that few such internal representations are exact.
As a simple example, 0.33333333333..... can't be represented exactly even with paper-and-pencil. However, as the ratio of two integers it can be represented exactly, i.e., 1/3 . Both 1 and 3 must be integers to represent this ratio exactly.
All real numbers (except trancendentials) can represented exactly as the ratio of two integers but floating-point uses only one value, not two integers, to represent the value. So, an exact representation of a real number, when using a single variable in a general-purpose way, is, for all practical purposes, not possible. Instead, we get very close.
When it comes to '==' close is not equal. There are macros in <math.h> that can be used for most floating-point logic. You should check them out. If we wanted to check for '==' we really need to do something like this:
double a, b;
some_loop() {
if(fabs(a-b) < 1.0e-38)
break;
}Where we get the absolute value of the difference between two FP variables and compare against some very small number.
To use the math macros, the comparison should be something like:
if (isless(fabs(a-b), 1.0e-38))
break;Cheers,
Dick Johnson
Penguin : Linux version 2.4.18 on an i686 machine (797.90 BogoMips).
Windows-2000/Professional isn't.
Quoted 3 lines · Richard B. Johnson
To use the math macros, the comparison should be something like:
if (isless(fabs(a-b), 1.0e-38)) break;
I might be saying something stupid, but, I was under the impression that floating point '==', assuming it follows IEEE rules, does exactly this.
I know for certain that it does not do memcmp(), as it has to deal with the exponent and mantissa being each off by +/-1 and <</>>1 respectively.
mark
Signature · 9 lines
[email protected]/[email protected]/[email protected] __________________________ . . _ ._ . . .__ . . ._. .__ . . . .__ | Neighbourhood Coder |\/| |_| |_| |/ |_ |\/| | |_ | |/ |_ | | | | | | \ | \ |__ . | | .|. |__ |__ | \ |__ | Ottawa, Ontario, Canada One ring to rule them all, one ring to find them, one ring to bring them all and in the darkness bind them... http://mark.mielke.cc/
Quoted 9 lines · Mark Mielke
Quoted 3 lines · Richard B. Johnson
To use the math macros, the comparison should be something like:
if (isless(fabs(a-b), 1.0e-38)) break;I might be saying something stupid, but, I was under the impression that floating point '==', assuming it follows IEEE rules, does exactly this.
A floating-point '==' tests for the two oprands to be exact. The only time they will be exact is:
float a, b; a = b = 10.000;
At this instant a = b and (a==b) will return TRUE. However, if by previous math, you expected a to equal 10.0, by doing:
a = b = 10.00
a += 12345.678; a -= 12345.678;
Now, 'a' is not equal to 'b' if the 'C' compiler actually performed the indicated operations (the compiler may obtimize them away).
In this case (a==b) will return FALSE because they are not equal.
Script started on Thu Apr 25 11:52:46 2002
# cat >xxx.c #include <stdio.h> #include <math.h>
int main()
{
double a, b;
a = b = 10.0;
printf("%d\n", (a==b));
a += 10.234567890;
printf("%d\n", (a==b));
a -= 10.234567890;
printf("%d\n", (a==b));
return 0;}
# gcc -o xxx xxx.c # ./xxx 1 0 0 # exit exit
Script done on Thu Apr 25 11:53:10 2002
So, as you can see, when first initialized with the same value, regardless of how inaccurately in can be represented, it does show equality.
However, as soon as I muck with it, add, then subtract the same number, it will no longer be egual.
This is why you NEVER use '==' when dealing with floating-point numbers. The use of '==' in floating-point operations is a bug. 'C' allows you to write buggy code. It will probably call a floating-point compare routine in the FPU (fcom, fcomp, fcompp) which will promptly return the 'not-equal' in C1 and the CPU flags.
'C' will even allow you to use floating-point numbers in loops, i.e.,
while(a--)
do_something_forever();Just because it's allowed, it doesn't mean that it's not a bug.
Cheers,
Dick Johnson
Penguin : Linux version 2.4.18 on an i686 machine (797.90 BogoMips).
Windows-2000/Professional isn't.