For Gauche 0.9.6

Next: , Previous: , Up: Library modules - SRFIs   [Contents][Index]

11.29 srfi-141 - Integer division

Module: srfi-141

This module provides a comprehensive set of integer division operators.

Quotient and remainder in integer divisions can be defined in multiple ways, when you consider the choice of sign of the result with regard to the operands. Gauche has builtin procedures in several flavors: R5RS quotient, remadiner and modulo, R6RS div, mod, div0 and mod0, and R7RS floor-quotient, floor-remainder, floor/, truncate-quotinet, truncate-remainder, truncate/.

This module complements R7RS procedures, by adding ceiling, round, euclidean and balanced variants.

The following procedures are in srfi-141 but built-in in Gauche (see Arithmetics).

floor-quotient     floor-remainder    floor/
truncate-quotient  truncate-remainder truncate/
Function: ceiling-quotient n d
Function: ceiling-remainder n d
Function: ceiling/ n d

[SRFI-141] {srfi-141}

ceiling-quotient = ceiling(n / d)
ceiling-remainder = n - d * ceiling-quotient
ceiling/ = values(ceiling-quotient, ceiling-remainder)
Function: round-quotient n d
Function: round-remainder n d
Function: round/ n d

[SRFI-141] {srfi-141}

round-quotient = round(n/d)
round-remainder = n - d * round-quotient
round/ = values(round-quotient, round-remainder)
Function: euclidean-quotient n d
Function: euclidean-remainder n d
Function: euclidean/ n d

[SRFI-141] {srfi-141}

euclidean-quotient = floor(n / d)   if d > 0
                     ceiling(n / d) if d < 0
euclidean-remainder = n - d * euclidean-quotient
euclidean/ = values(euclidean-quotient, euclidean-remainder)

The Eclidean variant satisfies a property 0 <= remainder < abs(d). These are the same as R6RS’s div, mod, and div-and-mod, except that they accept non-integers (see Arithmetics)

Function: balanced-quotient n d
Function: balanced-remainder n d
Function: balanced/ n d

[SRFI-141] {srfi-141}

balanced-quotient = roundup(n / d)
balanced-remainder = n - d * balanced-quotient
balanced/ = values(balanced-quotient, balanced-remainder)
  where roundup(x) is  ceiling(x) if x - floor(x) <= 0.5
                   and floor(x)   if x - floor(x) > 0.5

The balanced variant satisfies a property -abs(d/2) <= remainder < abs(d/2). These are the same as R6RS’s div0, mod0, and div0-and-mod0, except that they accept non-integers (see Arithmetics).

Next: , Previous: , Up: Library modules - SRFIs   [Contents][Index]