这是code:
typedef std::numeric_limits<float> fl;
int main()
{
std::cout.precision(100);
float f1 = 9999978e3;
std::cout << f1 << " (9999978e3 decimal number stored as binary)" << std::endl;
float f2 = 9999979e3;
std::cout << f2 << " (9999979e3 decimal number stored as binary)" << std::endl;
float f3 = 9999990e3;
std::cout << f3 << " (9999990e3 decimal number stored as binary)" << std::endl;
std::cout << "-----^" << std::endl;
}
打印:
9999978496 (9999978e3 decimal number stored as binary)
9999978496 (9999979e3 decimal number stored as binary)
9999989760 (9999990e3 decimal number stored as binary)
-----^
存储为二进制的十进制数正确保留第 6 位数字9999978e3
and 9999979e3
(这是7),但第 6 位数字9999990e3
is 8并不是9.
浮点精度不应该始终保证前 6 位数字吗?
是的,如果我圆89 I got 9,但这不一样;这仅在“视觉上”有效/有意义。
稍后(处理数字),当我对该值应用数学时,它将在xxxxx8
号码(9999989760),而不是在xxxxx9
。 +1 在这个幅度。
不是以你认为有保证的方式,不。
通过提供一个反例,对于 IEEE754 单精度浮点,最接近的数字
9999990000
is
9999989760
What is保证的是您的号码和float
,当两者四舍五入到六位有效数字时,将是相同的。这就是价值FLT_DIG
在您的平台上,假设它实现了 IEEE754。例如。最近的float
号码至9999979000
is 9999978496
.
See http://www.exploringbinary.com/floating-point-converter/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)