Not A Number

Is there a way to set a float variable equal to NaN?


sqrt(-1)? I figured this out once, but I can’t remember how I did it anymore…

(rip patrick mcgoohan, now I can never answer questions about NaN without being sad :( )

From memory:

__device__ float makeNaN()


   return __int_as_float(0x7FFFFFFF);


All right, here’s a better answer than the one I gave: use nanf() to generate a float NaN, nan(“string”) to generate a DP NaN:

float myNan = nanf("");

double myNanDec = nan("12345");

double myNanHex = nan("0xABDEF");

(another dev here told me that we support these C99 functions for creating a NaN, so that definitely seems like the optimal way to do it)

It seems bizarre to call a device function that takes a HOST POINTER as an argument.

So perhaps this is a case where the compiler is guaranteed to parse the string at compile time to generate the proper result?

NVidia Engineers: it would be great if you could elaborate on SPWorley’s comment. Specifically, if we introduce a static constant string into device code, is this string allocated and initialized in the device memory, is it optimized out into a set of instructions, etc?

Besides the issue, raised in this thread, I’ve started a thread some while ago:…mp;#entry498082

in which a somewhat related topic has been discussed.

I understand, that Cuda is not intended as a string processing toolkit (at least not for static strings), but having some basic string functionality, such as strncpy(), strncat(), etc., or similar functions from the STL might be handy. Building and/or using this functionality requires understanding of how static strings are implemented.


Okay I misunderstood the email I got–let me try this and see how it works…

Well, the wonderful person who told me about this function tried this, and the compiler indeed does the right thing. Hooray false alarms!

As far as the other issue goes… let me think about that some more before I shoot my mouth off like I am prone to do.