XPath/XQuery numeric-mod function

Summary

Returns the remainder resulting from dividing $arg1, the dividend, by $arg2, the divisor.

Operator Mapping

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

Signature

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

Rules

General rules: see .

The operation a mod b for operands that are xs:integer or xs:decimal, or types derived from them, produces a result such that (a idiv b)*b+(a mod b) is equal to a and the magnitude of the result is always less than the magnitude of b. This identity holds even in the special case that the dividend is the negative integer of largest possible magnitude for its type and the divisor is -1 (the remainder is 0). It follows from this rule that the sign of the result is the sign of the dividend.

For xs:float and xs:double operands the following rules apply:

  • If either operand is NaN, the result is NaN.

  • If the dividend is positive or negative infinity, or the divisor is positive or negative zero (0), or both, the result is NaN.

  • If the dividend is finite and the divisor is an infinity, the result equals the dividend.

  • If the dividend is positive or negative zero and the divisor is finite, the result is the same as the dividend.

  • In the remaining cases, where neither positive or negative infinity, nor positive or negative zero, nor NaN is involved, the result obeys (a idiv b)*b+(a mod b) = a. Division is truncating division, analogous to integer division, not rounding division i.e. additional digits are truncated, not rounded to the required precision.

Examples

The expression op:numeric-mod(10,3) returns 1.

The expression op:numeric-mod(6,-2) returns 0.

The expression op:numeric-mod(4.5,1.2) returns 0.9.

The expression op:numeric-mod(1.23E2, 0.6E1) returns 3.0E0.

Error Conditions

A dynamic error is raised for xs:integer and xs:decimal operands, if $arg2 is zero.