Message80871
> 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. |
|
Date |
User |
Action |
Args |
2009-01-31 15:14:10 | mark.dickinson | set | recipients:
+ mark.dickinson, Ultrasick |
2009-01-31 15:14:09 | mark.dickinson | set | messageid: <1233414849.97.0.0570185009845.issue5118@psf.upfronthosting.co.za> |
2009-01-31 15:14:08 | mark.dickinson | link | issue5118 messages |
2009-01-31 15:14:07 | mark.dickinson | create | |
|