float.nan is not itself ?
Hello, why does this assertion fail: assert(float.nan == float.nan); there is the std.math.isNaN function which works correctly, but why can I not just use the comparison ? Thanks, Joshua
Re: float.nan is not itself ?
On Tuesday, 14 February 2012 at 15:39:37 UTC, Joshua Reusch wrote: Hello, why does this assertion fail: assert(float.nan == float.nan); there is the std.math.isNaN function which works correctly, but why can I not just use the comparison ? Thanks, Joshua Use `float.nan is float.nan`; all other expressions with NaNs in them will be false (or result in NaN).
Re: float.nan is not itself ?
Joshua Reusch: why does this assertion fail: assert(float.nan == float.nan); By design, the hardware that manages floating point numbers makes a NaN not equal to everything else, including other NaNs: http://en.wikipedia.org/wiki/NaN In D2 is performs a bitwise comparison, but keep in mind there are many different NaNs (I think double.nan and double.init are different in the bits too). Bye, bearophile
Re: float.nan is not itself ?
Thank you for the explanation ! bearophile wrote: Joshua Reusch: why does this assertion fail: assert(float.nan == float.nan); By design, the hardware that manages floating point numbers makes a NaN not equal to everything else, including other NaNs: http://en.wikipedia.org/wiki/NaN In D2 is performs a bitwise comparison, but keep in mind there are many different NaNs (I think double.nan and double.init are different in the bits too). Bye, bearophile
Re: float.nan is not itself ?
On 02/14/2012 07:48 AM, Bernard Helyer wrote: On Tuesday, 14 February 2012 at 15:39:37 UTC, Joshua Reusch wrote: Hello, why does this assertion fail: assert(float.nan == float.nan); there is the std.math.isNaN function which works correctly, but why can I not just use the comparison ? Thanks, Joshua Use `float.nan is float.nan`; all other expressions with NaNs in them will be false (or result in NaN). In the general case, std.math.isNaN works: import std.math; void main() { float f; assert(f is float.nan); // fails assert(isNaN(f)); // passes } Ali
Re: float.nan is not itself ?
On Tue, 14 Feb 2012 16:39:37 +0100, Joshua Reusch yos...@arkandos.de wrote: Hello, why does this assertion fail: assert(float.nan == float.nan); there is the std.math.isNaN function which works correctly, but why can I not just use the comparison ? Thanks, Joshua My favorite explanation: Would you expect this to pass? assert( sqrt(-1.0) == 0.0 / 0.0 );
Re: float.nan is not itself ?
On 14/02/2012 15:39, Joshua Reusch wrote: Hello, why does this assertion fail: assert(float.nan == float.nan); there is the std.math.isNaN function which works correctly, but why can I not just use the comparison ? A NaN typically denotes some kind of invalid computation. If the results of two invalid computations were considered equal, it would probably cause problems. Another way to think of it is to consider NaN as an unknown value. Two unknown values cannot be considered to be equal to each other. OK, so whether they're equal or not is actually unknown. If there were such a thing as bool.nan, equality comparisons in which at least one operand is NaN would probably evaluate to it. Indeed, null in SQL works this way. But in the absence of this in D, the best design has turned out to be to consider NaNs unequal. Stewart.
Re: float.nan is not itself ?
Stewart Gordon wrote: If there were such a thing as bool.nan ... it would be called not-a-boolean. Of course it may make sense to compute something using such poisoned values. But if such values make sense, D is not prepared to use them, especially there is no if_then_else_otherwise statement in D. -manfred