div, mod, div-and-mod - number-theoretic integer division
(import (rnrs)) ;R6RS
(import (rnrs base)) ;R6RS
(import (rnrs arithmetic fixnums)) ;R6RS
(import (rnrs arithmetic flonums)) ;R6RS
(div x1 x2)
(mod x1 x2)
(div-and-mod x1 x2)
(div0 x1 x2)
(mod0 x1 x2)
(div0-and-mod0 x1 x2)
(fldiv fl1 fl2)
(flmod fl1 fl2)
(fldiv-and-mod fl1 fl2)
(fldiv0 fl1 fl2)
(flmod0 fl1 fl2)
(fldiv0-and-mod0 fl1 fl2)
(fxdiv fx1 fx2)
(fxmod fx1 fx2)
(fxdiv-and-mod fx1 fx2)
(fxdiv0 fx1 fx2)
(fxmod0 fx1 fx2)
(fxdiv0-and-mod0 fx1 fx2)
These procedures implement number-theoretic integer division and return the
results of the corresponding mathematical operations specified below.
-
(div x1 x2) => x1 div x2
(mod x1 x2) => x1 mod x2
(div-and-mod x1 x2)
=> x1 div x2, x1 mod x2
; two return values
(div0 x1 x2) => x1 div0 x2
(mod0 x1 x2) => x1 mod0 x2
(div0-and-mod0 x1 x2)
=> x1 div0 x2, x1 mod0 x2
; two return values
- Mathematical definition
- These procedures are defined in terms of the mathematical operations div,
mod, div0 and mod0:
- div, mod, div0, and mod0 each accept two real
numbers x1andx2 as operands, where x2 must be
nonzero. div returns an integer, and mod returns a real.
Their results are specified by
-
x1 div x2 = nd
x1 mod x2 = xm
where
x1 = nd x2 + xm
0 ≤ xm < abs(x2)
- div0 and mod0 are like div and mod, except the
result of mod0 lies within a half-open interval centered on zero.
The results are specified by
-
x1 div0 x2 = nd
x1 mod0 x2 = xm
where:
x1 = nd x2 + xm
-abs(x2/2) ≤ xm < abs(x2/2)
- (A note to those who are eager to implement these procedures: the
mathematical definition says essentially nothing about how to calculate
the results).
- Generic variants
- If x1 and x2 are exact, x2 must be nonzero.
- In the cases where the mathematical requirements specified above cannot be
satisfied by any number object, either an exception is raised with
condition type &implementation-restriction(3scm), or
unspecified number objects (one for div, mod, div0 and mod0, two for
div-and-mod and div0-and-mod0) are returned.
- Fixnum variants
- Fx2 must be nonzero. All arguments and return values are
fixnums.
- In the cases where the mathematical requirements above cannot be satisfied
by any fixnum, either an exception is raised with condition type
&implementation-restriction. This happens with (fxdiv
(least-fixnum) -1).
- Flonum variants
- All arguments and return values are flonums.
- In the cases where the mathematical requirements above cannot be satisfied
by any flonum, either an exception is raised with condition type
&implementation-restriction, or unspecified flonums are
returned.
The procedures div-and-mod and div0-and-mod0 (and their variants) return two
values which are number objects.
The div and mod (and their variants) return a single value; a
number object.
The following examples are for the mathematical operators.
123 div 10 = 12
123 mod 10 = 3
123 div -10 = -12
123 mod -10 = 3
-123 div 10 = -13
-123 mod 10 = 7
-123 div -10 = 13
-123 mod -10 = 7
123 div0 10 = 12
123 mod0 10 = 3
123 div0 -10 = -12
123 mod0 -10 = 3
-123 div0 10 = -12
-123 mod0 10 = -3
-123 div0 -10 = 12
-123 mod0 -10 = -3
These procedures are unique to R6RS. Compatible procedures under different names
can be found in SRFI-141.
This procedure can raise exceptions with the following condition types:
- &assertion (R6RS)
- The wrong number of arguments was passed or an argument was outside its
domain.
- &implementation-restriction (R6RS)
- There is no number object of the correct type that can represent the
result under the mathematical restrictions described above.
These procedures first appeared in R6RS. RnRS revisions before that had
/(3scm), quotient(3scm) and remainder(3scm).