[We discussed the lack of real-domain integer division in R7RS at the
last meeting, after which I asked Taylor Campbell for his thoughts on
extending R7RS-small's procedures. His responses follow. I've also
posted most of this to the Codeberg issue.]
----- Forwarded message from Taylor R Campbell -----
Date: Tue, 23 Jun 2026 05:36:36 +0000
From: Taylor R Campbell <XXXX@XXXX>
To: Wolfgang Corcoran-Mathe <XXXX@XXXX>
Subject: Re: Extending R7RS integer division to real arguments
> Date: Mon, 8 Jun 2026 17:56:20 -0400
> From: Wolfgang Corcoran-Mathe <XXXX@XXXX>
>
> I discovered recently that the editors of R7RS-small neglected to
> include any procedures for doing integer division on real numbers.
> The assumption seems to have been that your SRFI 141 integer division
> library supported reals and that it could thus replace 'div', 'mod',
> and 'div-and-mod' from R6RS (which accept real arguments). This
> assumption was false and now we are trying to fix the situation.
>
> The solution most popular with the R7RS WG2 seems to be to extend the
> R7RS integer division procedures to real arguments. Since they're
> your work, and since you chose to restrict them to integer arguments,
> I figured we should ask you what you think. Is there a reason these
> procedures shouldn't be extended?
>
> The R7RS issue for this topic is at
>
https://codeberg.org/scheme/r7rs/issues/344, where Zhu Zihao has made
> some arguments in favor of extension.
Hadn't put much thought to it. Sounds reasonable to adopt. On ratio
or floating-point inputs, the remainder should be computed exactly,
and the usual Scheme inexactness contagion rules should apply: if
either input is tagged inexact, the remainder is tagged inexact (even
though the floating-point computation itself is computed exactly).
Now, there's a snag with the quotient. With _integer_ division n =
d*q + r, the quotient q is always no larger than the dividend n -- we
always have |q| <= |n|. But with _real_ division, where the divisor d
may be smaller in magnitude than 1, 0 < |d| < 1, the quotient q may be
larger in magnitude than the dividend n -- and may even overflow the
floating-point range, let alone any convenient machine integer range.
Consider, e.g., n = 1e300 divided by d = 1e-300; the quotient is q =
1e600 which far exceeds the largest finite binary64 floating-point
number. So the standard C r = remquo(n, d, &q) function yields in q
only the low k bits and sign of (round-quotient n d) for some k >= 3,
rather than a useless infinity. (The purpose is to find what quadrant
the input to a periodic function like sin or cos is in (q), and where
in the quadrant it is (r) -- this really only needs two bits to
distinguish quadrants.)
With bignums that's not an issue, of course: you can multiply both
inputs by 2^-e where e is the divisor's exponent, and then compute
integer division as bignums. But in many Scheme systems, you can't
tag bignums inexact. So you'll have to choose whether to overflow, or
yield an integer that is not tagged exact, violating the inexactness
contagion rules -- and/or confuse users who expect to get round(n/d)
rather than merely something congruent to it mod 2^k.
IEEE 754 requires a round-to-nearest/ties-to-even remainder
(round-remainder). C99 also defines fmod to share the sign of the
dividend (truncate-remainder). In both cases, the remainder can
always be computed exactly -- and it is required to be so by IEEE 754
and C99.
----- End forwarded message -----
----- Forwarded message from Taylor R Campbell -----
Date: Tue, 23 Jun 2026 06:00:18 +0000
From: Taylor R Campbell <XXXX@XXXX>
To: Wolfgang Corcoran-Mathe <XXXX@XXXX>
Subject: Re: Extending R7RS integer division to real arguments
> Date: Tue, 23 Jun 2026 05:36:36 +0000
> From: Taylor R Campbell <XXXX@XXXX>
>
> > Date: Mon, 8 Jun 2026 17:56:20 -0400
> > From: Wolfgang Corcoran-Mathe <XXXX@XXXX>
> >
> > The assumption seems to have been that your SRFI 141 integer division
> > library [...]
By the way, as far as I can recall, I wasn't involved in authoring
SRFI 141; I only wrote the draft it was based on,
<
https://mumble.net/~campbell/tmp/division.txt> (including whatever
errors are in it), and the original implementation in MIT Scheme,
<
https://cgit.git.savannah.gnu.org/cgit/mit-scheme.git/tree/src/runtime/division.scm?id=a9f0f5edb447a55368e6d071458bdbd303923e01>,
several years before the SRFI. (Maybe we had some IRC or email
discussions about some of the content but that's all.)
----- End forwarded message -----
--
Wolfgang Corcoran-Mathe <
w...@sigwinch.xyz>