Re: Float point comparison-Newbie

From: Gordon Burditt (gordonb.tzw9d_at_burditt.org)
Date: 11/11/04


Date: 11 Nov 2004 03:07:27 GMT


>Is it OK to compare a float variable with 0 i.e if (float_var ==
>0.0)?If it is not correct please let me know how to compare float
>variable with 0.

It depends on how you calculated the float variable and why you are
comparing it to zero. Floating point calculations are often subject
to roundoff error. An expression like (10.0*0.1 - 1.0) is likely
to *NOT* come out exactly zero.

If the floating point variable got the value 0.0 because you assigned
the constant 0.0 to it, or you fed the text "0.0" into strtod() or
[sf]scanf() as a marker for the end of the data, you are likely
safe. The float variable will likely have exactly zero in it. On
the other hand, if you have calculated it by subtracting two numbers
that ought to be equal, you may not get exactly zero when you
mathematically would expect to.

There are two commonly used methods of comparing two floating point
variables for equality. One tests if they are equal within a certain
amount. The other tests if they are equal within a certain percentage.
The percentage method doesn't work too well if the value you are
comparing against is zero (division by zero or nearly zero tends
not to work well).

Generally, which method you use and how you figure the threshhold
should be determined by what the numbers represent, *NOT* the
characteristics of the floating point numbers. For example, in the
USA, you might omit sending out a bill if the amount due is within
half a cent of zero. (Presuming here you ARE supposed to send out
notices of credit balances). The "half a cent" number is determined
by the fact that there aren't any coins smaller than a cent, and
anything less than half a cent would be rounded to zero. It has
nothing to do with how many bits there are in a floating point
number.

(I prefer to use integer quantities of cents (or whatever currency)
and explicitly round to the nearest cent where necessary. Sometimes
the law (e.g. that governing sales tax) has peculiar laws about
rounding unlike any conventional methods of rounding.)

You might check whether two voltages are "equal enough" by testing
if they are within 2% of each other. The reason you might choose
2% is because that is the accuracy specification of the meter you
are using to measure voltages.

                                        Gordon L. Burditt



Relevant Pages

  • Re: GFloat Rounding issue in DEC Basic V1.2-000
    ... I have manually done the calculations and am versed in the ... I just knew that the recriminations about using floating point would be fired out rather quickly, and John and Tom didn't disappoint me. ... Richard has the right concept, but I think his fingers got a bit ahead of him. ... if ABS< (your spec for zero) then it's zero. ...
    (comp.os.vms)
  • Re: atof and VC7.1
    ... > at their mother's knee NEVER to test for equality of floating point ... There are one or two exceptions (like testing for zero) ... when comparing two constants. ...
    (microsoft.public.vc.language)
  • Re: 5/0.0 = Infinity : NO error is thrown
    ... I have a bunch of calculations involving doubles where I want to check ... for div by zero, how shoud I go about it? ... For int, divide by 0 raises an exception. ... IEEE made up the floating point rules. ...
    (comp.lang.java.programmer)
  • Re: comparing two equal size vectors
    ... I am comparing two equal size vectors. ... Both vectors are double and vector B consists of values that should be in A, otherwise it's zero. ... May be you have floating point errors that prevent the above to work correctly? ...
    (comp.soft-sys.matlab)
  • REPRESENT revisited
    ... Forth-94 has few floating point display functions [none in the ... If u is greater than zero the character string shall consist ... If u is zero the string shall consist of one digit representing ...
    (comp.lang.forth)