简单的问题:双精度数字 1.15507e-173 的正确位表示是什么?
完整的问题:如何确定这个数字的正确解析?
背景:我的问题来自这个答案 https://stackoverflow.com/questions/1994658/locale-independent-strtod-implementation/6924728#6924728它显示了来自三个不同解析器的两种不同的位表示,即
0x1c06dace8bda0ee0
and
0x1c06dace8bda0edf
我想知道哪个解析器是正确的。
Update第 6.4.4.2 节C99规范 http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf对于 C 解析器来说,
"...the result is either the nearest representable value, or the larger
or smaller representable value immediately adjacent to the nearest
representable value, chosen in an implementation-defined manner."
这意味着解析的数字不必是最近的,甚至也不必是两个相邻可表示数字之一。 7.20.1.3 中的相同规范表示 strtod() 的行为本质上与内置解析器相同。感谢指出这一点的回答者。
另请参阅这个答案 https://stackoverflow.com/questions/2499329/strtod-and-sprintf-inconsistency-under-gcc-and-msvc/2499570#2499570对于类似的问题,并且这个博客 http://www.exploringbinary.com/topics/#correctly-rounded-decimal-to-floating-point.
:= num1 = ImportString["\.1c\.06\.da\.ce\.8b\.da\.0e\.e0", "Real64", ByteOrdering->1] // First;
:= num2 = ImportString["\.1c\.06\.da\.ce\.8b\.da\.0e\.df", "Real64", ByteOrdering->1] // First;
:= SetPrecision[num1, Infinity]-numOr //N
:= numOr = SetPrecision[1.15507, Infinity] * 10^-173;
-190
= -6.65645 10
:= SetPrecision[num2, Infinity]-numOr //N
-189
= -2.46118 10
鉴于两者都偏离同一侧,因此正确的表示是第一个。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)