FixedPoint Maths in .Net using IronPython

I was just messing with IronPython and noticed that there was a Python FixedPoint library. Fixed point maths is at odds with the more generally used IEEE floating point standard. In-fact floating point Maths is so much more common that most modern CISC processors have an FPU for these operations.

The idea behind floating point is to abstract away the concept of the an integral and quotient parts of a real number. However, it cannot abstract away the reality of processor design; where registers have a fixed and finite number of bits typically 32 or 64. To see how bad an approximation float, double and even decimal make of real-number operations doesn’t take much effort:

For example, the following:

Update: The code snippet below has a typo which prints out the wrong value for decimal. (Pointed out by Stu in comments). Decimal has 128-bits of precision making far more suitable for financial applications.

Floating Point Test

Will output this:

Floating Point Output

Ouch, not very precise. In fact float falls over at 1000 iterations of 0.001 because of it smaller 32-bit precision.

This is a trumped-up example but it makes the point and these discrepancies are even greater when divisions or multiplications are done of numbers that vary greatly in magnitude.

So gaving floatingpoint a try in Python:

Python Fixed Point output

Much better! My fp object has ruthlessly ignored the first addition because it fell outside its precision of 6, but has exactly the correct value after the summation. So whats the cost of this accuracy? Speed; the above takes many seconds to execute.

The above example involves a million fixed point additions which may seem silly but in a financial setting Monte Carlo simulation models (to name just one) rely on executing many millions of calculations as quickly as possible.


When you need speed; go for floating point. When you need accuracy; go for fixed.

If you need both… you might be in trouble.


4 Responses to FixedPoint Maths in .Net using IronPython

  1. sanxiyn says:

    FYI, FixedPoint should be considered deprecated in favor of decimal, which is now included in the standard Python distribution.

    Python decimal module is a faithful implementation of IEEE 854 standard, and is tested against comprehensive test suite provided by IBM.

  2. Jan Bannister says:

    Thanks for the info sanxiyn. I’ll check out decimal.

  3. Stu Thompson says:

    There is a bug in the code. The thrid write line is printing the double, not the decimal as you inteded. That would impact your output.

  4. Jan Bannister says:

    Thanks Stu, you’re completely right. In fact I was a bit shocked that decimal preformed so badly. I should have looked a bit more carefully!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: