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 remainas it is ?
从double转换为int可能会删除小数部分,但整数部分应保留为何?
The output i get is : 1600015999
我得到的输出是:1600015999
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 is160001600016000
新产品是160001600016000
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”被解释为十进制数字并转换为double时,它不能完全表示,因为浮点值的精度有限,并且每个位的值都是2的幂,而不是十进制数字。最接近6.25e-5的IEEE 754双精度值是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.9fffffffffffe89p + 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.)
将long-double结果舍入为double将产生16000.(您可以使用显式转换来加倍;您不需要将中间值分配给double对象。)
#2
6
difference in rounding.
四舍五入的差异。
- (1/val_d) - double is rounded to the nearest possible number thatcan be represented with double precision; (ex.: 3.6999999999999999== 3.7)
- (unsigned int ) (1/val_d) - when casting to int decimal part istruncated, that results on rounding down (ex.: int(3.6) == 3
(1 / val_d) - double被四舍五入到可以用双精度表示的最接近的可能数字; (例如:3.6999999999999999 == 3.7)
(unsigned int)(1 / val_d) - 当转换为int decimal part istruncated时,会导致向下舍入(例如: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”被解释为十进制数字并转换为double时,它不能完全表示,因为浮点值的精度有限,并且每个位的值都是2的幂,而不是十进制数字。最接近6.25e-5的IEEE 754双精度值是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.9fffffffffffe89p + 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.)
将long-double结果舍入为double将产生16000.(您可以使用显式转换来加倍;您不需要将中间值分配给double对象。)
#2
6
difference in rounding.
四舍五入的差异。
- (1/val_d) - double is rounded to the nearest possible number thatcan be represented with double precision; (ex.: 3.6999999999999999== 3.7)
- (unsigned int ) (1/val_d) - when casting to int decimal part istruncated, that results on rounding down (ex.: int(3.6) == 3
(1 / val_d) - double被四舍五入到可以用双精度表示的最接近的可能数字; (例如:3.6999999999999999 == 3.7)
(unsigned int)(1 / val_d) - 当转换为int decimal part istruncated时,会导致向下舍入(例如: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.
将浮点值转换为整数值会删除小数部分,前提是结果可以用整数类型表示(即值不太大而不适合)。插入流会舍入值,这会产生不同的结果。