Re: Value close to zero.



"David " <dave@xxxxxxxxxxxxxx> wrote in message <ftvcqt$mk5
$1@xxxxxxxxxxxxxxxxxx>...
"Sebastiaan " <s.breedveld@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
wrote in message <ftv8nm$hkv$1@xxxxxxxxxxxxxxxxxx>...
In addition to the explanation of Roger, checking if a
floating point value is zero may become a problem.

E.g. this:
if x==0

might not work if x is a floating point number, since it
might contain round-off errors. Instead, check if x is
within the machine precision:

if x<eps

Regards,
Sebastiaan

note that you want to check the absolute value of x
against eps since roundoff error can result in small
negative numbers also.
---------
Just comparing some quantity x with matlab's 'eps' is not an adequate test
for its being essentially zero or otherwise. What it should be tested against
depends, first of all, on the size of the numbers from which it was computed,
and second on the nature of the computations that were performed.

For example, if we have

x = 1e43*(cos(pi/3)-1/2);

which ought to be zero if it were not for rounding errors, a single bit
rounding error in computing cos(pi/3)-1/2 will be magnified by the
enormous factor of 1e43, so we should therefore be using eps(1e43) instead
of simply eps.

If we have

x = sum(sin(2*pi/1000000*(1:1000000)));

which ideally should also be zero, we have to take into consideration the total
number of summands used here. The accumulated error due to rounding in a
million terms will amount to a very much larger error than a simple eps.

Allowing for the proper tolerance in distinguishing between quantities that
are likely to differ from zero only because of rounding errors, as opposed to
those whose values are significantly different from zero, is a task that
requires some degree of care - something that takes into account the
magnitude of expected rounding errors at all stages of the computation that
led up to the numbers.

Roger Stafford

.



Relevant Pages

  • Re: GFloat Rounding issue in DEC Basic V1.2-000
    ... issues surrounding the use of floating point numbers, rounding errors, and ... floating point guard digits when doing comparisons? ... Mike Ober. ... negative zero and zero values in hexadecimal. ...
    (comp.os.vms)
  • Re: Weirdness: (-0.666667 + 0.333333)
    ... > becuase of rounding errors. ... > I'm writing a program that draws graphs on a cartesian plane, ... The only thing you can do is to compare your value of x to zero ...
    (comp.lang.cpp)
  • Re: Weirdness: (-0.666667 + 0.333333)
    ... x - epsilon() < 0 ... >>becuase of rounding errors. ... The only thing you can do is to compare your value of x to zero ...
    (comp.lang.cpp)
  • Re: Round off error
    ... For me, I get zero in the third step, not a very small number. ... Public Sub Test2 ... Dim di as Double ... Ultimately, though, the only way to prevent all rounding errors is to ...
    (microsoft.public.excel.programming)
  • Re: sorry .09 instead of .08
    ... zero wrote: ... you're in the same boat as using a normal double when it comes to rounding errors. ... rounding errors (plus overflow errors for added fun). ...
    (comp.lang.java.help)