铸造是在C标准定义的负向双unsigned int类型的行为? [英] Is the behaviour of casting a negative double to unsigned int defined in the C standard?

查看:107
本文介绍了铸造是在C标准定义的负向双unsigned int类型的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有上似乎得到不同的结果,不同的平台上运行code。我要寻找一个合适的解释。

Windows系统:

 双DBL = -123.45;
INT d_cast =(无符号整数)DBL;
// d_cast == -123

的WinCE(ARM):

 双DBL = -123.45;
INT d_cast =(无符号整数)DBL;
// d_cast == 0

编辑:

感谢您的指向正确的方向。

修复

 双DBL = -123.45;
INT d_cast =(符号)(INT)DBL;
// d_cast == -123
//适用于两者。


解决方案


这种转换是不确定的,因此不便于携带。

据C99§6.3.1.4脚注50:


  

当真正的浮动类型的值转换为无类型无需执行时整数类型的值转换为无符号类型进行remaindering操作。因此,便携式实浮动值的范围是(-1,Utype_MAX + 1)。


I have code that runs on different platforms that seems to get different results. I am looking for a proper explanation.

Windows :

double dbl = -123.45; 
int d_cast = (unsigned int)dbl; 
// d_cast == -123

WinCE (ARM):

double dbl = -123.45; 
int d_cast = (unsigned int)dbl; 
// d_cast == 0

EDIT:

Thanks for pointing in the right direction.

fix

double dbl = -123.45; 
int d_cast = (unsigned)(int)dbl; 
// d_cast == -123
// works on both. 

解决方案

No


This conversion is undefined and therefore not portable.

According to C99 §6.3.1.4 footnote 50:

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. Thus, the range of portable real floating values is (−1, Utype_MAX+1).

这篇关于铸造是在C标准定义的负向双unsigned int类型的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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