www.digitalmars.com         C & C++   DMDScript  

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

reply Joshua Reusch <yoschi arkandos.de> writes:
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
next sibling parent reply "Bernard Helyer" <b.helyer gmail.com> writes:
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
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
prev sibling next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
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
parent Joshua Reusch <yoschi arkandos.de> writes:
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
prev sibling next sibling parent =?utf-8?Q?Simen_Kj=C3=A6r=C3=A5s?= <simen.kjaras gmail.com> writes:
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
prev sibling parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
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
parent Manfred Nowak <svv1999 hotmail.com> writes:
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