简单的补充 [英] Simple addition

查看:66
本文介绍了简单的补充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我尝试了一个简单的添加python,我不明白是什么是

正在进行:


$ python

Hi,

I tried a simple addition with python and I don''t understand what is
going on:

$python

464.73 + 279.78
464.73+279.78



744.50999999999999


很奇怪不是吗?我使用python2.3。


评论欢迎

Mathieu


744.50999999999999

weird isn''t it ? I use python2.3.

comments welcome
Mathieu

推荐答案

python
464.73 + 279.78
464.73+279.78



744.50999999999999

怪异不是吗?我使用python2.3。


评论欢迎

Mathieu


744.50999999999999

weird isn''t it ? I use python2.3.

comments welcome
Mathieu


Mathieu Malaterre写道:
Mathieu Malaterre wrote:


python
>> 464.73 + 279.78 744.50999999999999

奇怪的不是吗?我使用python2.3。
>>464.73+279.78 744.50999999999999

weird isn''t it ? I use python2.3.




Mathieu,您可以在

以下文档链接中找到您所看到内容的完整说明:
http://www.python.org /doc/current/tut/node15.html


从该页面:


"注意这是在二进制浮点的本质:这是

不是Python中的错误,它也不是代码中的错误,你会看到

相同支持你的硬件的所有语言的东西

浮点运算(虽然某些语言默认情况下可能不显示

差异,或者在所有输出模式下)。


Python的内置str()函数只产生12位有效数字,而

你可能希望使用它。 eval(str(x))对于
重现x是不寻常的,但输出可能更令人愉快:



Mathieu, you can find a full explanation of what you''re seeing at the
following documentation link:
http://www.python.org/doc/current/tut/node15.html

From that page:

"Note that this is in the very nature of binary floating-point: this is
not a bug in Python, it is not a bug in your code either, and you''ll see
the same kind of thing in all languages that support your hardware''s
floating-point arithmetic (although some languages may not display the
difference by default, or in all output modes).

Python''s builtin str() function produces only 12 significant digits, and
you may wish to use that instead. It''s unusual for eval(str(x)) to
reproduce x, but the output may be more pleasant to look at:

print str(0.1)
print str(0.1)



0.1


重要的是要意识到这是,在真正意义上,一个幻觉:

机器中的值不完全是1/10,你只是简单地将真实机器值的显示四舍五入。


0.1

It''s important to realize that this is, in a real sense, an illusion:
the value in the machine is not exactly 1/10, you''re simply rounding the
display of the true machine value."


这篇关于简单的补充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆