在2.4和2.7之间的round()函数中内置的Python改变 [英] Change in Python built in round() function between 2.4 and 2.7

查看:360
本文介绍了在2.4和2.7之间的round()函数中内置的Python改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python中内置的round()函数在2.4到2.7之间变化了吗?



Python 2.4:

  Python 2.4.6(#1,Feb 12 2009,14:52:44)
[GCC 3.4.6 20060404(Red Hat 3.4.6-8)] on linux2
输入help,copyright,credits或license以获取更多信息。
>>> f = 1480.39499999999998181010596454143524169921875
>>> round(f,2)
1480.4000000000001
>>>

Python 2.7:

  Python2.7.1(r271:86832,2011年5月13日,08:14:41)
[GCC 3.4.6 20060404(Red Hat 3.4.6-11)] on linux2
输入帮助,版权,信用或许可证以获取更多信息。
>>> f = 1480.39499999999998181010596454143524169921875
>>> round(f,2)
1480.39
>>>

有没有办法让Python 2.4行为回来?



我知道正确的答案当然是使用十进制算术模块。不幸的是,在这个时间限制下,这可能不是一种选择。



更新

对于上下文,我比较两个系统中的值,其中一个使用十进制表示,另一个使用浮点数。这可能(或可能不)是需要进一步检查的系统之间的合理区别,所以我将在报告级别咨询用户并处理它,而不是从系统获取数据。



感谢您的帮助! 解决方案

第一个问题是:是的, round 是在Python 2.7中修复的。

第二个问题的答案是:I不能确定,实际上使用较早的Python(2.6应该仍然表现出2.4的行为)。在这个特殊情况下,你的价值从1480.395和1480.395(对于我们的基数为10的人类)来说是1480.40。所以我想你可以先尝试一下,然后把它变成一个字符串,然后从中得到一个十进制数,但对不起,我想不出任何不涉及十进制的东西。如果我想到一些东西(之前别人发布的东西更好),我会回来编辑。

(但实际上,十进制是难以使用的吗?)



编辑:这里是一个如何使用Decimal的例子:

 >>> from decimal import Decimal 
>>> f =十进制('1480.395')
>>> f.quantize(十进制('0.00'))
十进制('1480.40')
>>>一些注意事项:

Python 2.7允许您使用浮点数作为Decimal的输入。 不要这样做,因为你会回到你开始的地方。另外,不要在Decimal上使用内置的 round 函数,因为这个函数会把你的De​​cimal右转换成float,所以你会回到where你开始了。在最简单的形式中,Decimal的 quantize 需要另一个Decimal,它包含您真正想要的小数位数(将其视为四舍五入的模板)。如果你的数据必须是浮点数,那么只有在所有的十进制计算完成后才转换回浮点数。



最后:我不确定这覆盖了所有可能奇怪的角落情况在Python 2.4的浮动行为。如果您完全依赖Python 2.4的行为,那么在Python 2.4上运行可能没有任何替代。上面所描述的仅仅是向人类四舍五入的方向迈进了一步。

Has the built in round() function in Python changed between 2.4 and 2.7?

Python 2.4:

Python 2.4.6 (#1, Feb 12 2009, 14:52:44)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> f = 1480.39499999999998181010596454143524169921875
>>> round(f,2)
1480.4000000000001
>>>

Python 2.7:

Python 2.7.1 (r271:86832, May 13 2011, 08:14:41)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> f = 1480.39499999999998181010596454143524169921875
>>> round(f, 2)
1480.39
>>>

Is there anyway to get the Python 2.4 behaviour back?

I'm aware that the right answer is of course to use the decimal arithmetic module. Unfortunately, this probably isn't an option at this point given time limitations.

Update

For context i'm comparing values in two systems, one of which uses a decimal representation and the other a floating point one. This may (or may not) be a legitimate difference between the systems which needs to be checked further, so i'll consult users and deal with it at the "reporting" level, rather than at the point I get the data from the systems.

Thanks for your help!

解决方案

The answer to your first question is: yes, round was fixed in Python 2.7.

The answer to your second question is: I'm not sure, short of actually using an earlier Python (2.6 should still exhibit 2.4's behavior). In this particular case, your value is indistinguishable from 1480.395, and 1480.395 (to us base-10 humans) rounds to 1480.40. So I suppose you could try first rounding to one place beyond what you are really after, turn that into a string, then get a Decimal from that... but sorry, I can't think of anything that doesn't involve Decimal. If I think of something (before someone else posts something better), I'll come back and edit.

(But really, is Decimal all that hard to use?)

Edit: Here is an example of how to use Decimal:

>>> from decimal import Decimal
>>> f = Decimal('1480.395')
>>> f.quantize(Decimal('0.00'))
Decimal('1480.40')
>>> float(Decimal('1480.40'))
1480.4

Some notes:

Python 2.7 allows you to use floats as input to Decimal. Don't do this as you would be back where you started. Also, don't use the built-in round function on a Decimal, because that function is going to convert your Decimal right back to float, and thus you would be back where you started. In its simplest form, Decimal's quantize takes another Decimal that has the number of decimal places you really want (think of it as a template for the rounding). If your data absolutely has to be float, then only convert back to float after all your Decimal calculations are done.

Finally: I'm not sure this covers all the possible weird corner cases in Python 2.4's float behavior. If you are relying on exactly Python 2.4's behavior, there may be no substitute to running on Python 2.4. What I have described above is merely one step closer to human-style rounding.

这篇关于在2.4和2.7之间的round()函数中内置的Python改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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