quotient(3scm)
π
quotient, remainder, modulo β number-theoretic integer division (the quotient(3scm) page, bestie)
okay so. three whole procedures today, and they're about number-theoretic integer division, which sounds scary but honestly it's just "dividing integers, but fancy." you got this. π
β¨ LIBRARY β¨
Import from wherever your Scheme lives, queen:
(import (rnrs r5rs)) ;R6RS
(import (scheme r5rs)) ;R7RS
(import (scheme base)) ;R7RS
β¨ SYNOPSIS β¨
(quotient n1 n2)
(remainder n1 n2)
(quotient n1 n2)
(remainder n1 n2)
(modulo n1 n2)
wait, that's not right β let me do it properly, no typos allowed:
(quotient n1 n2)
(remainder n1 n2)
(modulo n1 n2)
β¨ DESCRIPTION β¨
so these three implement number-theoretic (integer) division. n2 must be non-zero β like, non-negotiable, do not come for her with a zero, she will not be okay with it. π
if n1/n2 is an integer object, it's giving "clean division, no crumbs":
(quotient n1 n2) => n1/n2
(remainder n1 n2) => 0
(modulo n1 n2) => 0
if n1/n2 is not an integer object, then:
(quotient n1 n2) => nq
(remainder n1 n2) => nr
(modulo n1 n2) => nm
where, and this is the part you screenshot:
nqisn1/n2rounded towards zero0 < abs(nr) < abs(n2)0 < abs(nm) < abs(n2)nrandnmdiffer fromn1by a multiple ofn2nrhas the same sign asn1nmhas the same sign asn2
and consequently β bestie this is the iconique identity, the main character of the whole page β for integer objects n1 and n2 with n2 not equal to 0:
(= n1 (+ (* n2 (quotient n1 n2))
(remainder n1 n2)))
=> #t
provided all number objects involved in that computation are exact. (that qualifier is doing heavy lifting, don't ignore it π)
π§ IMPLEMENTATION NOTES π§
R6RS has this reference implementation, and she's cute but she is missing type checks β so like, she's not judging your arguments, at all, which is generous and also slightly unhinged:
(define (sign n)
(cond ((negative? n) -1)
((positive? n) 1)
(else 0)))
(define (quotient n1 n2)
(* (sign n1) (sign n2) (div (abs n1) (abs n2))))
(define (remainder n1 n2)
(* (sign n1) (mod (abs n1) (abs n2))))
(define (modulo n1 n2)
(* (sign n2) (mod (* (sign n2) n1) (abs n2))))
π RETURN VALUES π
each of these returns a single value β an integer object. one (1) whole result. she's lowkey low maintenance like that. π«Ά
π EXAMPLES π
get into the negative-number of it all, this is where things get spicy:
(modulo 13 4) => 1
(remainder 13 4) => 1
(modulo -13 4) => 3
(remainder -13 4) => -1
(modulo 13 -4) => -3
(remainder 13 -4) => 1
(modulo -13 -4) => -1
(remainder -13 -4) => -1
(remainder -13 -4.0) => -1.0 ; inexact
not the -13 erasure!! watch the signs, she's flipping them on purpose. π
π¦ APPLICATION USAGE π¦
both R6RS and R7RS ship these procedures for compatibility β they're the legacy besties you keep around. but highkey they have often unexpected behavior when one of the arguments is negative. π
unless you know you need something else, you should probably prefer Euclidean division. see div-and-mod(3scm).
like, fr fr, go look at div-and-mod(3scm) first. i'm serious.
π COMPATIBILITY π
R7RS implementations do not need to provide inexact integers, and the integer range may be reduced β but otherwise these procedures should work the same everywhere. slay for portability, honestly. β¨
π ERRORS π
these procedures can raise exceptions with the following condition types:
&assertion(R6RS) β the wrong number of arguments was passed, or an argument was outside its domain.- R7RS β the assertions described above are errors. implementations may signal an error, extend the procedure's domain of definition to include such arguments, or fail catastrophically. (yes. catastrophically. no i'm not being dramatic, that's the actual wording πΈ)
π SEE ALSO π
div-and-mod(3scm)floor-remainder(3scm)
β STANDARDS β
R4RS, IEEE Scheme, R5RS, R6RS, R7RS β she's certified, she's been through every board, iconic behavior only.
π HISTORY π
these procedures first appeared in R2RS. Scheme previous to R2RS, running on MacLisp, also had similar quotient and remainder procedures. they became legacy procedures in both R6RS and R7RS. so yeah, she's a veteran, she's seen things, respect her. π«‘
π AUTHORS π
this page is part of the scheme-manpages project. it includes materials from the RnRS documents. more information can be found at https://github.com/schemedoc/manpages/.
π BUGS π
depending on how these procedures are used, programs that use them may behave unexpectedly when one of the arguments is negative. that's it. that's the bug. negative numbers stay breaking hearts. π
and that's the page, bestie! π§ββοΈβ¨ this one's from the Scheme Programmer's Manual, dated 2022-04-12, quotient(3scm) β go forth and divide responsibly, and please, for me, check your signs. ππΈπ¦
π¦ where this page comes from
- nixpkgs unstable: scheme-manpages 0-unstable-2024-02-11
- π the page's license: MIT
- Copyright (c) 2022 Gwen Weinholt
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms