奇怪的浮点输出 [英] Bizarre floating-point output

查看:89
本文介绍了奇怪的浮点输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



x =(1.234567890125,1.2345678901255)

打印x

打印x [0],x [1]
< blockquote class =post_quotes>


>>(1.2345678901249999,1.2345678901254999)
1.23456789012 1.23456789013



是否有合理的理由,或者这只是代码演变的方式的工件?b
这显然不是一个错误:-)

问候,

Nick Maclaren。

解决方案



" Nick Maclaren" < nm ** @ cus.cam.ac.ukwrote in message

news:en ********** @ gemini.csx.cam.ac.uk ...


>

x =(1.234567890125,1.2345678901255)

打印x

打印x [0],x [1]


>>>(1.2345678901249999,1.2345678901254999)
1.23456789012 1.23456789013



是否有合理的理由,或者这只是代码演变的方式的工件?b
这显然不是一个bug :-)



print x [0]给出与打印str(x [0])相同的结果,

x的值格式化为字符串(四舍五入到

合理的位数)。


x [0]在命令提示符下给出相同的值结果为

打印repr(x),文本值的表示为

a string。


当你打印时一个元组它不会递归

调用str(),所以你得到了repr表示。


你可以得到类似的结果

str()和repr()值不同。

例如x =(u''a'',u''b'')




文章< en ***** ****@south.jnrs.ja.net> ;,

" Richard Brodie" < R。****** @ rl.ac.ukwrites:

|>

|当您在元组上打印时,它不会递归

|调用str(),所以你得到了repr表示。


啊!这解释了它。我会把这个理由称为理性和代码进化方式之间的神器之间的中间价格。

问候,

Nick Maclaren。


Nick Maclaren写道:


啊!这解释了它。我会把这个理由称为理性和代码演化方式之间的神器之间的中间价格。



哪些代码已经发展?那些精确问题是固有的

浮点数存储在内存中的问题。


问候,

Bj?rn


-

BOFH借口#292:


我们用完了拨号音,我们等着等为电话公司

提供另一瓶。



x = (1.234567890125, 1.2345678901255)
print x
print x[0], x[1]

>>(1.2345678901249999, 1.2345678901254999)
1.23456789012 1.23456789013

Is there a rational reason, or is that simply an artifact of the way
that the code has evolved? It is clearly not a bug :-)
Regards,
Nick Maclaren.

解决方案


"Nick Maclaren" <nm**@cus.cam.ac.ukwrote in message
news:en**********@gemini.csx.cam.ac.uk...

>
x = (1.234567890125, 1.2345678901255)
print x
print x[0], x[1]

>>>(1.2345678901249999, 1.2345678901254999)
1.23456789012 1.23456789013


Is there a rational reason, or is that simply an artifact of the way
that the code has evolved? It is clearly not a bug :-)

print x[0] gives the same result as printing str(x[0]),
the value of x formatted as a string (rounded to a
sensible number of places).

x[0] at the command prompt gives the same result as
printing repr(x), the representation of the text value as
a string.

When you do print on a tuple it doesn''t recursively
call str(), so you get the repr representations.

You can get similar results with anything where the
str() and repr() values are different.
e.g. x = ( u''a'', u''b'')



In article <en*********@south.jnrs.ja.net>,
"Richard Brodie" <R.******@rl.ac.ukwrites:
|>
|When you do print on a tuple it doesn''t recursively
|call str(), so you get the repr representations.

Ah! That explains it. I would call that reason intermediate
between rational and an artifact of the way the code has evolved!
Regards,
Nick Maclaren.


Nick Maclaren wrote:

Ah! That explains it. I would call that reason intermediate
between rational and an artifact of the way the code has evolved!

Which code has evolved? Those precision problems are inherent
problems of the way floats are stored in memory.

Regards,
Bj?rn

--
BOFH excuse #292:

We ran out of dial tone and we''re and waiting for the phone company
to deliver another bottle.


这篇关于奇怪的浮点输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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