浮点数中的有效数字和精度损失之间有什么关系?
因此,我一直试图通过头脑来理解浮点数中有效位数与相对精度损失之间的关系,但我似乎无法理解它。之前我读过一篇文章,说要执行以下操作:
- 将浮点数设置为值2147483647。你会看到它的值实际上是2147483648
- 从浮点数中减去64,你会看到操作是正确的
- 从浮点数中减去65你会发现你现在实际上有 2147483520,这意味着它实际上减去了 128。
那么为什么这个 128 当有10位有效数字吗?我了解浮点数是如何存储的(1 位用于符号,8 位用于指数,23 位用于尾数),并且了解如果您假设所有整数都会自动在浮点数据结构中找到精确的位置,那么您将如何失去精度,但我不知道不明白128从哪里来。我的直觉告诉我,我走在正确的轨道上,但我希望有人能够为我解决这个问题。
我最初认为可能的浮点数之间的距离是 2 ^ (n-1),其中 n 是有效数字的数量,但这并不成立。
谢谢你!
So I have been trying to wrap by head around the relation between the number of significant digits in a floating point number and the relative loss of precision, but I just can't seem to make sense of it. I was reading an article earlier that said to do the following:
- Set a float to a value of 2147483647. You will see that its value is actually 2147483648
- Subtract 64 from the float and you will see that the operation is correct
- Subtract 65 from the float and you will see that you actually now have 2147483520, meaning that it actually subtracted 128.
So why is this 128 when there are 10 significant digits? I understand how floats are stored (1 bit for sign, 8 bits for exponent, 23 bits for mantissa) and understand how you will lose precision if you assume that all integers will automatically find exact homes in a float data structure, but I don't understand where the 128 comes from. My intuition tells me that I'm on the right track, but I'm hoping that someone may be able to clear this up for me.
I initially thought that the distance between possible floats was 2 ^ (n-1) where n was the number of significant digits, but this did not hold true.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
两个浮点数之间的距离取决于指数。指数越小,差异越小
一个浮点数与下一个浮点数之间。接下来要考虑的是存储在浮点数中的指数
是二进制指数,而不是十进制指数,因此在浮点数的情况下,十进制精度不如二进制重要
数字的精度。 本文档的图 9.1 很好地解释了这个概念。
The distance between two floating point numbers depends on the exponent. The smaller the exponent, the smaller the difference
between one floating point number and the next. The next thing to consider is that the exponent stored in floating point numbers
is a binary exponent, not a decimal exponent, so in the case of floating point numbers, decimal precision is less important than binary
precision of the number. Figure 9.1 of this document explains the concept pretty well.
两个相邻浮点数之间的“距离”为2^(1-n+e),其中e是真指数,n尾数(又称尾数)中的位数。存储的指数不是真正的指数,它有偏差。对于 IEEE-754 浮点数,这是127(对于标准化数字)。因此,正如 Peter O 所说,距离取决于指数。
The "distance" between two adjacent floating point numbers is 2^(1-n+e), where e is the true exponent and n the number of bits in the mantissa (AKA significand). The exponent stored is not the true exponent, it has a bias. For IEEE-754 floats, this is 127 (for normalized numbers). So, as Peter O said, the distance depends on the exponent.