Python数学是错误的

2024-04-30 02:52:16 发布

您现在位置:Python中文网/ 问答频道 /正文

Possible Duplicate:
Python rounding error with float numbers

Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>> 4.2 - 1.8
2.4000000000000004
>>> 1.20 - 1.18
0.020000000000000018
>>> 5.1 - 4
1.0999999999999996
>>> 5 - 4
1
>>> 5.0 - 4.0
1.0

为什么Python的数学错了?


Tags: ortimeisitcontentthisareat
2条回答

对于浮点运算如何工作的严格指导,请完成有关如何计算答案中错误大小的说明,您需要:

What Every Computer Scientist Should Know About Floating-Point Arithmetic

一旦你读过,你应该去寻找8087上的注释,特别是,因为它执行IEEE 754很差,这可能会在你的生活中引起新的令人兴奋的问题。

你已经在计算机科学上达到了一个新的水平,而且你已经成年了。

因此,你现在已经为下一步做好了准备。我得到了BDFL本人的授权,向您透露以下超级机密文件。古人明白了,先破译了,现在,你也要破译!

The Floating Point Guide

小心对待这份文件!只跟你认识的人分享这个,他们得出了同样令人困惑的结论!


Moderator's Note

This answer is not representative of the expected quality standards on Stack Overflow. However, it has unexpectedly developed a life of its own and is solely preserved for historical significance now.

相关问题 更多 >