This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author mark.dickinson
Recipients gboutsioukis, mark.dickinson
Date 2009-04-17.20:13:52
SpamBayes Score 5.684e-11
Marked as misclassified No
Message-id <>
Here's a patch that improves float rounding in a number of respects:

(1) (when available) it uses the recently added correctly-rounded str <-
> float conversions in Python/dtoa.c.  This means that:

- round(x, n) finally does what the docs say it does:  rounds x to the 
closest multiple of 10**-n (and then returns the closest float to the

- for n > 0, the digits of round(x, n) will always agree with those
  of "%.<n>f" % x.

One *really* nice feature is that since e.g. round(x, 2) will always be 
the closest float to a multiple of 0.01, thanks to the recent repr 
changes it'll always be output with at most two places following the 
decimal point, and so will appear to be properly rounded...

Other changes. round(x, n) now:

(2) deals correctly with large values of n

(3) deals correctly with infinities and nans for x

(4) deals correctly with overflow (e.g. round(1.5e308, -308))

(5) doesn't have the weird exception for n = -2**31+1, which was
treated as a single-parameter round.

Still needs tests.  I'm working on them.
Date User Action Args
2009-04-17 20:13:56mark.dickinsonsetrecipients: + mark.dickinson, gboutsioukis
2009-04-17 20:13:56mark.dickinsonsetmessageid: <>
2009-04-17 20:13:54mark.dickinsonlinkissue1869 messages
2009-04-17 20:13:54mark.dickinsoncreate