从double到unsigned int的转换 [英] conversion from double to unsigned int

查看:1817
本文介绍了从double到unsigned int的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Double值转换为int值时遇到问题。我尝试运行下面的代码:

$ pre $ $ $ $ $ $ $ $ $ $ $ $ -05;
cout<< (1 / val_d)< ENDL;
unsigned int val_ui =(unsigned int)(1 / val_d);
cout<< val_ui<< ENDL;





$ p从double到int的转换可能会删除小数部分,但整数部分应保持为



我得到的输出是:
16000
15999



<那么为什么这里的o / p不同呢?
这只发生在fedora上。在Windows和Ubuntu上,它工作正常。 (两个输出都是16000)

我调整了上面的代码,得到了以下结果:

  int main()
{
double val_d = 6.25e-05;
cout<< (1 / val_d)< ENDL;
double val_intermediate =(1 / val_d);
cout<< val_intermediate<< ENDL;
unsigned int val_ui =(unsigned int)val_intermediate;
cout<< val_ui<< ENDL;





$ b新的输出是
16000
16000
16000

解决方案

当源文本6.25e-05被解释为十进制数字并转换为双精度,因为浮点值的精度有限,每个位的值都是2的幂,而不是十进制数,所以不完全可以表示。最接近6.25e-5的IEEE 754双精度值是6.25000000000000013010426069826053208089433610439300537109375e-05,或者在十六进制浮点型0x1.0624dd2f1a9fcp-14中。

当这个倒数倒数时,确切的数学结果又不能精确表示,所以必须再次舍入。最接近的双精度值是16000或0x1.f4p + 13。

C ++标准允许实现比标称类型更精确地计算浮点表达式。一些实现使用扩展精度,特别是Intel的80位浮点类型,它有一个64位有效数字。 (常规双精度有53位有效数)。在这个扩展精度下,倒数是0xf.9fffffffffffe89p + 10或15999.99999999999966693309261245303787291049957275390625。

显然,当扩展精度结果被截断为一个整数,结果是15999.

将long-double结果舍入为double将产生16000.(可以通过显式强制转换来实现double ;您不需要将中间值赋给双重对象。)


I am facing a problem with the conversion of value from Double to int. I try to run the following code :

int main()
{
    double val_d= 6.25e-05;
    cout << (1/val_d) << endl;
    unsigned int val_ui = (unsigned int ) (1/val_d);
    cout << val_ui << endl;
}

conversion from double to int may remove decimal part but integer part should remain as it is ?

The output i get is : 16000 15999

so why is the o/p different here ? This is happening only on fedora. On windows and Ubuntu it works fine. ( Both output are 16000)

I tweaked the above code and got the following results :

int main()
{
  double val_d= 6.25e-05;
  cout << (1/val_d) << endl;
  double val_intermediate =  (1/val_d) ;
  cout << val_intermediate << endl;
  unsigned int val_ui = (unsigned int ) val_intermediate;
  cout << val_ui << endl;

}

NEW OUTPUT is 16000 16000 16000

解决方案

When the source text "6.25e-05" is interpreted as a decimal numeral and converted to double, it is not exactly representable, because floating-point values have limited precision, and each bit has a value that is a power of two, not a decimal digit. The IEEE 754 double-precision value that is nearest to 6.25e-5 is 6.25000000000000013010426069826053208089433610439300537109375e-05, or, in hexadecimal floating-point, 0x1.0624dd2f1a9fcp-14.

When the reciprocal of this is taken, the exact mathematical result is again not exactly representable, so it must be rounded again. The nearest double-precision value is 16000 or 0x1.f4p+13.

The C++ standard allows implementations to evaluate floating-point expressions with more precision than the nominal type requires. Some implementations use extended precision, notably Intel's 80-bit floating-point type, which has a 64-bit significand. (Regular double precision has a 53-bit significand.) In this extended precision, the reciprocal is 0xf.9fffffffffffe89p+10 or 15999.99999999999966693309261245303787291049957275390625.

Obviously, when the extended-precision result is truncated to an integer, the result is 15999.

Rounding the long-double result to double would produce 16000. (You can do this with an explicit cast to double; you do not need to assign the intermediate value to a double object.)

这篇关于从double到unsigned int的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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