The Voidspace Techie Blog

I feel for you. I went through the same thing recently trying to implement a class for currencies as well. You'll also soon realize when you do some profiling that Decimal is painfully slow. We ended up subclassing object and having an int field that represents the amount of cents.


For fast floating point arithmetic in Python I suggest to use:

http://code.google.com/p/mpmath/


Gravatar @Anonymous - but that's a floating point library right? Floating point is not appropriate for financial calculations.

Which class in particular were you suggesting I use as the base for my use case?


Gravatar Hmmm... actually mpmath does look interesting. I wonder if it works with IronPyton? (I can see it defaults to 'long' as the base numeric type if 'gmpy' isn't availble.)


Gravatar The Decimal module is slow, yes. A couple years ago during the "Need for Speed" sprint Georg Brandl and I tried to write a version of it in C. As it turns out the interface has a couple quirky python-isms in the interface (settings and counts held in dicts, ick) so it can't be easilly translated into fast C. A slightly cleaner interface will be required and because it will be different it won't be a drop-in for the existing Decimal module (Hettinger agrees - he did the original and isn't responsible for the clunky bits).

So far no one has cared enough to write and submit a tidied replacement or sponsor someone to do it.

PS, the comment box is defined in pixels so if you enlarge the text it only fits a couple words per line. My resolution is setup for powerpoint so this is painful.


Gravatar @Jack
Sorry about comment box size - I use a larger font so it annoys me as well. I should fix it.

A C based Decimal wouldn't help me as this project is for Resolver One which runs on IronPython.

I don't yet have performance problems though, I haven't even got it running. For my case the answer is probably just to use the .NET Decimal structure and delegate rather than inherit - as I mentioned there are drawbacks to this approach...

I could probably inherit from the Python Decimal but delegate to the .NET one, but that would be at least slightly evil...


Name:

Email:

URL:

Comment:  ? 

 

Commenting by HaloScan