Filter löschen
Filter löschen

Division int32 numbers in MATLAB R2019b

2 Ansichten (letzte 30 Tage)
vadim onuchin
vadim onuchin am 27 Okt. 2020
Kommentiert: Steven Lord am 27 Okt. 2020
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++?

Akzeptierte Antwort

vadim onuchin
vadim onuchin am 27 Okt. 2020
Bearbeitet: vadim onuchin am 27 Okt. 2020
So, I found solution.
Need to use special division:
idivide(int32(1140), int32(32))
ans =
int32
35
  2 Kommentare
Bruno Luong
Bruno Luong am 27 Okt. 2020
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 am 27 Okt. 2020
FYI the way MATLAB performs arithmetic on integers is documented here.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Cris LaPierre
Cris LaPierre am 27 Okt. 2020
Bearbeitet: Cris LaPierre am 27 Okt. 2020
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.

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by