Division int32 numbers in MATLAB R2019b

2 次查看(过去 30 天)
Let's see some examples:
first:
1140 / 32
ans =
35.6250
second:
int32(1140) / int32(32)
ans =
int32
36
third:
int32(1140 / 32)
ans =
int32
36
So, why division with int32 numbers is not equivalent like in C++?

采纳的回答

vadim onuchin
vadim onuchin 2020-10-27
编辑:vadim onuchin 2020-10-27
So, I found solution.
Need to use special division:
idivide(int32(1140), int32(32))
ans =
int32
35
  2 个评论
Bruno Luong
Bruno Luong 2020-10-27
Just wonder what is the risk to overload integer "/" with idivide?
Beside Image Proceesing toolbox where else does MATLAB stock uses integers?
Steven Lord
Steven Lord 2020-10-27
FYI the way MATLAB performs arithmetic on integers is documented here.

请先登录,再进行评论。

更多回答(1 个)

Cris LaPierre
Cris LaPierre 2020-10-27
编辑:Cris LaPierre 2020-10-27
int32 is for storing 32-bit integers. The result of the division is rounded to the closest integer value.
int32(32.5)
ans = int32 33
If you want to control how the rounding is handled, use idivide.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by