Python change to built-in round () function between 2.4 and 2.7

Did the built-in round () function in Python work 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 any way to return the behavior of Python 2.4?

I know that the correct answer is, of course, using a decimal arithmetic module. Unfortunately, this is not an option at the moment, given the limited time limits.

Update

In the context, I compare values โ€‹โ€‹in two systems, one of which uses a decimal representation, and the other uses a floating point. This may (or not necessarily) be a legitimate difference between the systems that need to be checked further, so I will consult and engage with users at the โ€œreportingโ€ level, and not at the point where I get data from the systems.

Thank you for your help!

+4
source share
3 answers

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

The answer to your second question is: I'm not sure if not using earlier Python (2.6 should still demonstrate 2.4 behavior). In this particular case, your value is indistinguishable from 1480.395 and 1480.395 (for us, based on 10 people) rounds to 1480.40. Therefore, I assume that you can try to round to one place first, except that you really actually turn it into a string, and then get the decimal number from this ... but, unfortunately, I can't think of anything, which does not include Decimal. If I come up with something (before someone else writes something better), I will go back and edit.

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

Edit: Here is an example using a decimal number:

 >>> 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 float as input for Decimal. Do not do this , as you will return to where you started. Also, do not use the built-in round function in the decimal system, because this function converts your decimal right back to a float, and thus, you return to where you started. In its simplest form, Decimal quantize takes another decimal number, which has the number of decimal places you really want (think of it as a rounding pattern). If your data absolutely must be float, then only convert back to float after all your Decimal calculations are complete.

Finally: I'm not sure if this covers all of the possible weird corner cases in Python 2.4 floating point behavior. If you rely on the exact behavior of Python 2.4, there can be no replacement for Python 2.4. What I described above is just one step closer to rounding a person.

+6
source

Probably not, since Python 2.4 actually returned the wrong result. Since you are rounding an amount that is strictly less than 5, the correct result should be rounded down.

If you can describe the exact behavior you need, we can create an alternative approach.

+3
source

In what's new on the python 2.6 page, I see the following (with reference to PEP 3141 ):

Python 3.0 adds several abstract base classes for numeric types based on the number tower of a Schema. These classes were passed in 2.6 as a module of numbers.

...

In Python 3.0, PEP slightly overrides the existing built-in functions round (), math.floor (), math.ceil () and adds a new one, math.trunc (), which was passed in Python 2.6. math.trunc () is rounded to zero, returning the closest integral between the function argument and zero.

And thanks to the comment by @ mark-dickinson, what's new on the python 2.7 page , the following text was found:

The round () function is also now correctly rounded.

So this happened in python 2.7. This can be verified by checking that in python 2.6 the behavior is still the same:

 Python 2.6.7 (r267:88850, Aug 11 2011, 12:18:09) [GCC 4.6.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> f = 1480.39499999999998181010596454143524169921875 >>> round(f, 2) 1480.4000000000001 

In my Linux distribution, I still have access to both python 2.6 and python 2.7, and you probably have one too; so if you need the old behavior, maybe you can try running the code in python 2.6.

+1
source

Source: https://habr.com/ru/post/1387277/


All Articles