On 07-Feb-12 03:15, Steven D'Aprano wrote:
Steve Willoughby wrote:
If you need lots of precision, you might consider using the decimal
class. It'll cost you speed vs. the native floating-point type but
won't cause you round-off errors.

I'm afraid that's not correct. Decimal is still subject to rounding errors.

 >>> from decimal import Decimal
 >>> x = 1/Decimal(3) # one third, as close as a Decimal can give
 >>> x + x + x == 1
False

Sorry, I guess I took it for granted that was understood. I was referring to round-off caused by binary representation of a number that by all appearances is a simple, rational, non-repeating decimal number expressed in base 10.

In other words, when you're adding up financial figures, you'll get an accurate sum without "mysterious" rounding off, but of course certain fractions which don't have a straightforward way to represent as a decimal value, like 1/3, are going to be an issue. Good catch, though, it was better to point that out.


--
Steve Willoughby / st...@alchemy.com
"A ship in harbor is safe, but that is not what ships are built for."
PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to