math module for Decimals
dickinsm at gmail.com
Sun Dec 28 15:33:37 CET 2008
On Dec 28, 12:02 am, jerry.carl... at gmail.com wrote:
> 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.
Yes: it just converts the input (whether float, int, Fraction or
Decimal) to a float using the __float__ method, and then
applies the usual float->float maths function.
> I tried using the AJDecimalMathAdditions, but ran into issues like dSin
> (1E4) would take forever to calculate and would result in sin() >
> 1 ... If i understand it correctly, the program is using maclaurin
> series to calculate the value and since it does not chop off all the
> multiples of 2*pi, the maclaurin approximation becomes useless when
> its too far from x=0.
Implementing these functions *is* kinda tricky, especially if you
want them to be correctly rounded, efficient, and to deal correctly
with all the special cases (infinities, nans, ...). But it's far
from impossible. sin and cos present particular difficulties
for large arguments; probably the range of accepted inputs
would have to be restricted for those functions.
> (1) what do folks use when they need to calculate something like exp
> (sin(Decimal())) or even more complex things? Any recommendations? Or
> am I completely missing something?
Generally, something other than Python. :) (For example, GP/Pari
or MPFR.) I'd like to be able to use Python for this, though.
A couple of questions for you (I'm curious what additions would
suit your particular use case best):
- are you using Decimal for the base-10-ness or the
extra precision Decimal provides? Or significant zeros?
Or compatibility with existing Decimal code, or what?
- what 3 functions would you most like to see added?
For me, I think it would be something like sin, cos
and atan (or possibly atan2). Once you've got those
three, everything else is fairly easy. In particular,
atan/atan2 at least gives you access to pi.
> (2) Is there any plan to provide a standard python module that would
> do that? (Python 4.0? ;-)
No, there's no such plan. It's highly unlikely that the Decimal
module will grow any extra math functions: it's designed to
stick very closely to the IBM standard. It's not impossible
that extra Decimal functions could be added in some other
module, but it seems fairly unlikely.
FWIW, I'm the author of the current Decimal log, log10, exp
and pow functions, so I'm probably in a fairly good position
to try to implement reasonably high-quality versions of some
other elementary functions (again, just as an external
addition to the decimal module, not as part of the decimal
module itself). This is an itch I've often wanted
scratched, as well. I might just have a go....
(Help in the form of code, tests, suggestions, etc.
would be welcome!)
More information about the Python-list