idivide

Integer division with a chosen rounding rule.

Category: Language Fundamentals > Operators and Elementary Operations

Syntax

C = idivide(A, B)

Divides and rounds toward zero ('fix').

C = idivide(A, B, op)

Uses the rounding op: 'fix', 'floor', 'ceil' or 'round'.

Description

The / and ./ operators on integers always round to the nearest value, so int32(7)./int32(2) is 4. idivide lets you pick the rule instead: 7/2 = 3.5 gives 3 with 'fix' or 'floor', 4 with 'ceil' or 'round'.

For negative quotients the rules differ: -3.5 becomes -3 with 'fix' and 'ceil', and -4 with 'floor' and 'round'. Division by zero saturates.

Input arguments

A, B
Arrays of which at least one is an integer type. Sizes must be compatible.
op
Rounding rule. The default is 'fix'.

Output arguments

C
Quotient with the integer class of the inputs.

Example

a = int32(7);
b = int32(2);
idivide(a, b)
idivide(a, b, 'ceil')
idivide(-a, b, 'floor')
a ./ b
Output
ans = int32
   3
ans = int32
   4
ans = int32
   -4
ans = int32
   4

See also

floor fix mod rem ./