Python 2.4中的round()错了? [英] round() wrong in Python 2.4?

查看:71
本文介绍了Python 2.4中的round()错了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在Python 2.4中round()会发生变化?

$ python2.3

Python 2.3.5(#2,2005年6月19日,13 :28:00)

[GCC 3.3.6(Debian 1:3.3.6-6)]关于linux2

Why did round() change in Python 2.4?

$ python2.3
Python 2.3.5 (#2, Jun 19 2005, 13:28:00)
[GCC 3.3.6 (Debian 1:3.3.6-6)] on linux2

round(0.0225,3)
0.023"%。3f" %round(0.0225,3)
''0.023''
$ python2.4

Python 2.4.1(#2,2005年7月12日,09:22:25)

[GCC 4.0.1(Debian 4.0.1-1)] on linux2 round(0.0225,3)
0.021999999999999999"%。3f" %round(0.0225,3)
''0.022''
round(0.0225, 3) 0.023 "%.3f" % round(0.0225, 3) ''0.023'' $ python2.4
Python 2.4.1 (#2, Jul 12 2005, 09:22:25)
[GCC 4.0.1 (Debian 4.0.1-1)] on linux2 round(0.0225, 3) 0.021999999999999999 "%.3f" % round(0.0225, 3) ''0.022''




(这是由于使用了不同的GCC吗? )


如何正确输出2.4中的浮点数?


我不喜欢打印数+ EPS解决方案,因为你需要不同指数大小的
不同的EPS。在C中,您可以通过

获得整数1,并且在右指数中使用& -ing,然后通过void *转换为

double。但这不是很便携。

Klem fra Nils



(Is this due to the different GCC used?)

How do you correctly output floating-point numbers in 2.4?

I do not like the "print number + EPS" solution, as you would need
different EPS for different exponent sizes. In C you could get it by
taking integer 1, and &-ing in the right exponent, and then casting to
double via void*. This would not be very portable, though.
Klem fra Nils

推荐答案

python2.3

Python 2.3.5(#2,2005年6月19日,13:28:00)

[GCC 3.3.6(Debian 1:3.3.6-6)] on linux2
python2.3
Python 2.3.5 (#2, Jun 19 2005, 13:28:00)
[GCC 3.3.6 (Debian 1:3.3.6-6)] on linux2
round(0.0225,3)
0.023"%。3f" %round(0.0225,3)
''0.023''
round(0.0225, 3) 0.023 "%.3f" % round(0.0225, 3) ''0.023''


python2.4

Python 2.4.1(#2,2005年7月12日, 09:22:25)

[GCC 4.0.1(Debian 4.0.1-1)] on linux2 round(0.0225,3)
0.021999999999999999"%。3f" %round(0.0225,3)
''0.022''
python2.4
Python 2.4.1 (#2, Jul 12 2005, 09:22:25)
[GCC 4.0.1 (Debian 4.0.1-1)] on linux2 round(0.0225, 3) 0.021999999999999999 "%.3f" % round(0.0225, 3) ''0.022''




(这是由于使用了不同的GCC吗? )


如何正确输出2.4中的浮点数?


我不喜欢打印数+ EPS解决方案,因为你需要不同指数大小的
不同的EPS。在C中,您可以通过

获得整数1,并且在右指数中使用& -ing,然后通过void *转换为

double。但这不是很便携。

Klem fra Nils



(Is this due to the different GCC used?)

How do you correctly output floating-point numbers in 2.4?

I do not like the "print number + EPS" solution, as you would need
different EPS for different exponent sizes. In C you could get it by
taking integer 1, and &-ing in the right exponent, and then casting to
double via void*. This would not be very portable, though.
Klem fra Nils


Nils Grimsmo写道:
Nils Grimsmo wrote:
为什么在Python 2.4中round()会发生变化?
Why did round() change in Python 2.4?




这是通常的浮点表示问题。 0.0225不能完全代表



xpc20:〜> python

Python 2.3.4(#1,2005年3月14日,16:47:22)

[GCC 3.4.3 20041212(Red Hat 3.4.3-9。在linux2上的EL4)]

输入help,copyright,credit等等。或许可证或欲了解更多信息。



It the usual floating point representation problem. 0.0225 cannot be
represented exactly:

xpc20:~> python
Python 2.3.4 (#1, Mar 14 2005, 16:47:22)
[GCC 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

0.0225



0.022499999999999999



http://www.python.org/doc/current/tu...00000000000000


如果你需要精确的数学,那你就是''最好使用整数或小数

算术。


Jeremy


-

Jeremy Sanders
http://www.jeremysanders.net/


这篇关于Python 2.4中的round()错了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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