# Sum block not giving proper output

1 view (last 30 days)
Rajesh Metkar on 24 Jun 2022
Commented: Steven Lord on 30 Jun 2022
I am using Sum block to subtract 2 signal. Even if both the inputs of sum block are having same value as 2, Sum block is not giving output value as zero, instead Sum block is giving output value as 4.44089209850063E-16.
Data type of both the input signals is 'double'. Sum blocks Output data type and Accumulator data type is 'Inherit: Inherit via internal rule'.

Steven Lord on 24 Jun 2022
Even if both the inputs of sum block are having same value as 2
From the result you described apparently the signals are not both equal to 2. They may be very close, so close that they are displayed as 2, but the value stored in the variable or signal is not exactly equal to 2 down to the last bit. For example, the value of x displays as 2 in the short display format:
x = 2 - 1e-15
x = 2.0000
and in the long g display format:
format longg
x
x =
2
But it is not exactly 2.
x == 2 % false
ans = logical
0
It is, as you'd expect from the way x was constructed, slightly less than 2.
difference = 2 - x
difference =
1.11022302462516e-15
##### 2 CommentsShowHide 1 older comment
Steven Lord on 30 Jun 2022
Please try this little experiment. Find something to write with and something to write on (ideally compatible things; pencil and paper not pencil and whiteboard.)
Step 1: Using long division (like you learned in school) divide 1 by 3. Call the result x. You are allowed to write as many decimal places of the result as you want, but only those you explicitly write can be used in step 2. No using 0.3 repeating to get "an infinite" number of places.
Step 2: Multiply x by 3. Call the result y.
In exact arithmetic we know (1/3)*3 is exactly 1. But the x value you defined in step 1 is not one third. It is slightly smaller than one third because you rounded off one third to fit it into x. If you've written one more decimal place in step 1 you'd have an x that's closer to one third than the x you actually used in step 2. Therefore y will not be 1. The value stored in y will be slightly smaller than 1.
Is that a bug in either long division or long multiplication? No.
Is there any way to know the fractional value(exact value) which display block is not able to display properly.
This question assumes that the display block is not working correctly. That's likely an invalid assumption. The display block is displaying the value it's given correctly. It may not display the value you expect it was given but that's not a problem with the display block or necessarily with the code that computes the values for the display block. In the example above you expected y to be 1 but it was actually slightly smaller than 1.
To check the value that you have, look at the hex representation or subtract the value from 2 (as I did with the last line in my answer.)
x = 2 - 1e-15
x = 2.0000
y = 2
y = 2
difference = y-x
difference = 1.1102e-15
Since difference is not 0, x and y are not equal. Let's look at their bit patterns.
format hex
x
x =
3ffffffffffffffb
y
y =
4000000000000000
The patterns for x and y are very close, with only a difference in the last couple bits.
format longg
[fractionX, exponentX] = log2(x)
fractionX =
0.999999999999999
exponentX =
1
x is just a little less than 2^1.
[fractionY, exponentY] = log2(y)
fractionY =
0.5
exponentY =
2
format hex
fractionY
fractionY =
3fe0000000000000
0.5
ans =
3fe0000000000000
y is exactly 0.5 * 2^2, or in other words it is 2^1.
Also what could be the reason for the signal not taking value 2 ,but taking the fractional value which is close to 2?
There are many possibilities. Without seeing what you did to calculate those values it's impossible to say for sure.

R2016b

### Community Treasure Hunt

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

Start Hunting!