Problem

Operator Precedence. Think about what the results would be for the following expressions, and then type them into the terminal to verify your answers. Briefly explain the results for each one.

>> 1\2 
>> 1/2
>> int8(1/2)
>> int8(1/3)
>> -5^2  
>> -5**2  
>> (-5) ^ 2 
>> (-5) ** 2 
>> 10-6/2 
>> 5*4/2*3

Solution

MATLAB

Here is the output in MATLAB:

>> 1\2
ans =
     2
>> 1/2
ans =
    0.5000
>> int8(1/2)
ans =
  int8
   1
>> int8(1/3)
ans =
  int8
   0
>> -5^2
ans =
   -25
>> (-5)^2
ans =
    25
>> 10-6/2
ans =
     7
>> 5*4/2*3
ans =
    30
Python

The same results as for MATLAB do not hold in Python.

Comments