numerical error when dividing some identical complex number

I wanted to calculate the angular difference (e.g. 155 deg vs. 20 deg) by dividing the exponential angular representation (exp(1j*theta1)./exp(1j*theta2) to achieve this goal. While doing this, I found some numerical error for certain complex number.
For instance, exp(1j*0.5)./exp(1j*0.5)=1 (as it should be be). However, exp(1j*0.5166)./exp(1j*0.5166)=1.000+ 4.8e-17j. The imaginary component is tiny, but non-zero. It seems like a numerical precision issue. I can fix the problem by applying some threshold, but I'm just curious why this problem occurs only in some number, and not all complex number. Does anyone know it? Just out of curiosity.
Here is my MATLAB version info
-----------------------------------------------------------------------------------------------------
MATLAB Version: 9.13.0.2049777 (R2022b)
Operating System: Linux 5.10.0-19-amd64 #1 SMP Debian 5.10.149-2 (2022-10-21) x86_64
Java Version: Java 1.8.0_202-b08 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
-----------------------------------------------------------------------------------------------------
Thanks!
Misun

2 件のコメント

Paul
Paul 2022 年 11 月 2 日
編集済み: Walter Roberson 2022 年 11 月 3 日
Misun Kim
Misun Kim 2022 年 11 月 3 日
Thanks a lot @Paul for directing me to the previous discussion. This answers to my question.

サインインしてコメントする。

 採用された回答

Misun Kim
Misun Kim 2022 年 11 月 3 日

0 投票

@Paul provides the link to the previous discussion which answers to my question on dividing complex number. For the ease of finding it, I place the link here again. https://www.mathworks.com/matlabcentral/answers/548010-why-is-a-number-divided-by-itself-not-equal-to-unity

その他の回答 (1 件)

Steven Lord
Steven Lord 2022 年 11 月 2 日

0 投票

Are you certain the two numbers you're using are identical down to the last bit, not just down to the last displayed digit?
x = 0.1234
x = 0.1234
y = 0.12341
y = 0.1234
Despite x and y being displayed the same, they are not the same stored value. False is the correct answer for this comparison that checks the stored value not the display.
areTheyIdentical = x == y
areTheyIdentical = logical
0
Does that small difference between x and y make a difference in the exponentials?
ex = exp(1i*x)
ex = 0.9924 + 0.1231i
ey = exp(1i*y)
ey = 0.9924 + 0.1231i
They display the same, but is the answer exactly 1? No.
ex/ey
ans = 1.0000 - 0.0000i
Let's display the three values with a few more decimal places
format longg
[ex; ey; ex/ey]
ans =
0.992395876704891 + 0.123087058211376i 0.992394645784689 + 0.123096982163989i 0.99999999995 - 9.99999999984743e-06i
That small difference between x and y resulted in a difference in ex and ey.

3 件のコメント

Misun Kim
Misun Kim 2022 年 11 月 2 日
Yes, I already check whether two values are identical (not just shown identical on the display), but the exponential complex number is different, when divided by each other.
x=0.5166; y=0.5166;
x==y
ans = logical
1
Then, I check exp(1i*x)./exp(1i*y)
ex=exp(1i*x)
ex = 0.8695 + 0.4939i
ey=exp(1i*y)
ey = 0.8695 + 0.4939i
ex./ey
ans = 1.0000 + 0.0000i
imag(ex./ey)
ans = 4.8267e-17
ex-ey
ans = 0
If I repeated the process with some other value, it returns clean 1.
x=0.5; y=0.5;
x==y
ans = logical
1
ex=exp(1i*x)
ex = 0.8776 + 0.4794i
ey=exp(1i*y)
ey = 0.8776 + 0.4794i
ex./ey
ans = 1
imag(ex./ey)
ans = 0
So, it's some kind of numerical precision error with particular value.
More compactly:
format long g
x=0.5166
x =
0.5166
ex = exp(1i*x)
ex =
0.869503552901398 + 0.493926686353193i
ex./ex
ans =
1 + 4.82671432212255e-17i
This is not what I would have expected
Paul
Paul 2022 年 11 月 2 日
Check the link in the comment to the Question.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeFunction Creation についてさらに検索

製品

リリース

R2022b

質問済み:

2022 年 11 月 2 日

回答済み:

2022 年 11 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by