8

I started to learn GNU Octave today, and tried the first expression given in the manual

exp(i*pi)

The result is

ans = -1.0000e+000 + 1.2246e-016i

And it seems GNU Scientific Library gives similar results too.

So is this a Octave bug, or general problems of numeric analysis software (symbolic evaluation software will definitely give an answer of exact one)?

James Mertz
  • 26,529

1 Answers1

8

This isn't a bug with either, but due to the way computers perform floating point operations. There is a limited amount of precision any computer can operate with, and so you will sometimes see anomalies like this. While it is possible to write software that can handle this, it would take a lot more computation time and drastically increase memory requirements.

If you look at it, e^(i*pi) returns -1 + 1.2x10^-16i. As you can see, the imaginary component is extremely small (most would consider it negligible, since it's 16 orders of magnitude smaller then the real part). This component is introduced by rounding and precision errors, both with the calculation itself, as well as the stored value of pi since it's irrational (see this link for another example dealing with irrational numbers).

If this calculation error is unacceptable, you should look into math packages which perform symbolic rather then numerical analysis, or ones that use high-precision floating point numbers. The caveats of these are that they will drastically increase your memory requirements, and symbolic analysis is often much slower. Also, higher precision numbers will just shrink the magnitude of the rounding/precision errors, not eliminate them.

Breakthrough
  • 34,847