numerical error when dividing some identical complex number

4 views (last 30 days)
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

Accepted Answer

Misun Kim
Misun Kim on 3 Nov 2022
@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

More Answers (1)

Steven Lord
Steven Lord on 2 Nov 2022
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 Comments
Walter Roberson
Walter Roberson on 2 Nov 2022
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

Sign in to comment.

Categories

Find more on Function Creation in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!