MATLAB Answers

0

why the subtraction gives the wrong ans.?

Asked by Sultan Mehmood on 16 Jul 2019
Latest activity Answered by 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

KSSV
on 16 Jul 2019
You are asking same question repeatedly........
You are using uint all such integers are unsigned.
You cannot store negative numbers in a uint type.

Sign in to comment.

2 Answers

Answer by 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

Sign in to comment.


Answer by 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

Sign in to comment.