Product of two real numbers gives (sometimes!) a complex number?
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Maria
il 27 Nov 2014
Modificato: Roger Stafford
il 27 Nov 2014
Hello, I am running some tests. I have a vector called "Test" with the following values:
-8.886108641008270e+10 + 0.000000000000000e+00i
-8.885458690939740e+10 + 0.000000000000000e+00i
-8.735404059143396e+10 + 0.000000000000000e+00i
-8.622482848989987e+10 + 0.000000000000000e+00i
-8.567710315895950e+10 + 9.709734987051802e+09i
-8.567710315895950e+10 - 9.709734987051802e+09i
-8.561242064642362e+10 + 9.919436724860571e+09i
-8.561242064642362e+10 - 9.919436724860571e+09i
-7.721667671602890e+10 + 0.000000000000000e+00i
-6.735989534435745e+10 + 0.000000000000000e+00i
-1.130209647515076e+09 + 0.000000000000000e+00i
Now, if I do
K>> ans1=test(1)*test(2)*test(3)
ans1 =
-6.897226195528216e+32
K>> ans2=test(4)*test(5)*test(6)
ans2 =
-6.410682314585833e+32
If I do
K>> ans1*ans2
ans =
4.421592599137085e+65
But, if I do
K>> test(1)*test(2)*test(3)*test(4)*test(5)*test(6)
ans =
4.421592599137085e+65 - 5.846006549323612e+48i
I get a complex number. Can somebody explain me why it is happening what is happening?
Best,
Maria
0 Commenti
Risposta accettata
Roger Stafford
il 27 Nov 2014
Modificato: Roger Stafford
il 27 Nov 2014
In your computation you are assuming that multiplication with floating point numbers always obeys the associative law of multiplication exactly, but that is not necessarily true. Rounding after each operation can cause small differences in results. Try putting parentheses in your product
( test(1)*test(2)*test(3) ) * ( test(4)*test(5)*test(6) )
and notice the difference.
The point is that because of rounding differences, multiplying a sequence of three or more factors can yield different results if the factors are grouped differently. If you use a variety of different real numbers for test(4), the result of
test(4)*test(5)*test(6)
will sometimes have a non-zero imaginary component. That is because this product is ordinarily performed as though it had parentheses like this
( test(4)*test(5) ) * test(6)
and in the second multiplication you are no longer multiplying complex conjugate numbers, though in theory their product should be real. Rounding differences can lead to a non-zero imaginary part.
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!