从double到unsigned int的转换。

时间:2022-12-21 05:37:09

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

我遇到了从Double转换为int的问题,我尝试运行以下代码:

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

我得到的输出是: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)

那么为什么这里的o/p不同呢?这只发生在fedora上。在windows和Ubuntu上运行良好。(产量均为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

新的产量是16000 16000 000。

3 个解决方案

#1


15  

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.

当源文本“6.25e-05”被解释为十进制数字并转换为双精度时,它就不能被准确地表示出来,因为浮点值的精度是有限的,并且每个位的值的幂是2,而不是小数。ieee754的双精度值,最接近6.25e-5是6.25000000000000013010426069826053208089433610439300537109375e-05,或在十六进制浮点数,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.

当取它的倒数时,精确的数学结果又不能被精确地表示出来,所以它必须再次四舍五入。最近的双精度值是16000或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.

c++标准允许实现以更精确的方式评估浮点表达式。一些实现使用了扩展精度,特别是Intel的80位浮点类型,它具有64位的意义。(常规双精度具有53位的意义。)在这个扩展的精度中,倒数是0xf。9 fffffffffffe89p + 10到15999.99999999999966693309261245303787291049957275390625。

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

显然,当扩展精度结果被截断为整数时,结果是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.)

将长期的两倍结果四舍五入到16000。(可以使用显式转换为double;您不需要将中间值分配给一个double对象)。

#2


6  

difference in rounding.

不同的舍入。

  1. (1/val_d) - double is rounded to the nearest possible number that can be represented with double precision; (ex.: 3.6999999999999999 == 3.7)
  2. (1/val_d) - double被四舍五入到可以用双精度表示的最接近的数字;(例:3.6999999999999999 = = 3.7)
  3. (unsigned int ) (1/val_d) - when casting to int decimal part is truncated, that results on rounding down (ex.: int(3.6) == 3
  4. (无符号整型)(1/val_d) -当转换为整数小数部分时,将被截断,这将导致四舍五入(例:int(3.6) = 3

#3


1  

Converting a floating-point value to an integral value removes the fractional part, provided the result can be represented in the integral type (i.e. the value isn't too large to fit). Inserting into a stream rounds the value, which can produce a different result.

将浮点值转换为整数值可以去掉小数部分,前提是结果可以用整型表示(也就是说,这个值不太大,不太适合)。插入到流中的值将循环,这会产生不同的结果。

#1


15  

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.

当源文本“6.25e-05”被解释为十进制数字并转换为双精度时,它就不能被准确地表示出来,因为浮点值的精度是有限的,并且每个位的值的幂是2,而不是小数。ieee754的双精度值,最接近6.25e-5是6.25000000000000013010426069826053208089433610439300537109375e-05,或在十六进制浮点数,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.

当取它的倒数时,精确的数学结果又不能被精确地表示出来,所以它必须再次四舍五入。最近的双精度值是16000或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.

c++标准允许实现以更精确的方式评估浮点表达式。一些实现使用了扩展精度,特别是Intel的80位浮点类型,它具有64位的意义。(常规双精度具有53位的意义。)在这个扩展的精度中,倒数是0xf。9 fffffffffffe89p + 10到15999.99999999999966693309261245303787291049957275390625。

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

显然,当扩展精度结果被截断为整数时,结果是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.)

将长期的两倍结果四舍五入到16000。(可以使用显式转换为double;您不需要将中间值分配给一个double对象)。

#2


6  

difference in rounding.

不同的舍入。

  1. (1/val_d) - double is rounded to the nearest possible number that can be represented with double precision; (ex.: 3.6999999999999999 == 3.7)
  2. (1/val_d) - double被四舍五入到可以用双精度表示的最接近的数字;(例:3.6999999999999999 = = 3.7)
  3. (unsigned int ) (1/val_d) - when casting to int decimal part is truncated, that results on rounding down (ex.: int(3.6) == 3
  4. (无符号整型)(1/val_d) -当转换为整数小数部分时,将被截断,这将导致四舍五入(例:int(3.6) = 3

#3


1  

Converting a floating-point value to an integral value removes the fractional part, provided the result can be represented in the integral type (i.e. the value isn't too large to fit). Inserting into a stream rounds the value, which can produce a different result.

将浮点值转换为整数值可以去掉小数部分,前提是结果可以用整型表示(也就是说,这个值不太大,不太适合)。插入到流中的值将循环,这会产生不同的结果。