我在理解浮点类型的精度时遇到了问题.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.40129846432481707092372958328991613128026194187651577175706828388979108268586060148663818836212158203125•10−45.这个数字在十进制中有 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:
float
格式的正常指数范围内并转换为格式中表示的最接近的值,则,当结果被转换为最接近的十进制数字时,最多有 x 个有效数字,该转换的结果等于原始数字.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 中表示的最接近的值时
,该转换的结果等于原始数字.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 块砖,以及最后一块砖的一部分(例如,0.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 位数字的属性是 exterior 度量——它不是 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.
这篇关于如何计算浮点类型精度,它是否有意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!