templated isNaN

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Sat Aug 24 05:50:24 PDT 2013


On 24/08/13 13:53, Paolo Invernizzi wrote:
> Someone can suggest me a convenient way to declare an 'isNaN' templated function
> that plays well with the 'standard.math.isNaN'?
>
> The target is to be able to define isNaN functions for my custom structures, and
> I want to keep the same name...

Will this do?

//////////////////////////////////////

import std.math, std.traits;

bool isNaN(T)(T t)
{
     static if (isNumeric!T)
     {
         return std.math.isNaN(t);
     }
     else
     {
         return true;
     }
}

unittest
{
     assert(isNaN("NaN"));
     assert(!isNaN(0));
     assert(!isNaN(0.0));
     assert(isNaN(real.nan));
}

//////////////////////////////////////

Since you were kind enough to answer my accidental response to your mail, I 
thought I'd try and return the favour ... ;-)


More information about the Digitalmars-d-learn mailing list