Re: Double comparisons for equality



Also, I think we'd better no use == for the floating point comparison, just
use subtract operation, and compare the result with a very "little"
number(a tolerance level), just as "Comparing floating point numbers"
section in the link below pointed out:

"One consequence of all of this is that you should very, very rarely be
comparing binary floating point numbers for equality directly. It's usually
fine to compare in terms of greater-than or less-than, but when you're
interested in equality you should always consider whether what you actually
want is near equality: is one number almost the same as another. One simple
way of doing this is to subtract one from the other, use Math.Abs to find
the absolute value of the difference, and then check whether this is lower
than a certain tolerance level. "

"Binary floating point and .NET"
http://www.yoda.arachsys.com/csharp/floatingpoint.html

I think this should be a very important aspect of floating point.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

.



Relevant Pages

  • Re: Float comparison
    ... I know that you must keep some advices about comparing floating- ... points values for equality, but why I cannot use this method? ... Because floating point values are not exact. ...
    (comp.lang.c)
  • Re: Comparission cause problem
    ... > Now my concern about my code is that In some palces I am comparing the ... It is a principal problem in how floating point works in computers. ... Always take an epsilon value into account. ... due to different ranges (such as adding or subtracting values whos range ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Problem with floating point numbers.
    ... you are comparing to 4.5 and hoping that magically all the round off ... The floating point problem is not unique to SKILL. ... One good way of comparing floating point numbers in SKILL is to compare ... their printed representations because that is most often what you ...
    (comp.cad.cadence)
  • Re: Comparission cause problem
    ... >> It's true that comparing for equality is usually wrong ... >> when using floating point numbers, ... and it's in defining the appropriate concept ... When for example you want to jump out of a block when var is 0 or smaller, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Violation of Conversion rule of C99.
    ... warnings for comparing with 0.0. ... I have enough faith in implementors ... floating-point representations. ... binary floating point representation, that goes without saying. ...
    (comp.lang.c)

Loading