why the subtraction gives the wrong ans.?

16 views (last 30 days)
Sultan Mehmood
Sultan Mehmood on 16 Jul 2019
Answered: Walter Roberson on 16 Jul 2019
y=uint16(22);
>> Z=uint16(164);
>> S=y-Z
S =
0
how i get an ans = -142
  2 Comments
Shashank Sharma
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.

Sign in to comment.

Answers (2)

Shashank Sharma
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);

Walter Roberson
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.

Community Treasure Hunt

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

Start Hunting!