如何计算浮点型精度,这有意义吗? [英] How to calculate float type precision and does it make sense?

查看:135
本文介绍了如何计算浮点型精度,这有意义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解float类型的精度时遇到问题。
msdn 写入,精度从6到9位数字。但我注意到精度取决于数字的大小:

  float smallNumber = 1.0000001f ; 
Console.WriteLine(smallNumber); // 1.0000001

bigNumber = 100000001f;
Console.WriteLine(bigNumber); // 100000000

smallNumber比big更为精确,我理解IEEE754,但是我不了解MSDN如何计算精度,这是否有意义?



此外,您还可以使用浮点格式的数字表示法此处。请在您输入的输入中输入100000000的值,然后单击右侧的 +1。然后将输入值更改为1,然后再次单击 +1。您可能会看到精度上的差异。

解决方案

MSDN文档是荒谬而错误的。


错误的概念。二进制浮点格式的十进制数字没有任何精度,因为它根本没有十进制数字。它代表带符号的数字,固定数量的二进制数字(位)和2的幂的指数。


高端错误。浮点格式可以精确无误地表示许多数字。例如,精确地表示 3。您可以用十进制任意形式写3.0000000000…,并且所有十进制数字都是正确的。另一个示例是1.40129846432481707092372958328991613128026194187651577175706828388979108268586060148663818836212158203125e-45。此数字的十进制有效数字为105,但是 float 格式完全表示该数字(它是2 −149 )。


低端错误。 *当 999999.97从十进制转换为 float 时,结果为1,000,000。因此,甚至连十进制数字都不正确。


不是准确性的度量。因为 float 的有效位数有24位,其最低位的分辨率大约是其最高位的分辨率的2 23 倍。从log 10 2 23 约为6.9的意义上来说,这大约是6.9位数。但这只是告诉我们表示的分辨率(即粗糙度)。当我们将数字转换为 float 格式时,由于四舍五入到最接近的可表示值,因此得到的结果与该数字的区别最多为此分辨率的1/2。因此,转换为 float 的相对误差最多为2 24 的1个部分,在上述意义上,大约相当于7.2位。 / p>

这些数字从何而来?


因此,如果〜6-9位数字不是正确的概念,不是来自数字的实际界限,也不是衡量精度,它是从哪里来的?我们不确定,但是在 float 格式的两个描述中确实出现了6和9。


6是最大的数字 x 可对此进行保证:



  • 如果最多有 x 个有效数字的十进制数字在 float 格式的有限边界,并转换为该格式表示的最接近的值,然后,当结果转换为最多为 x的最接近的十进制数字时有效数字,该转换的结果等于原始数字。


所以说 float 可以保留至少六个十进制数字。但是,正如我们将看到的那样,没有涉及九位数字的边界。


9是保证该值的最小数字 x



  • 如果将任何有限的 float 数字转换为带有 x 位的最接近的十进制数字,则结果将转换为 float 中可表示的最接近的值,该转换的结果等于原始数字。


打个比方,如果 float 是一个容器,则保证可放入其中的最大十进制容器为六位数,而最小的十进制容器为保证将其保留为九位数。 6和9类似于 float 容器的内部和外部尺寸。


假设您有一个块长7.2个单位,并且您在看它在每块1单位长的砖线上的放置情况。如果将块的开始放在砖的开始处,它将延伸7.2砖。但是,其他人会选择它的开始位置,他们可能会在砖头中间开始。然后它将覆盖该砖块的一部分,所有接下来的6块砖,以及最后一块砖的一部分(例如.5 + 6 + .7 = 7.2)。因此,一个7.2单位的块只能保证覆盖6块砖。相反,如果您选择放置7.2单位的块,则可以隐藏8块砖。但是,如果有人选择他们从哪里开始,那么第一个可能只隐蔽.1个单位的区块。然后,您需要再增加7个分数,因此需要9个积木。


这个比喻成立的原因是,2的幂和10的幂相对不规则地间隔开。 2 10 (1024)接近10 3 (1000)。 10是 float 格式中从1024(含)到2048(不含)的数字所使用的指数。因此,从1024到2048的间隔就像是一个块,刚好在100-1000结束并且1000-10,000块开始之后就放置了。


但是请注意,此属性涉及9位数字外部衡量标准–它不是 float 可以执行的功能或它不能提供的服务。这是 float 所需要的(如果要以十进制格式保存),而不是它所提供的。因此,浮点数可以存储多少位数没有限制。


进一步阅读


为更好地理解浮点算法,请考虑研究 IEEE-754浮点算术标准或类似 Jean-Michel Muller写的浮点算法手册 et al


I have a problem understanding the precision of float type. The msdn writes that precision from 6 to 9 digits. But I note that precision depends from on the size of the number:

  float smallNumber = 1.0000001f;
  Console.WriteLine(smallNumber); // 1.0000001

  bigNumber = 100000001f;
  Console.WriteLine(bigNumber); // 100000000

The smallNumber is more precise than big, I understand IEEE754, but I don't understand how MSDN calculate precision, and does it make sense?

Also, you can play with the representation of numbers in float format here. Please write 100000000 value in "You entered" input and click "+1" on the right. Then change the input's value to 1, and click "+1" again. You may see the difference in precision.

解决方案

The MSDN documentation is nonsensical and wrong.

Bad concept. Binary-floating-point format does not have any precision in decimal digits because it has no decimal digits at all. It represents numbers with a sign, a fixed number of binary digits (bits), and an exponent for a power of two.

Wrong on the high end. The floating-point format represents many numbers exactly, with infinite precision. For example, "3" is represented exactly. You can write it in decimal arbitrarily far, 3.0000000000…, and all of the decimal digits will be correct. Another example is 1.40129846432481707092372958328991613128026194187651577175706828388979108268586060148663818836212158203125e-45. This number has 105 significant digits in decimal, but the float format represents it exactly (it is 2−149).

Wrong on the low end.* When "999999.97" is converted from decimal to float, the result is 1,000,000. So not even one decimal digit is correct.

Not a measure of accuracy. Because the float significand has 24 bits, the resolution of its lowest bit is about 223 times finer than the resolution of its highest bit. This is about 6.9 digits in the sense that log10223 is about 6.9. But that just tells us the resolution—the coarseness—of the representation. When we convert a number to the float format, we get a result that differs from the number by at most ½ of this resolution, because we round to the nearest representable value. So a conversion to float has a relative error of at most 1 part in 224, which corresponds to about 7.2 digits in the above sense.

Where do these numbers came from?

So, if "~6-9 digits" is not a correct concept, does not come from actual bounds on the digits, and does not measure accuracy, where does it come from? We cannot be sure, but 6 and 9 do appear in two descriptions of the float format.

6 is the largest number x for which this is guaranteed:

  • If any decimal numeral with at most x significant digits is within the finite bounds of the float format and is converted to the nearest value represented in the format, then, when the result is converted to the nearest decimal numeral with at most x significant digits, the result of that conversion equals the original number.

So it is reasonable to say float can preserve at least six decimal digits. However, as we will see, there is no bound involving nine digits.

9 is the smallest number x that guarantees this:

  • If any finite float number is converted to the nearest decimal numeral with x digits, then, when the result is converted to the nearest value representable in float, the result of that conversion equals the original number.

As an analogy, if float is a container, then the largest "decimal container" guaranteed to fit inside it is six digits, and the smallest "decimal container" guaranteed to hold it is nine digits. 6 and 9 are akin to interior and exterior measurements of the float container.

Suppose you had a block 7.2 units long, and you were looking at its placement on a line of bricks each 1 unit long. If you put the start of the block at the start of a brick, it will extend 7.2 bricks. However, somebody else chooses where it starts, they might start it in the middle of a brick. Then it would cover part of that brick, all of the next 6 bricks, and and part of the last brick (e.g., .5 + 6 + .7 = 7.2). So a 7.2-unit block is only guaranteed to cover 6 bricks. Conversely, 8 bricks can covert the 7.2-unit block if you choose where they are placed. But if somebody else chooses where they start, the first might covert just .1 units of the block. Then you need 7 more and another fraction, so 9 bricks are needed.

The reason this analogy holds is that powers of two and powers of 10 are irregularly spaced relative to each other. 210 (1024) is near 103 (1000). 10 is the exponent used in the float format for numbers from 1024 (inclusive) to 2048 (exclusive). So this interval from 1024 to 2048 is like a block that has been placed just after the 100-1000 ends and the 1000-10,000 block starts.

But note that this property involving 9 digits is the exterior measurement—it is not a capability that float can perform or a service that it can provide. It is something that float needs (if it is to be held in a decimal format), not something it provides. So it is not a bound on how many digits a float can store.

Further Reading

For better understanding of floating-point arithmetic, consider studying the IEEE-754 Standard for Floating-Point Arithmetic or a good textbook like Handbook of Floating-Point Arithmetic by Jean-Michel Muller et al.

这篇关于如何计算浮点型精度,这有意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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