简体   繁体   English

当最后一个十进制不是5时,如何从二进制转换为IEEE754?

[英]How do you convert from binary to IEEE754 when the last decimal isn't 5?

Everything I can find on this says to simply multiply by 2 until the decimal resolves to zero, but this only works if the last decimal is 5. 我所能找到的所有内容都是简单地乘以2直到小数变为零,但这仅在最后一个小数为5时才有效。

In my particular case the number to convert is 98765.4321, how would I convert this (or any other decimal that doesn't resolve) to IEEE754? 在我的特殊情况下,要转换的数字是98765.4321,我如何将其(或任何其他无法解析的小数)转换为IEEE754?

I will assume the question is as follows: We are given a number specified as a sequence of decimal digits that possibly includes a decimal fraction, and possibly makes use of scientific notation. 我将假设问题如下:我们给了一个指定为十进制数字序列的数字,该数字可能包含小数,并可能使用科学计数法。 How do we correctly convert this number into one of the binary floating-point formats specified by the IEEE 754 floating-point standard, ie binary16 (half precision), binary32 (single precision), binary64 (double precision), or binary128 (quadruple precision)? 我们如何正确地将此数字转换为IEEE 754浮点标准指定的二进制浮点格式之一,即, binary16 (半精度), binary32 (单精度), binary64 (双精度)或binary128 (四精度) )?

As you noted, most decimal numbers cannot be represented exactly in a binary floating-point format. 如您所述,大多数十进制数字不能完全以二进制浮点格式表示。 That means we need to chose one of the IEEE-754 rounding mode that should be used to determine the final result: Round towards positive infinity ("up"), round towards negative infinity ("down"), round towards zero (truncate), or round towards-nearest-or-even ("nearest"). 这意味着我们需要选择一种IEEE-754舍入模式用于确定最终结果:朝正无穷大(“向上”)舍入,朝负无穷大(“向下”)舍入,向零舍入(截断) ,或朝着最近或偶数(“最近”)取整。 Decimal-to-binary conversion typically uses the last mode listed, round towards-nearest-or-even, as this minimizes overall error in the conversion. 十进制到二进制的转换通常使用列出的最后一个模式,朝着最近或偶数取整,因为这样可以最大程度地减少转换中的总体误差。

Conceptually, our task is simple. 从概念上讲,我们的任务很简单。 Carry out the conversion process until we have generated enough bits to make a correct rounding decision. 进行转换过程,直到我们生成足够的位以做出正确的舍入决策为止。 Clearly, we will often need more bits than provided by the target format. 显然,我们经常需要比目标格式提供的位数更多的位。 However, we cannot tell a-priory exactly how many bits we will need, as some hard to round cases will generate results very close to a tie-case. 但是,我们无法准确地告诉a-priory我们需要多少位,因为一些难以取舍的情况将产生非常接近平局的结果。 The take-home message is that some parts of our algorithm will require the use of some sort of extended precision (or multi-precision) arithmetic, and we need to develop a criterion for determining when we have generated enough bits for correct rounding. 要注意的是,我们算法的某些部分将需要使用某种扩展的精度(或多精度)算法,并且我们需要开发一个标准来确定何时我们生成了足够的位以进行正确的舍入。

The fundamental algorithms for correct conversions were developed over a couple of decades in the past century, and are described in the following publications: 正确转换的基本算法是在过去一个世纪的几十年中开发的,并在以下出版物中进行了介绍:

David W. Matula, "In-and-out conversions". David W. Matula,“内外转换”。 Communications of the ACM , Vol. 通信ACM ,卷。 11, No. 1 (Jan. 1968), pp. 47-50 11,No.1(1968年1月),第47-50页

David W. Matula, "A Formalization of Floating-Point Numeric Base Conversion". David W. Matula,“浮点数值基数转换的形式化”。 IEEE Transactions on Computers , Vol 10, No. 8 (Aug. 1970), pp. 681-692 ( online ) IEEE Transactions on Computers ,第10卷,第8号(1970年8月),第681-692页( 在线

William D. Clinger, "How to Read Floating Point Numbers Accurately". 威廉·克林格(William D. Clinger),“如何准确读取浮点数”。 SIGPLAN Notices , Vol. SIGPLAN通告 ,卷。 25, No. 6 (June 1990), pp. 92-101 ( online ) 25,No.6(1990年6月),第92-101页( 在线

David M. Gay, "Correctly rounded binary-decimal and decimal-binary conversions". David M. Gay,“正确舍入二进制十进制和十进制二进制转换”。 Technical Report 90--10, AT&T Bell Laboraties, November 1990. ( online ) 技术报告90--10,AT&T贝尔实验室,1990年11月。( 在线

A fresh look at this research area is provided by the following publications: 以下出版物对本研究领域进行了重新审视:

Michel Hack, "On Intermediate Precision Required for Correctly-Rounding Decimal-to-Binary Floating-Point Conversion." Michel Hack,“关于正确舍入十进制到二进制浮点转换所需的中间精度”。 In Proceedings of Real Numbers and Computers (RNC'6) , Nov. 2004, pp. 113-133 ( online ) 实数与计算机学报(RNC'6) ,2004年11月,第113-133页( 在线

Aubrey Jaffer, "Easy Accurate Reading and Writing of Floating-Point Numbers". Aubrey Jaffer,“轻松准确地读取和写入浮点数”。 arXiv:1310.8121, draft v6 (Jan. 2015), ( online ) arXiv:1310.8121,草案v6(2015年1月),( 在线

Although the fundamental algorithms have been around for twenty-five years, they are of considerable complexity, and the "devil is in the details". 尽管基本算法已经问世了25年,但是它们却具有相当大的复杂性,“细节决定成败”。 Correct implementations of decimal-to-brinary conversions continue to prove elusive. 十进制到二进制转换的正确实现仍然难以捉摸。 Over the past 5 years, Rick Regan's blog "Exploring Binary" has chronicled a number of defects in the decimal-to-binary conversion functionality of widely used software, such as Microsoft Visual C/C++ , glibc , and PHP , where the last item would cause an infinite loop that might be exploited for denial-of-service attacks. 在过去的5年中,Rick Regan的博客“ Exploring Binary”已经记录了广泛使用的软件(如Microsoft Visual C / C ++glibcPHP)的十进制到二进制转换功能中的许多缺陷,最后一项会导致无限循环,可能被利用来进行拒绝服务攻击。

A paper by Vern Paxson and William Kahan addresses the issue of hard-to-round cases in decimal-to-binary conversion, and gives some examples that demonstrate how many additional bits beyond target precision may be required for correct rounding: Vern Paxson和William Kahan的论文解决了十进制到二进制转换中难以舍入的情况,并提供了一些示例来说明正确舍入可能需要超出目标精度的额外位数:

V. Paxson and W. Kahan, "A Program for Testing IEEE Decimal–Binary Conversion". V. Paxson和W. Kahan,“用于测试IEEE十进制-二进制转换的程序”。 Manuscript, May 1991 ( online ) 原稿,1991年5月( 在线

Additional hard-to-round cases for IEEE-754 binary64 were listed in a 1996 posting to the newsgroup comp.arch.arithmetic by Fred Tydeman. 附加难圆的情况下为IEEE-754 binary64在1996年被列入张贴到新闻组comp.arch.arithmetic由Fred Tydeman。

The following paper describes a test framework for testing conversions, however the files containing the test vectors were no longer accessible online the last time I checked: 以下论文描述了用于测试转换的测试框架,但是包含测试向量的文件在我上次检查时不再可以在线访问:

Brigitte Verdonk, Annie Cuyt, and Dennis Verschaeren. Brigitte Verdonk,Annie Cuyt和Dennis Verschaeren。 "A precision-and range-independent tool for testing floating-point arithmetic II: conversions." “用于测试浮点运算II:转换的与精度和范围无关的工具。” ACM Transactions on Mathematical Software , Vol. ACM Transactions on Mathematical Software ,第一卷。 27, No. 1 (Mar. 2001), pp. 119-140. 27,No.1(2001年3月),第119-140页。 ( draft online ) 在线草稿

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM