How does division "/" = work for integer types?

조회 수: 212 (최근 30일)
Travis Cuvelier
Travis Cuvelier 2021년 6월 16일
편집: Paul 대략 13시간 전
In doing some work with integers in R2021A I noticed something relatively strange, namely that if A and B are unsigned integers and A >= B/2, then A/B = 1. Before today, I was not aware of this "rounding". In fact this doesn't quite jive with the documentation. In particular, uint32(1)/uint32(2) gives 1, wheras bitshift(uint32(1),-1) gives 0. Per the documentation https://www.mathworks.com/help/matlab/ref/bitshift.html, a bitshift of -1 should give the same result as dividing by 2. Is this expected behavior? I've attached a screenshot.

채택된 답변

Chunru
Chunru 2021년 6월 16일
편집: Chunru 2021년 6월 16일
MATLAB rounds the results when dividing integer data types. That is to say:
uint32(1) /uint32(2) = round(1/2) = round(0.5) =1
uint32(1)/uint32(2)
ans = uint32 1
uint32(10)/uint32(3)
ans = uint32 3
uint32(5)/uint32(2)
ans = uint32 3
  댓글 수: 3
Walter Roberson
Walter Roberson 2023년 2월 17일
Integer division has no "natural" behaviour for remainders (except perhaps to offer to return the remainder separately).
What does "truncate" mean, exactly? Is it "round towards 0", or is it "floor" ? And should the result be different for 5/-2 than for -5/2 ?
TylerTK
TylerTK 2023년 2월 17일
편집: TylerTK 2023년 2월 17일
I disagree. The natural behavior on processor system and in math courses would be drop fractional values (round toward 0). 1 doesn't consist of 1 group of 2.

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

추가 답변 (2개)

Steven Lord
Steven Lord 2021년 6월 16일
See the "Creating Integer Data" and "Arithmetic Operations on Integer Classes" sections on this documentation page.

uno
uno 2023년 2월 17일
Stumbled on this since I've just encontered the same issue. Totally nuts and I believe should be fixed.
Seems at the moment the solution is to use the idivide() function.
  댓글 수: 13
Walter Roberson
Walter Roberson 대략 14시간 전
"Also given that, in the 1980s, Matlab was programmed in C"
It was programmed in a mix of FORTRAN and C -- heavier on the FORTRAN at the time.
Paul
Paul 대략 13시간 전
편집: Paul 대략 13시간 전
FWIW, the FORTRAN 77 standard (Matlab using FORTRAN 77 at that time) was (still is?):
" 6.1.5 Integer_Division. One operand of type integer may be divided by another operand of type integer. Although the mathematical quotient of two integers is not necessarily an integer, Table 2 specifies that an expression involving the division operator with two operands of type integer is interpreted as an expression of type integer. The result of such a division is called an integer quotient and is obtained as follows: If the magnitude of the mathematical quotient is less than one, the integer quotient is zero. Otherwise, the integer quotient is the integer whose magnitude is the largest integer that does not exceed the magnitude of the mathematical quotient and whose sign is the same as the sign of the mathematical quotient. For example, the value of the expression (-8)/3 is (-2)."
Though, of course, the integer data types in Matlab came much later.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by