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

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

问题描述

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

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

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

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

此外,您可以在 此处.请在您输入"输入中输入 100000000 值,然后单击右侧的+1".然后将输入的值更改为 1,并再次单击+1".您可能会看到精度上的差异.

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.

推荐答案

MSDN 文档是无意义的和错误的.

The MSDN documentation is nonsensical and wrong.

糟糕的概念.二进制浮点格式在十进制数字中没有任何精度,因为它根本没有十进制数字.它用一个符号、固定数量的二进制数字(位)和一个 2 的幂的指数来表示数字.

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.

错误的高端.浮点格式精确地表示许多数字,具有无限的精度.例如,3"是精确表示的.你可以用十进制任意远写,3.0000000000...,所有的十进制数字都会正确.另一个例子是 1.401298464324817070923729583289916131280261941876515771757068283889791082685860601486638<1883s>5-up1208386638<18836s>12082866638194186s.这个数字有 105 位十进制有效数字,但 float 格式准确地表示它(它是 2−149).

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.40129846432481707092372958328991613128026194187651577175706828388979108268586060148663818836212158203125•10−45. This number has 105 significant digits in decimal, but the float format represents it exactly (it is 2−149).

低端错误.当999999.97"从十进制转换为float时,结果是1,000,000.所以连一位十进制数字都不正确.

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.

不是准确度的衡量标准.因为float有效位有24位,其最低位的分辨率大约是223倍比其最高位的分辨率.从 log10223 约为 6.9 的意义上说,这约为 6.9 位.但这只是告诉我们表示的分辨率——粗糙程度.当我们将一个数字转换为 float 格式时,我们得到的结果与数字最多相差这个分辨率的 ½,因为我们四舍五入到最接近的可表示值.所以转换为float 的相对误差最多为224 中的1 部分,相当于上述意义上的约7.2 位.如果我们用数字来衡量分辨率,那么我们说分辨率大约是 7.2 位,而不是 6-9 位.

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. If we are using digits to measure resolution, then we say the resolution is about 7.2 digits, not that it is 6-9 digits.

这些数字从何而来?

那么,如果~6-9 位数字"不是一个正确的概念,不是来自数字的实际界限,也不是衡量准确度,它从何而来?我们不能确定,但​​ 6 和 9 确实出现在 float 格式的两种描述中.

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 是保证这一点的最大数 x:

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

  • 如果任何具有至多 x 个有效数字的十进制数字在 float 格式的正常指数范围内并被转换为格式中表示的最接近的值,则, 当结果转换为最接近的十进制数字时,最多 x 个有效数字,转换的结果等于原始数字.
  • If any decimal numeral with at most x significant digits is within the normal exponent 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.

所以说float 可以保留至少六位十进制数字是合理的.然而,正如我们将看到的,没有涉及九位数字的界限.

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 是保证这一点的最小数字 x:

9 is the smallest number x that guarantees this:

  • 如果任何有限float数被转换为最接近的具有x位的十进制数字,那么,当结果被转换为float中可表示的最接近的值时,该转换的结果等于原始数字.
  • 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.

打个比方,如果float是一个容器,那么保证能装进去的最大十进制容器"是六位数,保证能容纳它的最小十进制容器"是九位数.6 和 9 类似于 float 容器的内部和外部测量.

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.

假设您有一个 7.2 个单位长的块,并且您正在查看它在一排每 1 个单位长的砖块上的位置.如果你把方块的起点放在一块砖的起点,它会延伸 7.2 块砖.但是,其他人会选择从哪里开始,他们可能会从砖块中间开始.然后它将覆盖那块砖的一部分,接下来的所有 6 块砖,以及最后一块砖的一部分(例如,.5 + 6 + .7 = 7.2).所以一个 7.2 单元的块只能保证覆盖 6 块砖.相反,如果您选择放置它们的位置,则 8 块砖可以覆盖 7.2 单元的块.但是,如果其他人选择从哪里开始,第一个可能只覆盖块的 0.1 个单位.那么你还需要 7 个和一个分数,所以需要 9 个砖块.

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 cover the 7.2-unit block if you choose where they are placed. But if somebody else chooses where they start, the first might cover just .1 units of the block. Then you need 7 more and another fraction, so 9 bricks are needed.

这个类比成立的原因是,2 的幂和 10 的幂彼此之间的间隔是不规则的.210 (1024) 接近 103 (1000).10 是 float 格式中使用的指数,用于从 1024(含)到 2048(不含)的数字.所以这个从1024到2048的区间就像是在100-1000结束和1000-10000开始之后放置的块.

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.

但请注意,这个涉及 9 位数字的属性是外部度量——它不是 float 可以执行的功能或它可以提供的服务.这是 float 需要的东西(如果它以十进制格式保存),而不是它提供的东西.所以它不是限制 float 可以存储多少位数字.

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.

进一步阅读

为了更好地理解浮点运算,请考虑学习IEEE-754 浮点运算标准 或像 Jean-Michel Muller 的浮点算术手册.

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天全站免登陆