double.init is real.nan, is it a bug?

bsd slackovsky at gmail.com
Wed Aug 14 23:20:01 PDT 2013


On Thursday, 15 August 2013 at 05:12:35 UTC, Jonathan M Davis 
wrote:
> On Thursday, August 15, 2013 06:01:21 bsd wrote:
>> Hi all,
>> 
>
> There are multiple values for NaN. It's still a bit surprising 
> to me that
> assert(val is double.nan) failed, but as long as val is still 
> one of the
> values for NaN, it shouldn't matter. It wouldn't have been at 
> all suprising
> though if you had two different NaNs which were generated 
> through arithmetic
> which didn't match. In general, you can't rely on the bit 
> pattern for NaN.
>
>> I also found this confusing:
>> ---
>> void main() {
>> 
>>      assert(double.init is double.nan); // passes expected
>> 
>>      assert(double.init is float.init); // passes, unexpected
>>      assert(double.init is real.init); // passes, unexpected
>> 
>>      assert(double.init is real.nan); // passes, unexpected
>>      assert(double.init is float.nan); // passes, unexpected
>> 
>> }
>> ---
>> I don't think these should be passing...should they??
>
> To do the comparison, the same size types must be compared, so 
> the smaller
> floating point types are promoted to the larger type in the 
> expression, and
> evidently, promoting the init value of a smaller floating point 
> value gives you
> the same value as the init value of the larger floating point 
> value.
>
> - Jonathan M Davis

This explains why (double.nan is float.nan) && (double.nan is 
real.nan). I understand that now, thanks.

I found std.math.isNaN in just after I posted, I really should 
read the docs first :D

For some reason I thought ('var' is double.nan) did this isNaN 
check, sort of like (key in AA) ... but 'in' is in and 'is' is, 
well, just is.

Thanks for the help.



More information about the Digitalmars-d-learn mailing list