XPath/XQuery numeric-integer-divide function

Summary

Performs an integer division.

Operator Mapping

Defines the semantics of the "idiv" operator when applied to two numeric values.

Signature

op:numeric-integer-divide(
$arg1 as xs:numeric,
$arg2 as xs:numeric
) as xs:integer

Rules

General rules: see .

If $arg2 is INF or -INF, and $arg1 is not INF or -INF, then the result is zero.

Otherwise, subject to limits of precision and overflow/underflow conditions, the result is the largest (furthest from zero) xs:integer value $N such that the following expression is true:

fn:abs($N * $arg2) le fn:abs($arg1) 
               and fn:compare($N * $arg2, 0) eq fn:compare($arg1, 0).

The second term in this condition ensures that the result has the correct sign.

The implementation may adopt a different algorithm provided that it is equivalent to this formulation in all cases where implementation-dependent or implementation-defined behavior does not affect the outcome, for example, the implementation-defined precision of the result of xs:decimal division.

Examples

The expression op:numeric-integer-divide(10,3) returns 3.

The expression op:numeric-integer-divide(3,-2) returns -1.

The expression op:numeric-integer-divide(-3,2) returns -1.

The expression op:numeric-integer-divide(-3,-2) returns 1.

The expression op:numeric-integer-divide(9.0,3) returns 3.

The expression op:numeric-integer-divide(-3.5,3) returns -1.

The expression op:numeric-integer-divide(3.0,4) returns 0.

The expression op:numeric-integer-divide(3.1E1,6) returns 5.

The expression op:numeric-integer-divide(3.1E1,7) returns 4.

Error Conditions

A dynamic error is raised if the divisor is (positive or negative) zero.

A dynamic error is raised if either operand is NaN or if $arg1 is INF or -INF.

Notes

Except in situations involving errors, loss of precision, or overflow/underflow, the result of $a idiv $b is the same as ($a div $b) cast as xs:integer.

The semantics of this function are different from integer division as defined in programming languages such as Java and C++.