Re: comparing doubles for equality



John Smith wrote:
This code for the comparison of fp types is taken from the C FAQ.
Any problems using it in a macro?

/* compare 2 doubles for equality */
#define DBL_ISEQUAL(a,b) (fabs((a)-(b))<=(DBL_EPSILON)*fabs((a)))

Do the same issues involved in comparing 2 fp types for equality
apply to comparing a float to zero? E.g. is if(x == 0.0) considered harmful?
Depending on how you use this, you could make your application highly dependent on external issues, such as whether you compile for an extra precision mode, or disable gradual underflow.
.



Relevant Pages

  • Re: comparing doubles for equality
    ... Any problems using it in a macro? ... Do the same issues involved in comparing 2 fp types for equality ... apply to comparing a float to zero? ... highly dependent on external issues, ...
    (comp.lang.c)
  • Re: Comparission cause problem
    ... >>Due to higher precision i would recommend to use the datatype double ... floating points then we should take the prefrence for the double data type. ... as you suggested that double will be faster as compared with the float. ... when it comes to comparing floating point types for equality you ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Float comparison
    ... values for equality, but why I cannot use this method? ... Your method is still comparing for equality, ... Oh, and as you consider me to be one of the "regs" I could consider some of the exchanges between you and Kenny as insulting to me, exchanges where the last response was yours agreeing with Kenny. ...
    (comp.lang.c)
  • Re: Defect Report: Value and object representation
    ... Is there any non-integer type where comparing ... operations on float values were always ... it counts as a "conversion" any more; ... know that values are preserved when promoting "up the line" ...
    (comp.std.c)
  • Re: possible to use Bang ! notation in place of "Not" keyword?
    ... Note that C# overloads the '==' and '!=' operators so that they work with both value types and reference types. ... Other programming languages, such as VB, provide different operators for comparing value equality and object identity. ...
    (microsoft.public.dotnet.languages.vb)