www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - std.math.isIdentical and NaN

reply Dan Olson <gorox comcast.net> writes:
Docs for isIdentical say:

   Same as ==, except that positive and negative zero are not identical,
   and two NANs are identical if they have the same 'payload'.

However, it returns false for NaN's with different signbits but same
payload.  Should this be the case?

Ran into this because isIdentical is used in unittests to compare NaN's
but I find an occassional test fails due to signbit for some operations
on LDC ARM.  I wrote an isNaNWithPayload() predicate for the failing
tests, but wonder if isIdentical should ignore signbit for NaNs or have
its docs changed.
-- 
Dan
Jun 22 2015
parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Monday, 22 June 2015 at 08:10:20 UTC, Dan Olson wrote:
 Docs for isIdentical say:

    Same as ==, except that positive and negative zero are not 
 identical,
    and two NANs are identical if they have the same 'payload'.

 However, it returns false for NaN's with different signbits but 
 same payload.  Should this be the case?

 Ran into this because isIdentical is used in unittests to 
 compare NaN's but I find an occassional test fails due to 
 signbit for some operations on LDC ARM.  I wrote an 
 isNaNWithPayload() predicate for the failing tests, but wonder 
 if isIdentical should ignore signbit for NaNs or have its docs 
 changed.
Well, given the name of the function and the fact that it's clearly trying to check for NaN equality rather than treating their comparison as always false, I would have expected that it would be true for NaNs if they were absolutely identical, which presumably includes the signbit, but I really don't know much about how NaNs are implemented, so I don't know what the implications of that are. Still, it seems odd to compare part of the NaN for equality but not all of it. - Jonathan M Davis
Jun 22 2015
next sibling parent Dan Olson <gorox comcast.net> writes:
"Jonathan M Davis" <jmdavisProg gmx.com> writes:

 On Monday, 22 June 2015 at 08:10:20 UTC, Dan Olson wrote:
 Docs for isIdentical say:

    Same as ==, except that positive and negative zero are not
 identical,
    and two NANs are identical if they have the same 'payload'.

 However, it returns false for NaN's with different signbits but same
 payload.  Should this be the case?

 Ran into this because isIdentical is used in unittests to compare
 NaN's but I find an occassional test fails due to signbit for some
 operations on LDC ARM.  I wrote an isNaNWithPayload() predicate for
 the failing tests, but wonder if isIdentical should ignore signbit
 for NaNs or have its docs changed.
Well, given the name of the function and the fact that it's clearly trying to check for NaN equality rather than treating their comparison as always false, I would have expected that it would be true for NaNs if they were absolutely identical, which presumably includes the signbit, but I really don't know much about how NaNs are implemented, so I don't know what the implications of that are. Still, it seems odd to compare part of the NaN for equality but not all of it.
Yeah, based on the name "isIdentical", its current behaviour assert(!isIdentical(-NaN(0xabc), NaN(0xabc)); does seem to correct. In that case the documentation could say "NaNs are identical if they have the same payload and signbit".
Jun 22 2015
prev sibling parent ketmar <ketmar ketmar.no-ip.org> writes:
On Mon, 22 Jun 2015 09:41:08 +0000, Jonathan M Davis wrote:

 Still, it seems odd to compare
 part of the NaN for equality but not all of it.
it's very useful for NaN-boxing.=
Jun 23 2015