# why the subtraction gives the wrong ans.?

조회 수: 14(최근 30일)
Sultan Mehmood 2019년 7월 16일
답변: Kiran Kintali 2023년 2월 7일
y=uint16(22);
>> Z=uint16(164);
>> S=y-Z
S =
0
how i get an ans = -142
##### 댓글 수: 2표시 이전 댓글 수: 1숨기기 이전 댓글 수: 1
Shashank Sharma 2019년 7월 16일
편집: Shashank Sharma 2019년 7월 16일
You are using uint all such integers are unsigned.
You cannot store negative numbers in a uint type.

댓글을 달려면 로그인하십시오.

### 답변(3개)

Shashank Sharma 2019년 7월 16일
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표시 이전 댓글 수: -1숨기기 이전 댓글 수: -1

댓글을 달려면 로그인하십시오.

Walter Roberson 2019년 7월 16일
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표시 이전 댓글 수: -1숨기기 이전 댓글 수: -1

댓글을 달려면 로그인하십시오.

Kiran Kintali 2023년 2월 7일
Integers in MATLAB have Saturation behaviors.
To avoid saturation behavior of Integers when using MATLAB Code, you may need to consider the following code generation options or consider using fixed-point math for finer control on the numerics.
By default (and when the "Saturation on integer overflow" is checked in MATLAB coder settings), when an arithmetic operation involving integers exceeds the maximum or minimum value, the result is set to the maximum or minimum value. This is known as "saturation"
For example, if we consider 8-bit signed integers with a maximum value of 127 and a minimum value of -128:
>> x = int8(127) + 1
x =
int8
127
>> x = int8(-128) - 1
x =
int8
-128
>> x = int8(-128) - 8
x =
int8
-128
When the "Saturation on integer overflow" property is unchecked, then any amount that overflows or exceeds the maximum or minimum value is wrapped around to the opposite extreme. This is known as "wrapping". For example:
>> x = int8(127) + 1
x =
int8
-128
>> x = int8(-128) - 1
x =
int8
127
>> x = int8(-128) - 8
x =
int8
120
Please note that this checkbox applies only to MATLAB code generation, and not to executing MATLAB files or executing MATLAB commands in the Command Window. For more information regarding this behavior, see the following documentation:

댓글을 달려면 로그인하십시오.

### 범주

Find more on Automated Fixed-Point Conversion in MATLAB in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by