Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NaN to Bool conversion: True or False?

What part of the C++ spec, or the IEEE float spec, states that a NaN value should convert to true as opposed to false?

If I look at the C++ standard section 4.12 Boolean Conversions it says:

A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.

Now IEEE floats say that NaN compares false to any other value. So whether NaN is true or false depends on how you do your comparision (below). Thus I presume there must be an explicit mention.

value == 0 ? false : true
value != 0 ? true : false

Now, what about conversion to an integer. The short program below shows that a variable NAN converted to an integer results in the minimum integer, whereas a constant gets converted to 0 (using GCC). This seems odd.

#include <iostream>
#include <cmath>

void write( double r, int i, bool b )
{
    std::cout << r << " == " <<  i << " == " << (b ? "True" : "False") << std::endl;
}

int main()
{
    double value = NAN;
    write( value, value, value );
    write( NAN, NAN, NAN );
}

Output:

nan == -2147483648 == True
nan == 0 == True

The conversion of a NaN to zero but bool conversion as True seems troubling. I also not that something like MatLab will convert a NaN to 0 using a function like int16.

So, what are the specifics of the relevant standards that state how NaN converts to boolean and integer values?

I'm tagging C as well, since while it may not define the boolean conversion, it probably defines an integral conversion and use in a conditional and I suspect C++ will follow the same rules

like image 375
edA-qa mort-ora-y Avatar asked Feb 06 '12 10:02

edA-qa mort-ora-y


People also ask

Is NaN true or false in Python?

Numpy follows the python standard for truth testing here, any numeric type evaluates to False if and only if its numerical value is zero. Note that truth testing with NaN values can be unintuitive in other ways as well (e.g., nan == nan evaluates to False ).

Is a bool a true or false?

5.1. This is the basis of all modern computer logic. In Python, the two Boolean values are True and False (the capitalization must be exactly as shown), and the Python type is bool.

Is bool automatically true or false?

bool "bar" is by default true, but it should be false, it can not be initiliazied in the constructor. is there a way to init it as false without making it static?

Is NaN boolean Python?

Test element-wise for Not a Number (NaN), return result as a bool array. Input array. For scalar input, the result is a new boolean with value True if the input is NaN; otherwise the value is False.


1 Answers

In both C and C++, the behaviour is undefined when converting NAN to an integer type (other than bool):

C99 6.3.1.4/1: When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined.

C++11 4.9/1: A prvalue of a floating point type can be converted to a prvalue of an integer type. The conversion truncates; that is, the fractional part is discarded. The behavior is undefined if the truncated value cannot be represented in the destination type. [ Note: If the destination type is bool, see 4.12. —end note ]

In both languages, converting NAN to bool (or _Bool) gives true (or 1):

C99 6.3.1.2/1: When any scalar value is converted to _Bool, the result is 0 if the value compares equal to 0; otherwise, the result is 1.

C++11 4.12/1: A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.

NAN is not a zero value, and doesn't compare equal to zero.

like image 161
Mike Seymour Avatar answered Oct 14 '22 13:10

Mike Seymour