storing value in a variable

54 次查看(过去 30 天)
According to my knowledge, the in x we can store values from -127 to 127. then why on executing this code, x=250 and x=3*10^12 is getting stored inx?
int8 x
intmax("int8")
x=35
x=250
x=30000000000000000000

采纳的回答

Stephen23
Stephen23 2020-9-7
编辑:Stephen23 2020-9-7
It looks like you are trying to declare a variable type. MATLAB does not have variable type declarations.
This is what your MATLAB code is actually doing:
int8 x % convert the character 'x' to int8.
intmax("int8") % call INTMAX for "int8".
x=35 % define a double with value 35, give it the variable name x
x=250 % define a double with value 250, give it the variable name x
x=30000000000000000000 % define a double with value 3e20, give it the variable name x
None of these commands have anything to do with each other. None of them have any effect on each other.
The first two lines assign their outputs to the default ans. The behavior of the first line is explained here:
Here are the two common ways in MATLAB of converting a value to a particular class, either specify the class:
>> x = int8(250)
x = 127
or using indexing to assign to an existing array of that class:
>> x = zeros(1,1,'int8');
>> x(1) = 250
x = 127

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by