简体   繁体   English

将负浮点值转换为无符号int的行为是什么?

[英]What is the behaviour on converting a negative floating point value into an unsigned int?

What happens if a negative floating point value is converted into a value of unsigned integral type? 如果将负浮点值转换为无符号整数类型的值会怎样? Standard quotes would be appreciated. 标准报价将不胜感激。 The problem I'm facing is conversion into values of unsigned integral types from a variant class, that contains an object of floating-point type. 我面临的问题是从包含浮点类型对象的变量类转换为无符号整数类型的值。

EXAMPLE: 例:

unsigned i = -.1;

In case the negative value is -1.0 or lower, it invokes undefined behavior since the integral part then cannot be represented by an unsigned number. 如果负值为-1.0或更低,则它将调用未定义的行为,因为整数部分然后不能用无符号数字表示。 Otherwise, (as in the case of -0.1), if it can be represented by an integer type, it is well-defined behavior. 否则,(如-0.1的情况),如果可以用整数类型表示,则它是明确定义的行为。 See the C11 standard, ISO 9899:2011: 请参阅C11标准,ISO 9899:2011:

6.3.1.4 6.3.1.4

When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (ie, the value is truncated toward zero). 当实数浮点型的有限值转换为_Bool以外的整数类型时,小数部分将被丢弃(即,该值将被截断为零)。 If the value of the integral part cannot be represented by the integer type, the behavior is undefined. 如果整数部分的值不能用整数类型表示,则行为是不确定的。 61) 61)

And then there is a non-normative foot note explaining the above text: 然后有一个非规范性的脚注解释了上面的文本:

61) The remaindering operation performed when a value of integer type is converted to unsigned type need not be performed when a value of real floating type is converted to unsigned type. 61)当将整数类型的值转换为无符号类型时,无需执行余数运算。 Thus, the range of portable real floating values is (−1, Utype_MAX+1). 因此,可移植实际浮点值的范围是(-1,Utype_MAX + 1)。

ISO/IEC 9899:1999 (C99) contains exactly the same text. ISO / IEC 9899:1999(C99)包含完全相同的文本。

It is undefined behaviour in C99 if the floating point number is less than or equal to -1.0. 如果浮点数小于或等于-1.0,则在C99中是未定义的行为。 If it's in the range (-1.0, 0.0), the resulting value will be 0. 如果在(-1.0,0.0)范围内,则结果值为0。

From C99, §6.3.1.4, paragraph 1 根据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 (ie, the value is truncated toward zero). 当实数浮点型的有限值转换为_Bool以外的整数类型时,小数部分将被丢弃(即,该值将被截断为零)。 If the value of the integral part cannot be represented by the integer type, the behavior is undefined 如果整数部分的值不能用整数类型表示,则行为不确定

Footnote 50 clarifies the behaviour for the (-1.0, 0.0) range. 脚注50阐明了(-1.0,0.0)范围内的行为。

Your example, unsigned i = -.1; 您的示例, unsigned i = -.1; is well-defined by both C11 and C99, and the result is i == 0 . 由C11和C99 定义良好 ,结果为i == 0

Quoted from N1570 , 6.3.1.4 Real floating and integer: 引用自N1570 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 (ie, the value is truncated toward zero). 当实数浮点型的有限值转换为_Bool以外的整数类型时,小数部分将被丢弃(即,该值将被截断为零)。 If the value of the integral part cannot be represented by the integer type, the behavior is undefined.61) 如果整数部分的值不能用整数类型表示,则行为是不确定的(61)。

61) The remaindering operation performed when a value of integer type is converted to unsigned type need not be performed when a value of real floating type is converted to unsigned type. 61)当将整数类型的值转换为无符号类型时,无需执行余数运算。 Thus, the range of portable real floating values is (-1, Utype_MAX+1). 因此,可移植实际浮点值的范围是(-1,Utype_MAX + 1)。

Quoted from N869 , 6.3.1.4 Real floating and integer: 引用自N869 6.3.1.4实数浮点数和整数:

#1 #1

When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (ie, the value is truncated toward zero). 当实数浮点型的有限值转换为_Bool以外的整数类型时,小数部分将被丢弃(即,该值将被截断为零)。 If the value of the integral part cannot be represented by the integer type, the behavior is undefined.43) 如果整数部分的值不能用整数类型表示,则行为是不确定的(43)。

43)The remaindering operation performed when a value of integer type is converted to unsigned type need not be performed when a value of real floating type is converted to unsigned type. 43)当将整数类型的值转换为无符号类型时,无需执行余数运算。 Thus, the range of portable real floating values is (-1, Utype_MAX+1). 因此,可移植实际浮点值的范围是(-1,Utype_MAX + 1)。

However, as you can see from the quotations, trying to convert floating-point constants outside the range (-1, Utype_MAX+1) invokes undefined behaviour. 但是,从引号中可以看到,尝试转换超出范围(-1,Utype_MAX + 1)的浮点常量会调用未定义的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM