Monday, August 20, 2007

Universal Floating Point Errors

Steve Holden writes about Euler’s Identity, and how Python can’t quite calculate it correctly. Specifically,
e i π + 1 = 0
However, in Python, this isn’t quite true:
>>> import math
>>> math.e**(math.pi*1j) + 1
1.2246063538223773e-16j
If you note, the imaginary component is quite small: -1 x 10-16.

Python is Steve’s tool of choice, so it’s possible to misread his post and believe that python got the answer wrong. However, the error is fundamental. Witness:
$ ghci
Prelude> :m + Data.Complex
Prelude Data.Complex> let e = exp 1 :+ 0
Prelude Data.Complex> let ipi = 0 :+ pi
Prelude Data.Complex> e
2.718281828459045 :+ 0.0
Prelude Data.Complex> ipi
0.0 :+ 3.141592653589793
Prelude Data.Complex> e ** ipi + 1
0.0 :+ 1.2246063538223773e-16
As I said, it would be possible to misread Steve’s post as a complaint against Python. It is not. As he says:
I believe the results would be just as disappointing in any other language
And indeed they are, thanks to irrational numbers like π and the limitations of IEEE doubles.

Updated: corrected uses of -iπ with the proper exponent, .

6 comments:

ray said...

It's very simple - floats are not reals, and expecting them to always behave like reals is wrong. Expecting them to behave "close enough" for many purposes seems alright to me, though.

chessguy said...

You may also be interested in this video, if you haven't seen it:

http://video.google.com/videoplay?docid=-2285617608766742834

Philip said...

You seem to be using -i instead of i (in both examples) to multiply with pi, unless I'm misreading.

Adam Turoff said...

Philip,

Thanks for the correction. I don't play with imaginary numbers very often.

Updated.

Steve said...

Of course, you insult my friend pi by describing it as merely irrational when in fact it is also transcendental. ray clearly has no poetry in his soul, and would make a good applied mathematician (ray: don't be offended, I regard my own ineptness at applied math as a shortcoming, so I was only joking). chessguy: I am only a few minutes into the video, which I was previously unaware of, and I already know I am going to watch it to the end!

Ray said...

Steve: I've been called worse than "applied mathematician" before!