正确的 strtod 实现?

发布于 2024-11-28 04:19:46 字数 1118 浏览 2 评论 0原文

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

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

0x1c06dace8bda0ee0

0x1c06dace8bda0edf

我想知道哪个解析器是正确的。

更新 C99规范说,对于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() 的行为本质上与内置解析器相同。感谢指出这一点的回答者。

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

Simple question: what is the correct bit-representation of the number 1.15507e-173, in double precision?
Full question: how does one determine the correct parsing of this number?

Background: my question follows from this answer which shows two different bit-representations from three different parsers, namely

0x1c06dace8bda0ee0

and

0x1c06dace8bda0edf

and I'm wondering which parser has got it right.

Update Section 6.4.4.2 of the C99 specification says that for the C parser,

"...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."

This implies that the parsed number need not be the nearest, nor even one of the two adjacent representable numbers. The same spec in 7.20.1.3 says that strtod() behaves essentially the same way as the built-in parser. Thanks to the answerers who pointed this out.

Also see this answer to a similar question, and this blog.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

§对你不离不弃 2024-12-05 04:19:46
:= 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

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

:= 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

Given that both deviate for the same side, it follows that the correct representation is the first one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文