## digitalmars.D.learn - float.nan is not itself ?

- Joshua Reusch (5/6) Feb 14 2012 there is the std.math.isNaN fu...
- Bernard Helyer (3/9) Feb 14 2012 Use `float.nan is float.nan`; ...
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (10/23) Feb 14 2012 In the general case, std.math....
- bearophile (6/9) Feb 14 2012 By design, the hardware that m...
- Joshua Reusch (2/11) Feb 14 2012 ...
- =?utf-8?Q?Simen_Kj=C3=A6r=C3=A5s?= (4/10) Feb 14 2012 My favorite explanation: Would...
- Stewart Gordon (10/15) Feb 14 2012 A NaN typically denotes some k...
- Manfred Nowak (6/7) Feb 14 2012 ... it would be called not-a-b...

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

Feb 14 2012

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).

Feb 14 2012

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

Feb 14 2012

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

Feb 14 2012

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

Feb 14 2012

On Tue, 14 Feb 2012 16:39:37 +0100, Joshua Reusch <yoschi 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 );

Feb 14 2012

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.

Feb 14 2012

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

Feb 14 2012