python中的数学错误 [英] Math errors in python
问题描述
在Windows 95下运行的python 2.3(IDLE 1.0.3)中,每当我进行简单的算术运算时,我都会得到
以下类型的错误:
第一个例子:
12.10 + 8.30
20.399999999999999 1.1 - 0.2
0.90000000000000013
第二个例子(这里没有错误):bool(130.0 - 129.0 == 1.0)
True
第3个例子:a = 0.013
b = 0.0129
c = 0.0001
[a,b,c]
[0.01299999999999999999,0.0129,0.0001] bool((a - b)== c)
False
在大多数情况下,这种错误并不是什么大不了的事,但我确定它可以在某些条件下成为问题,特别是3
例子,我正在使用真值测试。所有
情况都会出现相同的结果,无论我是定义变量a,b和c,还是直接在bool语句中输入值
。此外,它并没有什么区别
是否a = 0.013或者a = 0.0130。
我没有在Windows 2000或XP下检查过这个,但我希望同样的事情会发生。有关修复此类
错误的方法的建议吗?
查看常见问题解答(之前的回复你的消息构建)。
-----原始消息-----
来自:放射性人
新闻组:comp.lang.python
要: py ******** *@python.org
已发送:2004年9月18日星期六上午9:50
主题:python中的数学错误
在Windows 95下运行的python 2.3(IDLE 1.0.3)中,每当我进行简单的算术运算时,我都会得到
以下类型的错误:
第一个例子:12.10 + 8.30
20.399999999999999 1.1 - 0.2
0.90000000000000013
2nd示例(此处没有错误):bool(130.0 - 129.0 == 1.0)
True
第3例:a = 0.013
b = 0.0129
c = 0.0001
[a,b,c]
[0。 012999999999999999,0.0129,0.0001] bool((a - b)== c)
False
这种错误在大多数情况下没什么大不了的,但我确定它可以在某些条件下成为问题,特别是在我使用的第3个b $ b例子中真相测试。所有
情况都会出现相同的结果,无论我是定义变量a,b和c,还是直接在bool语句中输入值
。此外,它并没有什么区别
是否a = 0.013或者a = 0.0130。
我没有在Windows 2000或XP下检查过这个,但我希望同样的事情会发生。有什么方法可以解决这种问题吗?
错误?
-
http://mail.python.org/mailman/listinfo/python-list
>
[放射性人]在Windows 95下运行的python 2.3(IDLE 1.0.3)中,每当我进行简单算术时,我都会得到以下类型的错误:
第一个例子:12.10 + 8.30
20.399999999999999
....
请阅读关于浮点问题的教程附录:
http://docs.python.org/tut/node15.html
2004年9月18日星期六上午09:50,放射性人写道:在python 2.3(IDLE 1) .0.3)在Windows 95下运行,每当我做简单算术时,我都会得到以下类型的错误:
第一个例子:12.10 + 8.30
20.399999999999999
这不是一个错误,它是二进制算术的一个特征在所有coumputers
所有语言。 (但也许Python是第一次没有向你隐藏
。)
参见Python FAQ条目1.4.2:
http://www.python.org/doc/faq/genera...-so-inaccurate
Gary Herron
In python 2.3 (IDLE 1.0.3) running under windows 95, I get the
following types of errors whenever I do simple arithmetic:
1st example:
12.10 + 8.30 20.399999999999999 1.1 - 0.2 0.90000000000000013
2nd example(no errors here): bool(130.0 - 129.0 == 1.0) True
3rd example: a = 0.013
b = 0.0129
c = 0.0001
[a, b, c] [0.012999999999999999, 0.0129, 0.0001] bool((a - b) == c)
False
This sort of error is no big deal in most cases, but I''m sure it could
become a problem under certain conditions, particularly the 3rd
example, where I''m using truth testing. The same results occur in all
cases whether I define variables a, b, and c, or enter the values
directly into the bool statement. Also, it doesn''t make a difference
whether "a = 0.013" or "a = 0.0130".
I haven''t checked this under windows 2000 or XP, but I expect the same
thing would happen. Any suggestions for a way to fix this sort of
error?
Have a look at the FAQ (before the response to your message builds).
----- Original Message -----
From: Radioactive Man
Newsgroups: comp.lang.python
To: py*********@python.org
Sent: Saturday, September 18, 2004 9:50 AM
Subject: Math errors in python
In python 2.3 (IDLE 1.0.3) running under windows 95, I get the
following types of errors whenever I do simple arithmetic:
1st example:12.10 + 8.30 20.399999999999999 1.1 - 0.2 0.90000000000000013
2nd example(no errors here): bool(130.0 - 129.0 == 1.0) True
3rd example: a = 0.013
b = 0.0129
c = 0.0001
[a, b, c] [0.012999999999999999, 0.0129, 0.0001] bool((a - b) == c)
False
This sort of error is no big deal in most cases, but I''m sure it could
become a problem under certain conditions, particularly the 3rd
example, where I''m using truth testing. The same results occur in all
cases whether I define variables a, b, and c, or enter the values
directly into the bool statement. Also, it doesn''t make a difference
whether "a = 0.013" or "a = 0.0130".
I haven''t checked this under windows 2000 or XP, but I expect the same
thing would happen. Any suggestions for a way to fix this sort of
error?
--
http://mail.python.org/mailman/listinfo/python-list
[Radioactive Man]In python 2.3 (IDLE 1.0.3) running under windows 95, I get the
following types of errors whenever I do simple arithmetic:
1st example:12.10 + 8.30
20.399999999999999
....
Please read the Tutorial appendix on floating-point issues:
http://docs.python.org/tut/node15.html
On Saturday 18 September 2004 09:50 am, Radioactive Man wrote:In python 2.3 (IDLE 1.0.3) running under windows 95, I get the
following types of errors whenever I do simple arithmetic:
1st example:12.10 + 8.30
20.399999999999999
It''s not a bug, it''s a feature of binary arithmetic on ALL coumputers
in ALL languages. (But perhaps Python is the first time it has not
been hidden from you.)
See the Python FAQ entry 1.4.2:
http://www.python.org/doc/faq/genera...-so-inaccurate
Gary Herron
这篇关于python中的数学错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!