Re: Floating Point Precision





michael.mcgarry@xxxxxxxxx wrote On 09/09/05 14:02,:
> Hi,
>
> I have a question about floating point precision in C.
>
> What is the minimum distinguishable difference between 2 floating point
> numbers? Does this differ for various computers?

The smallest discernible difference depends on the
magnitude of the numbers: the computer can surely distinguish
1.0 from 1.1, but might not be able to tell 100000000000000.0
from 100000000000000.1 even though the two pairs of values
differ (mathematically speaking) by the same amount. You've
got to be concerned with relative error, not with absolute
error.

And yes: The relative error (loosely speaking, the precision)
will differ from one machine to another.

> Is this the EPSILON? I know in float.h a FLT_EPSILON is defined to be
> 10^-5. Does this mean that the computer cannot distinguish between 2
> numbers that differ by less than this epsilon?

First, I think you've misunderstood what FLT_EPSILON is.
It is the difference between 1.0f and the "next" float value,
the smallest float larger than 1.0f that is distinguishable
from 1.0f. That is, FLT_EPSILON is one way of describing the
precision of float values on the system at hand. Note that
although 1.0f is distinguishable from 1.0f+FLT_EPSILON,
1000000f need not be distinguishable from 1000000f+FLT_EPSILON.

Second, FLT_EPSILON is not necessarily 1E-5: the C Standard
requires that it be no greater than 1E-5, but permits lower
values (greater precision) for machines that support them.

> A problem I am seeing is a difference in values from a floating point
> computation for a run on a Windows machine compared to a run on a Linux
> machine. The values differ by 10^-6.

Without knowing what the values are, there's no way to tell
whether a difference of 1E-6 is huge or tiny. If the values
are supposed to be Planck's Constant (~6.6E-34), 1E-6 represents
an enormous error. If they're supposed to be Avogadro's Number
(~6.0E23) the difference is completely insignificant.

For the purposes of argument, let's say the values are in
the vicinity of 1. Then a difference of 1E-6 in float arithmetic
on a machine where FLT_EPSILON is 1E-5 is nothing to worry about;
you've already done better than you had any right to expect.

Beyond that, we get into the analysis of the origins and
propagation of errors, a field known as "Numerical Analysis."
The topic is simple at first but deceptively so, because it
fairly rapidly becomes the stuff of PhD theses. A widely-
available paper called (IIRC) "What Every Computer Scientist
Should Know about Floating-Point Arithmetic" would be worth
your while to read.

--
Eric.Sosman@xxxxxxx

.



Relevant Pages

  • Re: float vs. double?
    ... There have been repeated myths that float is faster than double. ... In antique machines, the ... Note that the floating-point ALU of Intel chips supports an 80-bit floating point number, ... is different precision being stored? ...
    (microsoft.public.vc.mfc)
  • Re: Interesting math
    ... Floating point number represents a real number with 6 digits precision. ... Floating point numbers are denoted by the keyword float. ...
    (alt.usage.english)
  • Re: Interesting math
    ... Floating point number represents a real number with 6 digits precision. ... Floating point numbers are denoted by the keyword float. ...
    (alt.usage.english)
  • Re: prePEP: Decimal data type
    ... a float (the D isn't part of the float syntax in Python, ... > #- context. ... The reason I say that they're rather different is that floating point and ... My personal opinion in the matter is that setting the precision high ...
    (comp.lang.python)
  • Re: Rounding error when converting from double to int
    ... If someone find one use for that broken float i'm interstted to hear it. ... Floating point is inherently approximate (don't let this spark another ... "Numerical Analysis". ...
    (comp.lang.c)