View Single Post
  #2  
Old July 16th 03, 06:43 AM
Gordon Burditt
external usenet poster
 
Posts: n/a
Default

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's allowable because FLT_EPSILON makes no promises when added to
numbers greater than 1.0.

Since this is an IEEE implementation with FLT_RADIX of 2, I'd expect
it to work
if (d + 4.0*FLT_EPSILON f)
puts("huh?");

for values of f where f = 4.0 and f 8.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...


There is no exact representation of most non-integer decimal numbers
in binary floating point. This is the case for 5.15002 above.
You can't get an exact representation without an INFINITE number of
bits, but the more bits you use, the closer you'll get. Expect
double - float to lose accuracy. And don't think about "decimal digits".
0.1 in binary is an infinite repeating fraction.

Gordon L. Burditt