why the subtraction gives the wrong ans.?
16 views (last 30 days)
Show older comments
y=uint16(22);
>> Z=uint16(164);
>> S=y-Z
S =
0
how i get an ans = -142
2 Comments
Shashank Sharma
on 16 Jul 2019
Edited: Shashank Sharma
on 16 Jul 2019
You are using uint all such integers are unsigned.
You cannot store negative numbers in a uint type.
Answers (2)
Shashank Sharma
on 16 Jul 2019
If you want to store negative it is better to convert it to int16.
This makes it possible to store negative integers.
The format is Y = int16(X);
0 Comments
Walter Roberson
on 16 Jul 2019
double(y) - double(Z)
You could also get away with
int32(y) - int32(Z)
However, int16(y) - int16(Z) will not work reliably: it will have problems if any of the entries exceed 32767.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!