I just ran into this line of code;
if( lineDirection.length2() ){...}
where length2 returns a double. It kind of puzzles me that 0.0 in this case is equivalent to 0\NULL.
Is this part of the C++ standard or is it undefined behaviour?
I just ran into this line of code;
if( lineDirection.length2() ){...}
where length2 returns a double. It kind of puzzles me that 0.0 in this case is equivalent to 0\NULL.
Is this part of the C++ standard or is it undefined behaviour?
Yes - the comparison is against zero (0.0), and returns false if the result is exactly zero, and true otherwise.
The behaviour is inherited from C, which treats the equivalent comparison the same way.
When you compare without operators, you are comparing "against true", so that every variable type is checked to be either boolean (simple case) or other. Numeric variable types has their false value defined as "0", "0.0" or so, so when you compare them "against true", your comparison will return false.
It's worth noting that this code is extremely brittle for floating-point representations. This code will work if and only if the floating point value is exactly 0, which is actually pretty unlikely in most circumstances. It may not be in this particular case, but it should certainly be documented / commented if so.
In practically all other situations you need to decide on an "epsilon value" that defines the range of floating point numbers you consider "the same" - otherwise your comparisons are very likely to surprise you in corner (and often not-so-corner) cases.
It is a very much Standard Behavior (Boolean Conversion)
$4.12/1 - "An rvalue of arithmetic, enumeration, pointer, or pointer to member type can be converted to an rvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true."
If length2 returns 0.0 it will be taken as false. But you might get surprising result with this comparison. Better use epsilon value as MadKeithV suggested during floating point comparison.