View Single Post
  #1  
Old July 16th 03, 03:25 AM
Dylan Nicholson
external usenet poster
 
Posts: n/a
Default Floating point error?

The following C code:

float f = 5.15002;
double d = 5.15002;
if (d + FLT_EPSILON f)
puts("huh?");

Where 'FLT_EPSILON' is defined as the minimum number whereby 1.0 +
FLT_EPSILON != 1.0, causes the string "huh!" to be printed out on my
system (Pentium III)
I have included the relevant assembly code below.
Now is this a bug in the intel chip, or allowable because FLT_EPSILON
makes no promises when added to numbers greater than 1.0?

It doesn't happen when 'd' is another float, so presumably it has
something to do with double-precision/single-precision conversions
too.

Curious...

Dylan


8: float f = 5.15002;
00401028 C7 45 FC F7 CC A4 40 mov dword ptr [ebp-4],40A4CCF7h
9: double d = 5.15002;
0040102F C7 45 F4 D2 FB C6 D7 mov dword ptr
[ebp-0Ch],0D7C6FBD2h
00401036 C7 45 F8 9E 99 14 40 mov dword ptr [ebp-8],4014999Eh
10: if (d + FLT_EPSILON f)
0040103D DD 45 F4 fld qword ptr [ebp-0Ch]
00401040 DC 05 28 60 42 00 fadd qword ptr
[__real@8@3fe88000000000000000 (00426028)]
00401046 D9 45 FC fld dword ptr [ebp-4]
00401049 DE D9 fcompp
0040104B DF E0 fnstsw ax
0040104D F6 C4 41 test ah,41h
00401050 75 0D jne main+4Fh (0040105f)
11: puts("huh?");
00401052 68 1C 60 42 00 push offset string "huh?"
(0042601c)
00401057 E8 34 00 00 00 call puts (00401090)
0040105C 83 C4 04 add esp,4
12: return 0;
0040105F 33 C0 xor eax,eax