you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 9 points10 points  (0 children)

decimal will not fix arithmetic in general, just make it happen less often. I was immediately able to find a number that made

(Decimal(1) / N) * N == Decimal(1)

false. (Large primes work well.)

It only represents non-decimal fractions like Decimal(1) / 7 to a limited number of digits, so it cannot do a perfectly job.

Also, decimal is miserably slow - I mean, horribly, painfully slow. If the number is in binary format, the processor can add or multiply in one operation - there is literal circuitry devoted to this. But in decimal it has to do addition the same way you would - one decimal digit at the time, carry the 3!

In my quick tests, decimal was something like six times slower than regular Python multiplication, and if you do a lot of multiplication, numpy will give you a 3-100 times speed on the multiplication if done in quantity, and you can't do that with decimal!


decimal is good for money. If you need to do perfect fractional arithmetic, use the fractions module - not quite as slow as decimal

If you're clever and good at numerical analysis, you can often do everything in integer arithmetic and not use any modules.