I am running a script in Matlab R2020b. The script contains an array with following values:
a=[500, 500, 500, 1000, 750, 750, 567.79 613.04]
The script as an equation:
(a(1)*(a(8)-a(6)) a(7)*(a(6)-a(2)) a(5)*(a(2)-a(8)))
When running on Matlab the above equation gives the answer -11312 for the values of array a. But when I calculate each value separately and add them the Matlab compiler gives a different answer.
a(1)*(a(8)-a(6)) = -68480
a(7)*(a(6)-a(2)) = 1.419e 05
a(5)*(a(2)-a(8)) = -84780
>>(-68480) (1.419e 05) (-84780)
the answer for the above is -11310.
A screenshot of the commands is also attached.
kindly tell me why Matlab compiler gives these different answers??
CodePudding user response:
The problem is that MATLAB's default format is 'short'
, and this is not showing you complete precision. Try format long
.
>> format long
>> a(7)*(a(6)-a(2))
ans =
1.419475000000000e 05
CodePudding user response:
You are wrong.
If you add format long g
you can see the real numbers:
format long g
a=[500, 500, 500, 1000, 750, 750, 567.79 613.04]
res1=(a(1)*(a(8)-a(6)) a(7)*(a(6)-a(2)) a(5)*(a(2)-a(8)))
a2=a(7)*(a(6)-a(2))
a1=a(1)*(a(8)-a(6))
a3=a(5)*(a(2)-a(8))
res2=a1 a2 a3
results in:
res1 =
-11312.5
a2 =
141947.5
a1 =
-68480
a3 =
-84780
res2 =
-11312.5