div
Syntax
div(X, Y) or X/Y
Arguments
X / Y is a scalar/pair/vector/matrix. If X or Y is a pair/vector/matrix, the other is a scalar or a pair/vector/matrix of the same size.
Details
Return element-by-element division of X by Y. When X or Y is float/double, it returns
float/double. When both X and Y are integers, div
means integer
division, which is the same as applying the floor function after division. For example, 5/2 is 2.
If you want "true" division for integers, you can apply the operator ratio "" instead. Integer division
is often used together with the operator mod for grouping data. The results of ## div and ## mod should satisfy
the relationship: X=div(X,Y)*Y+mod(X,Y). In practice, it does not make much sense to
divide X by negative integer(s). Therefore, we require Y must be positive when both
X and Y are integers. Otherwise, the calculation returns a NULL value.
Examples
9/2:5;
// output
4 : 1
11:25/3:4;
// output
3 : 6
x=1 2 3;
x/2;
// output
[0,1,1]
2/x;
// output
[2,1,0]
y=4 5 6;
x/y;
// output
[0,0,0]
y/x;
// output
[4,2,2]
m1=1..6$2:3;
m1;
#0 | #1 | #2 |
---|---|---|
1 | 3 | 5 |
2 | 4 | 6 |
m1/2;
#0 | #1 | #2 |
---|---|---|
0 | 1 | 2 |
1 | 2 | 3 |
m2=6..1$2:3;
m2;
#0 | #1 | #2 |
---|---|---|
6 | 4 | 2 |
5 | 3 | 1 |
m1/m2;
#0 | #1 | #2 |
---|---|---|
0 | 0 | 2 |
0 | 1 | 6 |
-7/5;
// output
-2
// when the denominator is less than or equal to 0, return NULL values
x=-1 2 3;
x/-5;
// output
[,,]