浮点表示错误?
当我做这个乘法时
0.94 * 8700
输出为
8177.999999999999
但应该是
8178
我正在使用 java,但我认为此错误与特定编程语言无关 现在我的问题是......为什么会发生这种情况?
还有哪些其他数字(仅作为示例)会导致相同的错误?
when i make this multiplication
0.94 * 8700
the output is
8177.999999999999
but it should have been
8178
i'm using java , but i don't think this error is related to a particular Programming language
now my question is ... why this happened ??
and what other numbers (just as an example) cause the same error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的情况的具体原因是实数 0.94 无法用双精度浮点数精确表示。当您输入
0.94
时,实际存储的数字为0.939999999999999946709294817992486059665679931640625
。The specific reason in your case is that the real number 0.94 cannot be represented exactly in a double precision floating point. When you type
0.94
, the actual number stored is0.939999999999999946709294817992486059665679931640625
.这不是一个错误。 IEEE 浮点数不能精确表示十进制数。
It isn't an error. IEEE floats can't represent decimal numbers exactly.