0.699 x 100 = 69.89999999999999?
我已经在 php 和 javascript 中看到这个问题。我有这个数字:float 0.699
如果我这样做: 0.699 x 100 = 69.89999999999999
为什么?
编辑
round(0.699 x 10, 2)
: float 69.90000000000001
Possible Duplicates:
Why does 99.99 / 100 = 0.9998999999999999
Dealing with accuracy problems in floating-point numbers
I've seen this issue in php and javascript. I have this number: float 0.699
if I do this:
0.699 x 100 = 69.89999999999999
why?
edit
round(0.699 x 10, 2)
: float 69.90000000000001
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
浮点运算并不精确。
请参阅维基百科上的浮点以更深入地讨论该问题。
Floating point arithmetic is not exact.
See Floating point on Wikipedia for a deeper discussion of the problem.
这就是过去对我的帮助。这与事物如何以二进制表示有很大关系。基本上,长话短说,二进制中没有所有大数实数的确切数字。
下面的链接将为您更详细地描述这一点。
每个计算机科学家应该了解的浮点运算知识
This is what has helped me in the past. It has a lot to do with how things are represented in binary. Basically long story short in binary there isn't an exact number for all real numbers of large numbers.
The link below will describe that in more detail for you.
What Every Computer Scientist Should Know About Floating-Point Arithmetic
这在任何语言中都会发生。与计算机上的其他所有内容一样,浮点数也以二进制形式存储。数字 0.699 虽然可以精确地用十进制表示,但可能是二进制的重复小数,因此无法以精确的精度存储。
查看维基百科条目了解浮点数的存储方式以及发生这种情况的原因。
This will happen in any language. Floats, like everything else on a computer, are stored as binary. The number 0.699, while representable exactly in decimal, is probably a repeating decimal in binary, so it can't be stored to exact precision.
Check out the wikipedia entry for how floats are stored, and why this happens.
JavaScript 数字是浮点。
查看完整的 JavaScript 编号参考。摘抄:
Javascript numbers are floating point.
Take a look at The complete javascript number reference. Excerpt:
查看浮点,特别是有关 IEEE 754 和可表示数字的部分。
Take a look at Floating Point, specifically the section on IEEE 754 and representable numbers.
这种行为可以用许多编程语言重现,包括 C++ 和汇编语言。原因是FPU 使用浮点格式。您可以在此处阅读详细信息:
http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/Chapter_14/CH14-1.html#HEADING1-19
一般规则:永远不要期望浮动的确切结果- 点操作。永远不要比较两个浮点数,使用区间,例如:不要测试 f1 == f2,而是使用 f1 > (f2-e)且f1< ( f2 + e ), e 是一些小值。
This behavior can be reproduced in many programming languages, including C++ and Assembly. The reason is floating point format using by FPU. You can read details here:
http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/Chapter_14/CH14-1.html#HEADING1-19
General rule: never expect exact result of floating-point operations. Never compare two floating point numbers, use interval, for example: instead of testing f1 == f2, use f1 > (f2 -e) and f1 < ( f2 + e ), e is some small value.