math module for Decimals

Gabriel Genellina gagsl-py2 at
Sun Dec 28 03:33:36 CET 2008

En Sat, 27 Dec 2008 22:02:51 -0200, <jerry.carl.mi at> escribió:

> I have been looking for a Python module with math functions that would
> both eat and spit Decimals. The standard math module eats Decimals
> allright but spits floats... herefore exp(sin(Decimal())) produces exp
> () of a float :-(

Which math functions? ln, log10, exp, sqrt already exist as methods of  
Decimal instances. At the end of the Decimal docs there are a few  
examples, including computing sin and cos (but apparently they naïvely use  
a McLaurin series like you noticed in other module).
There is a way to convert a float into a Decimal object with no loss in  
precision, see the FAQ (using float.as_integer_ratio(), I think such  
function was implemented for exactly this use case)
So you might convert to float, operate, and go back -- if the precision of  
"double" operands is enough for you.

Gabriel Genellina

More information about the Python-list mailing list