正确的 strtod 实现?
简单的问题:双精度数字 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
鉴于两者都偏离同一侧,因此正确的表示是第一个。
Given that both deviate for the same side, it follows that the correct representation is the first one.