将 IEEE 754 双精度数转换为字符串的算法?
许多使用 IEEE 754 双精度数的编程语言都提供了将这些双精度数转换为字符串的库函数。比如C有sprintf
,C++有stringstream
,Java有Double.toString
等。
在内部,这些函数是如何实现的呢?也就是说,考虑到它们通常受到程序员选择的精度限制,他们使用什么算法将双精度数转换为字符串表示形式?
谢谢!
Many programming languages that use IEEE 754 doubles provide a library function to convert those doubles to strings. For example, C has sprintf
, C++ has stringstream
, Java has Double.toString
, etc.
Internally, how are these functions implemented? That is, what algorithm(s) are they using to convert the double into a string representation, given that they are often subject to programmer-chosen precision limitations?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
各种软件环境用于将浮点数转换为字符串表示形式的代码通常基于以下出版物(Steele 和 White 的著作尤其被频繁引用):
Jerome T. Coonen:“An Implementing Guide to a Proproved Standard for浮点运算。” 计算机,卷。 13,第 1 期,1980 年 1 月,第 68-79 页
盖伊。 L. Steele Jr. 和 JL White:“如何准确打印浮点数”。在 ACM SIGPLAN '90 编程语言设计和实现会议 的会议记录中,纽约怀特普莱恩斯,1990 年 6 月,第 112-126 页
David M. Gay:“正确舍入的二进制十进制和十进制-二进制转换。”技术报告 90-10,AT&T 贝尔实验室,1990 年 11 月。
一些相关后续工作:
Robert G. Burger 和 R. Kent Dybvig:“快速准确地打印浮点数。” ACM SIGPLAN 1996 年编程语言设计和实现会议,美国宾夕法尼亚州费城,1996 年 5 月,第 108-116 页
Guy L. Steele Jr. 和 Jon L. White:“回顾:如何准确地打印浮点数。” ACM SIGPLAN 通知,卷。 39,第 4 期,2004 年 4 月,第 372–389 页
Florian Loitsch:“用整数快速准确地打印浮点数。” 2010 ACM SIGPLAN 编程语言设计和实现会议会议记录,加拿大安大略省多伦多,2010 年 6 月,第 233-243 页
Marc Andrysco、Ranjit Jhala 和 Sorin Lerner:“打印浮点数字:一种更快、始终正确的方法。” ACM SIGPLAN 通知,卷。 51,第 1 期,2016 年 1 月,第 555-567 页
Ulf Adams:“Ryū:快速浮点到字符串转换。” ACM SIGPLAN 通知,卷。 53,第4期,2018年4月,第270-282页
The code used by various software environments to convert floating-point numbers to string representations is typically based on the following publications (the work by Steele and White is particularly frequently cited):
Jerome T. Coonen: "An Implementation Guide to a Proposed Standard for Floating-Point Arithmetic." Computer, Vol. 13, No. 1, January 1980, pp. 68-79
Guy. L. Steele Jr. and J. L. White: "How to print floating-point numbers accurately". In proceedings of ACM SIGPLAN '90 Conference on Programming Language Design and Implementation, White Plains, New York, June 1990, pp. 112-126
David M. Gay: "Correctly rounded binary-decimal and decimal-binary conversions." Technical Report 90-10, AT&T Bell Laboratories, November 1990.
Some relevant followup work:
Robert G. Burger and R. Kent Dybvig: "Printing floating-point numbers quickly and accurately." In proceedings of ACM SIGPLAN 1996 conference on Programming Language Design and Implementation, Philadelphia, PA, USA, May 1996, pp. 108-116
Guy L. Steele Jr. and Jon L. White: "Retrospective: How to print floating-point numbers accurately." ACM SIGPLAN Notices, Vol. 39, No. 4, April 2004, pp. 372–389
Florian Loitsch: "Printing floating-point numbers quickly and accurately with integers." In proceedings of 2010 ACM SIGPLAN Conference on Programming Language Design and Implementation, Toronto, ON, Canada, June 2010, pp. 233-243
Marc Andrysco, Ranjit Jhala, and Sorin Lerner: "Printing floating-point numbers: a faster, always correct method." ACM SIGPLAN Notices, Vol. 51, No. 1, January 2016, pp. 555-567
Ulf Adams: "Ryū: fast float-to-string conversion." ACM SIGPLAN Notices, Vol. 53, No. 4, April 2018, pp. 270-282
我相信您正在寻找 快速准确地打印浮点数
我在另一篇文章中找到了该链接:此处。
I believe you are looking for Printing Floating-Point Numbers Quickly and Accurately
I found that link on another post: here.
请参阅 Ryan Juckett 的打印浮点数 (2014),描述了浮点到字符串转换的历史和实现。在这篇由四部分组成的文章中,Ryan 还提供了基于 Steele 和 White (1990) 的 Dragon4 的 C++ 实现,这是一种将浮点格式的二进制数转换为字符串格式的十进制数的高效算法。
您还可以查看 Ryan's Dragon4 for Numpy 的 C 实现 此处,并在 Python/Numpy 1.14 中使用它
format_float_positional
和format_float_scientific
函数。2018 年,发布了一个算法/库 Ryu,它绑定了许多现代编程语言(C、Java、C++、C#、Scala、Rust、Julia、Go...)
See Ryan Juckett's Printing Floating-Point Numbers (2014), which describes history and implementations of floating-point to string conversions. In this four-part post, Ryan also provides a C++ implementation of Dragon4 based on Steele and White (1990), which is an efficient algorithm to convert a binary number in floating point format to a decimal number in string format.
You can also see a C implementation of Ryan's Dragon4 for Numpy here, and use it within Python/Numpy 1.14
format_float_positional
andformat_float_scientific
functions.In 2018, an algorithm/library Ryu was published, with bindings in many modern programming languages (C, Java, C++, C#, Scala, Rust, Julia, Go, ...)
对于您引用的大多数示例语言,可以在线免费查阅源代码,因为它们是开源的。
对于 Java,类 java.lang.Double 委托这项工作到sun.misc.FloatingDecimal。查看它的构造函数和 toJavaFormatString() 方法。
对于 C,glibc 始终是一个很好的例子,我们 看到浮点输出位于其自己的源文件中。
For most example languages you quote the source is freely consultable online as they're available in open source.
For Java, the class java.lang.Double delegates this work to sun.misc.FloatingDecimal. Check out its constructor and toJavaFormatString() method.
For C, glibc is always a good example, and there we see that floating point output is located in its own source file.