NaN到布尔的转换:对还是错? [英] NaN to Bool conversion: True or False?
问题描述
C ++规范或IEEE浮动规范的哪一部分规定NaN值应转换为true
而不是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?
如果我查看C ++标准部分 4.12布尔转换,它会说:
If I look at the C++ standard section 4.12 Boolean Conversions it says:
零值,空指针值或空成员指针值是 转换为false;其他任何值都将转换为true.
A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.
现在,IEEE浮言说NaN将false与其他任何值进行比较.因此,NaN是正确还是错误取决于您进行比较的方式(如下).因此,我认为必须明确提及.
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
现在,如何转换为整数呢?下面的简短程序显示,将变量NAN转换为整数会生成最小整数,而将常量转换为0(使用GCC).这似乎很奇怪.
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 );
}
输出:
nan == -2147483648 == True
nan == 0 == True
将NaN转换为零,但将布尔值转换为True似乎很麻烦.我也不是说MatLab之类的东西会使用int16
之类的函数将NaN转换为0.
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
.
那么,说明NaN如何转换为布尔值和整数值的相关标准的细节是什么?
So, what are the specifics of the relevant standards that state how NaN converts to boolean and integer values?
我也在标记C,因为尽管它可能未定义布尔转换,但它可能定义了整数转换并在条件条件下使用,我怀疑C ++会遵循相同的规则 >
推荐答案
在C和C ++中,将NAN
转换为整数类型(bool
以外)时,行为均未定义:
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:当将实数浮点型的有限值转换为
_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:浮点类型的prvalue可以转换为整数类型的prvalue.转换被截断;即,小数部分被丢弃.如果无法在目标类型中表示截断的值,则行为未定义. [注意:如果目标类型是bool
,请参见4.12. -尾注]
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 ]
在两种语言中,将NAN
转换为bool
(或_Bool
)都会得到true
(或1
):
In both languages, converting NAN
to bool
(or _Bool
) gives true
(or 1
):
C99 6.3.1.2/1:将任何标量值转换为
_Bool
时,如果该值比较等于0,则结果为0;否则,结果为0.否则,结果为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:将零值,空指针值或空成员指针值转换为false
;其他任何值都将转换为true
.
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
不是零值,并且不等于零.
NAN
is not a zero value, and doesn't compare equal to zero.
这篇关于NaN到布尔的转换:对还是错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!