templated isNaN

2013-08-24 Thread Paolo Invernizzi
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...


---
import std.math;

bool isNaN(T)(T t) if(is(T==string)){ return true; }

// that is really necessary? What if I have my isNaN in different 
modules? Must duplicate?

// bool isNaN(T)(T t) if(is(T:real)) { return std.math.isNaN(t); }

void foo(){
bool b = isNaN(0.0);
}

/d471/f783.d(9): Error: template f783.isNaN does not match any 
function template declaration. Candidates are:

/d471/f783.d(3):f783.isNaN(T)(T t) if (is(T == string))
/d471/f783.d(9): Error: template f783.isNaN(T)(T t) if (is(T == 
string)) cannot deduce template function from argument types 
!()(double)

---

Thanks in advance!

- Paolo Invernizzi


Re: templated isNaN

2013-08-24 Thread Joseph Rushton Wakeling

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


Re: templated isNaN

2013-08-24 Thread Joseph Rushton Wakeling

On 24/08/13 14:50, Joseph Rushton Wakeling wrote:

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


Note that the correct if () condition here depends on how you want your isNaN to 
behave in certain cases.  Using isNumeric will mean isNaN('c') returns true.  If 
you want isNaN to return false when passed a char type, use your if(is(T : 
real)) instead.