问题铸件NAN彩车为int [英] Problems casting NAN floats to int

查看:142
本文介绍了问题铸件NAN彩车为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

忽略我为什么会想这样做,754 IEEE FP标准没有定义下列行为:

Ignoring why I would want to do this, the 754 IEEE fp standard doesn't define the behavior for the following:

float h = NAN;
printf("%x %d\n", (int)h, (int)h);

Gives: 80000000 -2147483648

基本上,无论我给什么NAN值时,输出8000(十六进制)或-2147483648(DEC)。是否有一个原因和/或这是正确的行为?如果是这样,怎么来的?

Basically, regardless of what value of NAN I give, it outputs 80000000 (hex) or -2147483648 (dec). Is there a reason for this and/or is this correct behavior? If so, how come?

我给它NaN的值不同的方式在这里:
<一href=\"http://stackoverflow.com/questions/10365328/how-can-i-manually-set-the-bit-value-of-a-float-that-equates-to-nan\">How我可以手动设置等同于楠浮动的位值?

The way I'm giving it different values of NaN are here: How can I manually set the bit value of a float that equates to NaN?

因此​​,基本上,是那里的楠有效载荷影响铸件的产量情况?

So basically, are there cases where the payload of the NaN affects the output of the cast?

谢谢!

推荐答案

一个浮点数强制转换为整数的结果是不确定的值不在整数变量(±1截断)的范围。

The result of a cast of a floating point number to an integer is undefined for values not in the range of the integer variable (±1 for truncation).

A NaN的超出范围,所以结果是不确定的。

A NaN is out of range, so the result is undefined.

事实上,该标准只提到转换的有限的浮点类型为整数类型的值。

In fact, the standard only mentions the conversion of finite values of floating point types to integer types.

这篇关于问题铸件NAN彩车为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆