LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Why NaN to integer doesn't return type default value, 0?

Is there a neat answer to this question, why a conversion to integer types from NaN returns 2147483648 for signed 32 bits for example?

 

Why not -2147483647 ?

 

Why not 0 ?

0 Kudos
Message 1 of 4
(2,756 Views)

As far as IEEE is concerned the conversion from NaN to integer is undefined and unspecified, and therefore it is implementation specific.

 

I have seen documentation that Intel has defined the instruction to return 0x80000000 (the value you see), I do not know about other platforms.  I would not rely on this or any other value in your code and handle NaNs explicitly during all operations where you expect finite results such as conversions to integer.

Message 2 of 4
(2,739 Views)

The choice is arbitrary, but one argument for using an extremum is to match how Inf and out of range values behave. Even though there is no "error" code for integers, we already have a situation where the max and min values aren't guaranteed to be valid. Adding another case where zero may or may not be a valid result complicates it even further.

 

That said, you really need to explicitly check up front for all the special values whenever you care about the behavior they produce in a conversion.

0 Kudos
Message 3 of 4
(2,631 Views)

If you're going to ask "why not" for two potential values, why would you ask for the one value without offering any justification?

 

I could just as easily say:

 

Why not -1?

Why not 1?

Why not 2147483648?

Why not program your Vi in a way that handles NaN how you want it to be handled instead of worrying about the reasoning behind an arbitrary decision?

0 Kudos
Message 4 of 4
(2,603 Views)