Product of two real numbers gives (sometimes!) a complex number?

조회 수: 5 (최근 30일)
Maria
Maria 2014년 11월 27일
편집: Roger Stafford 2014년 11월 27일
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

채택된 답변

Roger Stafford
Roger Stafford 2014년 11월 27일
편집: Roger Stafford 2014년 11월 27일
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개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

제품

Community Treasure Hunt

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

Start Hunting!

Translated by