[Tutor] decimal precision in python
Steven D'Aprano
steve at pearwood.info
Tue Feb 7 12:15:28 CET 2012
Steve Willoughby wrote:
> On 06-Feb-12 07:25, Kapil Shukla wrote:
>> i tried writing a small code to calculate option price using the
>> binomial tree model. I compared my results with results of the same
>> program in excel. There seems to be a minor difference due to decimal
>> precision as excel is using 15 decimal precision and python (both 2.7
>> and 3.1) using 11. (at least that's what shown on shell)
>
> 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
The difference is that the rounding errors you get with Decimal are usually
different to the ones you get with binary floats. For example:
>>> y = 0.1 # one tenth, as close as a binary float can give
>>> y+y + y+y + y+y + y+y + y+y == 1
False
while the same calculation is exact with Decimal.
The reason for the error is the same in both cases: in the first, 1/3 takes an
infinite number of decimal digits, while in the second, 1/10 takes an infinite
number of binary digits. So Decimal 1/3 is not *precisely* 1/3, and float 1/10
is not precisely 1/10 either.
Binary floats can store exactly any fraction which can be written as a sum of
powers of 1/2, e.g.:
0.40625 = 13/32 = 1/4 + 1/8 + 1/32 so can be stored exactly in a float
Every other number is rounded. The same applies to Decimal: it can store
exactly any fraction which can be written as a sum of powers of 1/10.
The advantage of Decimal is that anything which can be stored as an exact
float can also be stored as an exact Decimal, plus some numbers which can't be
written as exact floats. But there are still plenty of numbers which can't be
stored exactly as either.
--
Steven
More information about the Tutor
mailing list