unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Andy Wingo <wingo@pobox.com>
To: No Itisnt <theseaisinhere@gmail.com>
Cc: guile-devel <guile-devel@gnu.org>
Subject: Re: Exposing more math functionality
Date: Fri, 27 Aug 2010 08:44:40 -0700	[thread overview]
Message-ID: <m34oeggivr.fsf@unquote.localdomain> (raw)
In-Reply-To: <m3d3tetwqh.fsf@unquote.localdomain> (Andy Wingo's message of "Thu, 19 Aug 2010 09:04:22 -0700")

On Thu 19 Aug 2010 09:04, Andy Wingo <wingo@pobox.com> writes:

> Regarding modf -- it seems that the R6RS extends the definition of
> `modulo' (called `mod') to be defined over the real numbers.
>
>   (mod 10 3) => 1
>   (mod 10 3.0) => 1.0
>   (mod 10 3.1) => 0.7
>
> This appears to be a compatible extension of the R5RS' `modulo', so we
> should just extend our definition. That way we can avoid adding another
> symbol.

This was a mistaken impression, something I realized while trying to
implement this. From the R6RS rationale, section 11.6.6:

    div and mod

    Given arithmetic on exact integer objects of arbitrary precision, it
    is a trivial matter to derive signed and unsigned integer types of
    finite range from it by modular reduction.  For example 32-bit
    signed two-complement arithmetic behaves like computing with the
    residue classes “mod 232 ”, where the set {−231 , . . . , 231 − 1}
    has been chosen to represent the residue classes. Likewise, unsigned
    32-bit arithmetic also behaves like computing “mod 232 ”, but with a
    different set of representatives {0, . . . , 232 − 1}.

    Unfortunately, the R5 RS operations quotient, remainder, and modulo
    are not ideal for this purpose. In the following example, remainder
    fails to transport the additive group structure of the integers over
    to the residues modulo 3.
                                                                        
                                         
          (remainder (+ -2 3) 3) =⇒ 1
                                                                        
          (remainder (+ (remainder -2 3)
                        (remainder 3 3))
                     3) =⇒ -2
                                                                   
    In fact, modulo should have been used, producing residues in {0, 1,
    2}.  For modular reduction with symmetric residues, i.e., in {−1, 0,
    1} in the example, it is necessary to define a more complicated
    reduction altogether.
                                                                   
    Therefore, quotient, remainder, and modulo have been replaced in R6
    RS by the div, mod, div0, and mod0 procedures, which are more useful
    when implementing modular reduction. The underlying mathematical
    functions div, mod, div0 , and mod0 (see report section 11.7.3) have
    been adapted from the div and mod operations by Egner et
    al. [11]. They differ in the representatives from the residue
    classes they return: div and mod always compute a nonnegative
    residue, whereas div0 and mod0 compute a residue from a set centered
    on 0. The former can be used, for example, to implement unsigned
    fixed-width arithmetic, whereas the latter correspond to
    two’s-complement arithmetic.
                                                                   
    These operations differ slightly from the div and mod operations
    from Egner et al. The latter make both operations available through
    a single pair of operations that distinguish between the two cases
    for residues by the sign of the divisor (as well as returning 0 for
    a zero divisor). Splitting the operations into two sets of
    procedures avoids potential confusion.
                                                                   
    The procedures modulo, remainder, and quotient from R5RS can easily
    be defined in terms of div and mod.
                                                               
So I'm not sure what to do exactly. If Scheme people are happy with div
and mod, as in the R6RS, then we should implement them and make them the
default, implementing quotient, modulo, and remainder in terms of
them. Hmm.

Andy
-- 
http://wingolog.org/



  parent reply	other threads:[~2010-08-27 15:44 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-07-24  3:46 Exposing more math functionality No Itisnt
2010-08-19 16:04 ` Andy Wingo
2010-08-19 20:37   ` Phil
2010-08-27 15:44   ` Andy Wingo [this message]
2010-08-28  4:02     ` Andy Wingo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m34oeggivr.fsf@unquote.localdomain \
    --to=wingo@pobox.com \
    --cc=guile-devel@gnu.org \
    --cc=theseaisinhere@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).