Is this a logical Error in Matlab ? exp (pi*i*6) = 1.000000000000000 - 0.000000000000001i. WHY an i component ?, should just be 1

2 visualizzazioni (ultimi 30 giorni)
Is this a Logical Error in Matlab ?
exp (pi*i*6) = 1.000000000000000 - 0.000000000000001i.
WHY an i component ?, shouldnt the answer just be 1?
Please answer.
  1 Commento
Stephen23 il 19 Feb 2024
Modificato: Stephen23 il 19 Feb 2024
"Is this a Logical Error in Matlab ?"
Probably not.
"WHY an i component ?, shouldnt the answer just be 1?"
The answer may be exactly one if you are doing algebraic/symbolic mathematics:
ans = 
But you did not tell your computer to do symbolic mathematics, you gave it a numeric task using numeric operations. All numeric operations have precision limited by their data types, take finite speed, have finite domains of validity. And when working with binary floating point numbers they easily accumulate floating point error.
The only "logical error" here is assuming that your computer has infinite precision when doing numeric calculations.

Accedi per commentare.

Risposta accettata

John D'Errico
John D'Errico il 19 Feb 2024
Modificato: John D'Errico il 19 Feb 2024
Remember that pi in MATLAB is not truly pi, any more than pi is 3.14, or even 3.14159. pi is a transcendental number. There are infinitely many digits in pi.
format long
ans =
And, yes, that looks vagely like pi. Many digits, but once we get past the first 16 digits or so, we need to recognize that pi there has only the first 16 digits correctly represented. We have an error in the least significant bits, as there must be in any finite representation of a transcendental number.
Double precision numbers are stored in a BINARY form. So they have 52 binary bits. In the case of pi, the binary form would look like
So there, the ones represent powers of 2. But the problem is, the true binary form for pi goes on forever, whereas in MATLAB, it stops at that point.
The result of all this is that pi as a double precision number is not truly pi. Close. But not truly pi, any more than pi==3.14. And if we try that simple aproximation for pi, we see the problem far more obviously.
ans =
0.999954342529211 - 0.009555776105247i
Of course you would not expect that result to be exact. But what you did was essentially the same thing.
If you want MATLAB to represent pi as the transcendental number pi, then you need to do something like this
ans = 
where MATLAB now understands you want to use the actual number pi, instead of a floating point approximation to pi.
  5 Commenti
Torsten il 22 Feb 2024
Modificato: Torsten il 22 Feb 2024
ans = 
More about the choice of precision can be found here:

Accedi per commentare.

Più risposte (2)

Steven Lord
Steven Lord il 19 Feb 2024
πis a transcendental number. pi is the double precision approximation to that number. There are some functions in MATLAB that let you operate as though you were using πinstead of pi (like sinpi and cospi) but exp is not one of them.
So if you wanted to compute this without the roundoff error caused by using pi instead of π, you could use the identity .
format longg
y = [exp(6i*pi); ...
cos(6*pi)+1i*sin(6*pi); ...
y =
1 - 7.34788079488412e-16i 1 - 7.34788079488412e-16i 1 + 0i

Sulaymon Eshkabilov
Sulaymon Eshkabilov il 19 Feb 2024
It is all about a display format:
format short
ans = 1.0000 - 0.0000i
% compare with this:
ans = 1.0000 - 0.0000i
% Compare this:
format long
[exp(pi*1i*6); cos(6*pi)+1i*sin(6*pi)]
ans =
1.000000000000000 - 0.000000000000001i 1.000000000000000 - 0.000000000000001i




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by