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 Ultrasick, mark.dickinson
Date 2009-01-31.15:14:07
SpamBayes Score 5.867529e-14
Marked as misclassified No
Message-id <1233414849.97.0.0570185009845.issue5118@psf.upfronthosting.co.za>
In-reply-to
Content
> print round(2.545, 2) // returns 2.55

Aha!  Yes, that one *is* a bug (see issue #1869), though it's not one that 
I regard as terribly serious, and not one that can be easily solved in all 
cases.

Here's why I don't see it as particularly serious: you're rounding a value 
that's just on the boundary:  2.545+tiny_error should round up, while 
2.545-tiny_error should round down.  But tiny (or not-so-tiny) errors are 
an almost unavoidable part of working with binary floating-point 
arithmetic.  Additionally, whether the binary approximation stored for 
2.545 is less than or greater than the true value depends on many things 
(format of a C double, system C library function used for string-to-double 
conversion, etc.), so in a sense either 2.55 *or* 2.54 can be defended as 
a valid result, and a good numeric programmer won't write code that 
depends on getting one or the other.

Having said that, if you're interested in providing a patch for issue 
#1869 I'd certainly take a look.

If you care about *exact* representations of numbers with a finite number 
of places after the decimal point, you may be interested in Python's 
'decimal' module.
History
Date User Action Args
2009-01-31 15:14:10mark.dickinsonsetrecipients: + mark.dickinson, Ultrasick
2009-01-31 15:14:09mark.dickinsonsetmessageid: <1233414849.97.0.0570185009845.issue5118@psf.upfronthosting.co.za>
2009-01-31 15:14:08mark.dickinsonlinkissue5118 messages
2009-01-31 15:14:07mark.dickinsoncreate