[Tutor] decimal precision in python
Dave Angel
d at davea.name
Mon Feb 6 16:52:55 CET 2012
On 02/06/2012 10:25 AM, 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)
>
> can some one guide me whats the equivalent of using a double datatype on
> python and i can't use long() in 3.1 any more.
>
> Please also suggest a free editor for python which can at least repeat
> previous command with a key stroke
>
> regards
> kapil
>
>
A Python float is equivalent to a C double; it's already about 18
digits, using the IEEE binary hardware available on most modern
systems. However, it's a binary value, so it'll round in different
places than the decimal values that Excel probably uses.
You might want to read this:
http://docs.python.org/tutorial/floatingpoint.html
From the subject you choose, you are apparently asking for a decimal
package. You can use the python decimal package if you actually need
the round-off to be according to decimal's quirks. Certainly that's
easier (and slower) to deal with. Or you can use decimal because you
need more than about 18 digits. It defaults to 28, but you can set it
higher or lower. import decimal.
http://docs.python.org/library/decimal.html
<http://docs.python.org/library/decimal.html>
For a free editor that's python friendly, I'd suggest Komodo Edit,
http://www.activestate.com/komodo-edit
I use the non-free Komodo-ide, which is based on the free editor.
--
DaveA
More information about the Tutor
mailing list