Difference between value of sum obtained from program as compared to and the one obtained from command window

I am adding four very small numbers in my program. The issue I am facing is the sum obtained from the program "obj" is coming out to be different than when I add the same four numbers in the command window. Why is the sum getting calculated wrongly when I run the program? How to fix it?
The green box displays the expected value of the sum while the red box has the incorrect result of adding I1t, I2t, I3t and I4t.

4 Comments

Please share the code and the values used to obtain the output obj.
Note that the values displayed are not the same as the values stored.
Hello Dyuman,
obj is a simple addition of the four values, I1t, I2t, I3t and I4t.
Thank you for that advice , I added the complete numbers in the long decimal format and the value of obj obtained from the program checks out.
Sharing the values as seen in workspace
obj = 0.000000013021625
I1t = -0.001335458063828
I2t = 0.000057425539310
I3t = 0.000971336765721
I4t = 0.000306708780422
Sum of above 4 numbers obained in command window:
1.3022e-08
"The issue I am facing is the sum obtained from the program "obj" is coming out to be different than when I add the same four numbers in the command window."
The only issue here is that you are adding different numbers.
"Why is the sum getting calculated wrongly when I run the program?"
It is not wrong: when you add different numbers you will get a different result:
sum([-0.001335458063828,0.000057425539310,0.000971336765721,0.000306708780422])
ans = 1.3022e-08
sum([-0.0013,5.7426e-5,9.7134e-4,3.0671e-4])
ans = 3.5476e-05
So far absolutely no surprises.
"How to fix it?"
What is broken?
If you want to add exactly the same numbers then you will need to add exactly the same numbers. Adding exactly the same numbers does NOT mean adding some other numbers with a much lower precision.
Hi Stephen,
Thank you for the explanation, was kind of a brainfade moment for me to be checking the sum of the displayed variables instead of the "actual" values.

Sign in to comment.

Answers (1)

Hi Chinmay,
I understand that you are attempting to add four numbers using two distinct methods. Let's execute both approaches now to comprehend the differences between them.
Case 1:
I1t = -0.001335458063828;
I2t = 0.000057425539310;
I3t = 0.000971336765721;
I4t = 0.000306708780422;
%obj = 0.000000013021625
obj=I1t+I2t+I3t+I4t
obj = 1.3022e-08
Case 2:
I1t = -0.0013;
I2t = 0.000057426;
I3t = 0.00097134;
I4t = 0.00030671;
%obj = 3.5476e-05
obj=I1t+I2t+I3t+I4t
obj = 3.5476e-05
Now you can clearly discern the difference between the two additions due to the variance in precision. This is the reason why you received different outputs in both cases. I hope this clarifies your doubt!

1 Comment

Hello Sai,
Thank you for the clarification. Was wrong on my part to be comparing the sum of the displayed variables in the command window with that of the actual values.

Sign in to comment.

Products

Release

R2023b

Asked:

on 21 Jan 2024

Commented:

on 21 Jan 2024

Community Treasure Hunt

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

Start Hunting!