var::VariantType_Double::equals fails!


#1

The method returns true, but this is simply not true.

firstValue == 0.099999999999999978
secondValue == 0.10000000000000001

(VS2015, 32bit)


#2

The comparison is done like this:

    return std::abs (otherType.toDouble (otherData) - data.doubleValue) < std::numeric_limits<double>::epsilon();

which seems pretty sensible to me. And given that both the numbers you have there are clearly rounding errors from 0.1, a comparison that shows them as equal seems like the right result!


#3

Sorry, the current behavior is fuzzy logic, i cannot accept this answer

c++ has a built-in operator for for comparing doubles, its ==
otherType.toDouble(otherData) == data.doubleValue
we should trust the compiler


#4

Comparing floating point values with == is undefined behaviour! (except when one of them is zero)

There’s actually a compiler warning you can enable to warn you about this, and it’s really bad style to do it!


#5

Good Points! Any ideas how to turn on the compiler warnings on Visual C++ or XCode?


#6

What about comparisons between small numbers ? with var a=0.0, b=1e-16, then (a==b) is true. I’m not sure it is a good idea to redefine operator== for doubles.


#7

I’m not sure it’s a good idea to use operator== on floating point numbers at all - in C++ it’s undefined behaviour, and god only knows what you’d expect to happen in javascript!


#8

Comparison of any type in C++ to another is never undefined behaviour, so long as it has an equivalent operator, i.e. it compiles - assuming the value was stored into the location by the C++ abstract machine. Type-punning breaks this, and is not allowed anyway.

That includes doubles as well, and they have precisely defined semantics for any possible representation.

The standard way of doing floating point comparisons is scaling an epsilon to the magnitude of the arguments, allowing for a threshold. That current comparison falsely returns equal for over half of the possible values of doubles (numeric_limits::epsilon is not a small number).

Javascript defines Number (guaranteed to be a double) equality to be value equality, i.e. operator == in C++:
http://es5.github.io/#x11.9.3