浮点数如何保证7位数精度? [英] How does float guarantee 7 digit precision?

查看:83
本文介绍了浮点数如何保证7位数精度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道单精度浮点数的符号为1位,指数为8位,尾数为23位.

我可以理解7位整数适合23位尾数并且不会丢失精度,但无法理解像1234567000000000这样的数字在没有宽松的"1,2,3,4,5,6,7"位的情况下如何适合,这背后的数学是什么?

解决方案

IEEE-754基本的32位二进制浮点格式仅保证六个有效的十进制数字将在往返转换中幸存下来,而不是七个.具体来说:如果您使用正确的从最接近的四舍五入四舍五入方法将精确地用十进制十进制数字乘以十的幂表示的数字转换为二进制格式,并且没有上溢或下溢,然后转换回最近的值.可以用六个十进制数字乘以十的幂表示的数字,结果将是原始数字.

通常,当十进制数字转换为二进制浮点数时,用十进制写入时结果可能没有相同的数字.您的示例1234567000000000转换为1234567008616448,但是我们可能会发现123456000…转换为123455900…的情况,因此原始数字之一是不同的.但是二进制格式提供的精度使得转换为二进制格式的结果总是非常接近原始值,以至于差值永远不会超过第六位数字的位置值的一半.例如,转换123456000…始终会产生123455500…和123456500…之间的结果.由于第一次转换的结果总是在这样的时间间隔内,因此将其转换回六舍五入的十进制数字始终会产生原始数字.

为了保证这一点,格式的精度必须与999999中的一个精度一样高.这是因为二进制格式的数字之间的间距是如此之细,以至于在9999985的间隔中至少有一个…到9999995…,因此从十进制转换为二进制的结果可以产生与原始结果足够接近的结果,从而可以转换回原始值.有效位数为24位(隐含一个,隐含23个),精度为2 23 的至少一部分,即8,388,608.(有效位数最多可以达到16,777,215,但是我们对此没有选择,必须对其进行归一化,使其保持在8,388,608到16,777,215之间.)

8,388,608中的一位好于999,999中的一位,因此可以保证六位数字,但并不好于9,999,999中的一位,因此不能保证七位数字.

As I know Single-precision floating-point number has 1 bit for sign, 8 bits for exponent and 23 bits for mantissa.

I can understand that 7 digit integers fit 23 bit mantissa and don't loose precision but can't understand how a number like 1234567000000000 fits without loose "1,2,3,4,5,6,7" digits, what is the math behind this?

解决方案

The IEEE-754 basic 32-bit binary floating-point format only guarantees that six significant decimal digits will survive a round-trip conversion, not seven. Specifically: If you convert a number that is exactly represented with six decimal digits multiplied by a power of ten to the binary format using correct round-to-nearest rounding, and there is no overflow or underflow, and then you convert back to the nearest number representable with six decimal digits times a power of ten, the result will be the original number.

Generally, when a decimal numeral is converted to binary floating-point, the result might not have the same digits when written in decimal. Your example, 1234567000000000, converts to 1234567008616448, but we might find some case where 123456000… converts to 123455900…, so one of the original digits is different. But the precision supplied by the binary format is such that the result of converting to the binary format is always so near the original value that the difference is never more than half the position value of the sixth digit. For example, converting 123456000… will always produce a result between 123455500… and 123456500… Since the result of the first conversion is always within such an interval, converting it back to six decimal digits, with rounding, always produces the original number.

In order to make this guarantee, the precision of the format must be as fine as one part in 999999. That is because then the numbers in the binary format are so finely spaced that there is at least one in the interval from 9999985… to 9999995…, so the result of converting from decimal to binary can produce a result close enough to the original that converting back produces the original value. With 24 bits in the significand (one implied, 23 explicit), the precision is at least one part in 223, which is 8,388,608. (The significand can go up to 16,777,215, but we do not have a choice about it—it must be normalized, which keeps it in the range from 8,388,608 to 16,777,215.)

One part in 8,388,608 is better than one in 999,999, so six digits can be guaranteed, but it is not better than one in 9,999,999, so seven digits are not guaranteed.

这篇关于浮点数如何保证7位数精度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆