问题描述
>>(1.0 / 10.0)+(2.0 / 10.0)+(3.0 / 10.0)
0.60000000000000009
>>(1.0/10.0) + (2.0/10.0) + (3.0/10.0)
0.60000000000000009
0.59999999999999998
使用十进制模块是最好的方法吗? (我期待第一个
总和来匹配第二个)。看起来不合时宜的是十进制字符串为
输入。
非常感谢帮助;
Rory
-
Rory Campbell-Lange
< ro ** @ campbell-lange.net>
< www.campbell -lange.net>
0.59999999999999998
Is using the decimal module the best way around this? (I''m expecting the first
sum to match the second). It seem anachronistic that decimal takes strings as
input, though.
Help much appreciated;
Rory
--
Rory Campbell-Lange
<ro**@campbell-lange.net>
<www.campbell-lange.net>
推荐答案
你的结果有什么问题,或者你的目标是什么?具有浮点数的这种
精度误差是正常的,因为
精度在技术上是有限的。
对于浮点数a和b,你很少会说if a == b: (因为在你的情况下,它的价格通常是假的),而是
如果a - b<阈值:"对于合理的阈值,
取决于您的申请。
同时检查最近的线程奇异的浮点输出。
问候,
Bj?
-
BOFH借口#333:
需要水管工,网络排水管堵塞
What''s your problem with the result, or what''s your goal? Such
precision errors with floating point numbers are normal because the
precision is limited technically.
For floats a and b, you''d seldom say "if a == b:" (because it''s
often false as in your case) but rather
"if a - b < threshold:" for a reasonable threshold value which
depends on your application.
Also check the recent thread "bizarre floating point output".
Regards,
Bj?rn
--
BOFH excuse #333:
A plumber is needed, the network drain is clogged
[...]
同时检查最近的线程奇异的浮点输出。
[...]
Also check the recent thread "bizarre floating point output".
Python教程的最后一节浮动点
算术:问题和限制
-
Gabriel Genellina
Softlab SRL
__________________________________________________
$ b $bPreguntá。 Respondé。 Descubrí。
Todo loquequeríasaber,y lo que ni imaginabas,
$ b $bestáenYahoo! Respuestas(测试版)。
?Probalo ya!
And the last section on the Python Tutorial "Floating Point
Arithmetic: Issues and Limitations"
--
Gabriel Genellina
Softlab SRL
__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
?Probalo ya!
http://www.yahoo.com.ar/respuestas
0.60000000000000009
0.60000000000000009
0.59999999999999998
正在使用十进制模块围绕这个最好的方法? (我期待第一个
总和来匹配第二个)。
0.59999999999999998
Is using the decimal module the best way around this? (I''m expecting the first
sum to match the second).
可能不是。十进制算术不能解决浮点数b / b
算术错误。
Probably not. Decimal arithmetic is NOT a cure-all for floating-point
arithmetic errors.
十进制(" ; 0.9999999999999999999999999999")
Decimal("0.9999999999999999999999999999")
十进制(" 1.999999999999999999999999999")
Decimal("1.999999999999999999999999999")
你如何区分十进制(''0.1'')和
十进制(''0.100000000000000005551115123125782702118 1583404541015625'')?br />
How else would you distinguish Decimal(''0.1'') from
Decimal(''0.100000000000000005551115123125782702118 1583404541015625'')?
这篇关于数学错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!