正确的 strtod 实现?

Gav*_*and 5 c floating-point strtod

简单的问题:双精度数字 1.15507e-173 的正确位表示是什么?完整问题:如何确定这个数字的正确解析?

背景:我的问题来自这个答案,它显示了来自三个不同解析器的两种不同的位表示,即

0x1c06dace8bda0ee0
Run Code Online (Sandbox Code Playgroud)

0x1c06dace8bda0edf
Run Code Online (Sandbox Code Playgroud)

我想知道哪个解析器做对了。

C99 规范的更新第 6.4.4.2 节说对于 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."
Run Code Online (Sandbox Code Playgroud)

这意味着解析的数字不需要是最近的,甚至不需要是两个相邻的可表示数字之一。7.20.1.3 中的相同规范表示 strtod() 的行为方式与内置解析器基本相同。感谢指出这一点的回答者。

另请参阅对类似问题的回答此博客

Art*_*cto 1

:= num1 = ImportString["\.1c\.06\.da\.ce\.8b\.da\.0e\.e0", "Real64", ByteOrdering->1] // 首先;
:= num2 = ImportString["\.1c\.06\.da\.ce\.8b\.da\.0e\.df", "Real64", ByteOrdering->1] // 首先;
:= SetPrecision[num1, Infinity]-numOr //N
:= numOr = SetPrecision[1.15507, 无穷大] * 10^-173;
             -190
= -6.65645 10
:= SetPrecision[num2, Infinity]-numOr //N
             -189
= -2.46118 10

鉴于两者都偏离同一侧,因此正确的表示是第一个。