From: Peter Seibel
Subject: Barbie and I both say ...
Date: 
Message-ID: <m37k9eosro.fsf@javamonkey.com>
... math is hard![1] Today I'm banging my head against trying to get
an intuitive sense of the difference between REM and MOD. As in, when
would you want to use vs the other. Anyone have any good examples they
want to share with me? (I realize that for positive numbers and
divisors, they behave the same.

Also, the spec says:

  "mod and rem are generalizations of the modulus and remainder
  functions respectively."

but I haven't been able to find a definitive definition of "the"
modulus and remainder functions. Most folks seem to think they're the
same thing. Even checked a number theory book I have lying around. Any
pointers?

-Peter

[1] http://www.syntac.net/hoax/barbie.php

-- 
Peter Seibel                                      ·····@javamonkey.com

  The intellectual level needed   for  system design is  in  general
  grossly  underestimated. I am  convinced  more than ever that this
  type of work is very difficult and that every effort to do it with
  other than the best people is doomed to either failure or moderate
  success at enormous expense. --Edsger Dijkstra

From: Erann Gat
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <gat-2804032255350001@192.168.1.51>
In article <··············@javamonkey.com>, Peter Seibel
<·····@javamonkey.com> wrote:

> ... math is hard![1] Today I'm banging my head against trying to get
> an intuitive sense of the difference between REM and MOD. As in, when
> would you want to use vs the other. Anyone have any good examples they
> want to share with me? (I realize that for positive numbers and
> divisors, they behave the same.
> 
> Also, the spec says:
> 
>   "mod and rem are generalizations of the modulus and remainder
>   functions respectively."
> 
> but I haven't been able to find a definitive definition of "the"
> modulus and remainder functions. Most folks seem to think they're the
> same thing. Even checked a number theory book I have lying around. Any
> pointers?

MOD and REM give different results when their two arguments have different
signs.  e.g.:

(mod -22 7) --> 6
(rem -22 7) --> -1
(mod 22 -7) --> -6
(rem 22 -7) --> 1

E.
From: Peter Seibel
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <m3vfwxohkb.fsf@javamonkey.com>
···@jpl.nasa.gov (Erann Gat) writes:

> In article <··············@javamonkey.com>, Peter Seibel
> <·····@javamonkey.com> wrote:
> 
> > ... math is hard![1] Today I'm banging my head against trying to get
> > an intuitive sense of the difference between REM and MOD. As in, when
> > would you want to use vs the other. Anyone have any good examples they
> > want to share with me? (I realize that for positive numbers and
> > divisors, they behave the same.
> > 
> > Also, the spec says:
> > 
> >   "mod and rem are generalizations of the modulus and remainder
> >   functions respectively."
> > 
> > but I haven't been able to find a definitive definition of "the"
> > modulus and remainder functions. Most folks seem to think they're the
> > same thing. Even checked a number theory book I have lying around. Any
> > pointers?
> 
> MOD and REM give different results when their two arguments have
> different signs. e.g.:
> 
> (mod -22 7) --> 6
> (rem -22 7) --> -1
> (mod 22 -7) --> -6
> (rem 22 -7) --> 1

Right. So when might one choose one or the other. In other words, what
do these functions "mean". For instance, "+" means "if you have some
stuff and you get some more stuff, and you want to know how much you
have now, + the two amounts". Or, to account for negative numbers, "if
you owe somebody some amount of stuff and you have another amount of
stuff, + the two amounts to find out whether you have enought to pay
off your debt--if the result is positive, you do."

I have a similarly intuitive understanding of both MOD and REM when
both arguments are positive--take one amount out of another as many
times as you can (without going into debt) and MOD/REM will tell you
how much you have left. But I don't have any feel for what they mean
in the examples you give above. I understand how they work, relative
to FLOOR and TRUNCATE and all that. It's not the language semantics
that I'm unclear on but a higher-level of semantics, if you see what I
mean.

-Peter

-- 
Peter Seibel                                      ·····@javamonkey.com

  The intellectual level needed   for  system design is  in  general
  grossly  underestimated. I am  convinced  more than ever that this
  type of work is very difficult and that every effort to do it with
  other than the best people is doomed to either failure or moderate
  success at enormous expense. --Edsger Dijkstra
From: Erann Gat
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <gat-2904030823310001@192.168.1.51>
In article <··············@javamonkey.com>, Peter Seibel
<·····@javamonkey.com> wrote:

> It's not the language semantics
> that I'm unclear on but a higher-level of semantics, if you see what I
> mean.

You probably want to ask this question on comp.math or someplace like
that, but here's a stab at it:

(rem x y) is the answer to the question: "What is 'left over' when you
divide X by Y (using the naive algorithm of counting how many times you
can reduce the magnitide of X by taking away Y)?"

(mod x y) is the answer to the question: "What would I get if I counted X
things in a number system that only had Y numbers in it?"

E.
From: Gareth McCaughan
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <slrnbatgpo.on.Gareth.McCaughan@g.local>
Erann Gat wrote:
>  In article <··············@javamonkey.com>, Peter Seibel
>  <·····@javamonkey.com> wrote:
>  
> > It's not the language semantics
> > that I'm unclear on but a higher-level of semantics, if you see what I
> > mean.
>  
> You probably want to ask this question on comp.math or someplace like
> that, but here's a stab at it:
> 
> (rem x y) is the answer to the question: "What is 'left over' when you
> divide X by Y (using the naive algorithm of counting how many times you
> can reduce the magnitude of X by taking away Y)?"
> 
> (mod x y) is the answer to the question: "What would I get if I counted X
> things in a number system that only had Y numbers in it?"

I don't think I like those definitions, because they
don't make it clear what happens when Y is negative.

I really think the only good way to think about these,
if you want to understand the cases where the arguments
can be negative, is by saying:

When <f,g> == <div,mod> or <truncate,rem>, the
following thing is true unless (zerop y):

    (= x (+ (* y (f x y))
            (g x y)))

Then, if you understand DIV and TRUNCATE, you also
understand MOD and REM. Unfortunately, this is all
rather dry and abstract.

-- 
Gareth McCaughan  ················@pobox.com
.sig under construc
From: Erann Gat
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <gat-2904031416220001@k-137-79-50-101.jpl.nasa.gov>
In article <······························@g.local>,
················@pobox.com wrote:

> Erann Gat wrote:
> >  In article <··············@javamonkey.com>, Peter Seibel
> >  <·····@javamonkey.com> wrote:
> >  
> > > It's not the language semantics
> > > that I'm unclear on but a higher-level of semantics, if you see what I
> > > mean.
> >  
> > You probably want to ask this question on comp.math or someplace like
> > that, but here's a stab at it:
> > 
> > (rem x y) is the answer to the question: "What is 'left over' when you
> > divide X by Y (using the naive algorithm of counting how many times you
> > can reduce the magnitude of X by taking away Y)?"
> > 
> > (mod x y) is the answer to the question: "What would I get if I counted X
> > things in a number system that only had Y numbers in it?"
> 
> I don't think I like those definitions,

They weren't intended to be definitions, they were intended to be informal
intuitive descriptions.  I thought that's what the OP wanted.

E.
From: Jacek Generowicz
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <tyfsms1bszl.fsf@pcepsft001.cern.ch>
Peter Seibel <·····@javamonkey.com> writes:

> > In article <··············@javamonkey.com>, Peter Seibel
> > <·····@javamonkey.com> wrote:
> > 
> > > Today I'm banging my head against trying to get an intuitive
> > > sense of the difference between REM and MOD.

> I understand how they work, relative to FLOOR and TRUNCATE and all
> that. It's not the language semantics that I'm unclear on but a
> higher-level of semantics, if you see what I mean.

Do you have an intuitive feeling for the meaning of FLOOR and
TRUNCATE?

I infer from what you say that you are aware that 

  (= (mod a b) (nth-value 1 (floor a b)))

and

  (= (rem a b) (nth-value 1 (truncate a b))).
From: Peter Seibel
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <m3el3lnlga.fsf@javamonkey.com>
Jacek Generowicz <················@cern.ch> writes:

> Peter Seibel <·····@javamonkey.com> writes:
> 
> > > In article <··············@javamonkey.com>, Peter Seibel
> > > <·····@javamonkey.com> wrote:
> > > 
> > > > Today I'm banging my head against trying to get an intuitive
> > > > sense of the difference between REM and MOD.
> 
> > I understand how they work, relative to FLOOR and TRUNCATE and all
> > that. It's not the language semantics that I'm unclear on but a
> > higher-level of semantics, if you see what I mean.
> 
> Do you have an intuitive feeling for the meaning of FLOOR and
> TRUNCATE?

Well, I do for for the primary return value. But not--perhaps
obviously--for the remainder value.

> 
> I infer from what you say that you are aware that 
> 
>   (= (mod a b) (nth-value 1 (floor a b)))
> 
> and
> 
>   (= (rem a b) (nth-value 1 (truncate a b))).
> 

Yes. In fact I was also wondering why these two were picked to have
their own "remainder-only" functions. While poking around I happened
to notice that

    (nth-value 1 (round a b))

happens to compute values similar to those computed by the
IEEEremainder method in java.lang.Math which claims to be an an
implementation of the remainder function for floating points defined
in IEEE 754.

-Peter

-- 
Peter Seibel                                      ·····@javamonkey.com

  The intellectual level needed   for  system design is  in  general
  grossly  underestimated. I am  convinced  more than ever that this
  type of work is very difficult and that every effort to do it with
  other than the best people is doomed to either failure or moderate
  success at enormous expense. --Edsger Dijkstra
From: Pascal Bourguignon
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <87vfwwv0q3.fsf@thalassa.informatimago.com>
···@jpl.nasa.gov (Erann Gat) writes:

> In article <··············@javamonkey.com>, Peter Seibel
> <·····@javamonkey.com> wrote:
> 
> > ... math is hard![1] Today I'm banging my head against trying to get
> > an intuitive sense of the difference between REM and MOD. As in, when
> > would you want to use vs the other. Anyone have any good examples they
> > want to share with me? (I realize that for positive numbers and
> > divisors, they behave the same.
> > 
> > Also, the spec says:
> > 
> >   "mod and rem are generalizations of the modulus and remainder
> >   functions respectively."
> > 
> > but I haven't been able to find a definitive definition of "the"
> > modulus and remainder functions. Most folks seem to think they're the
> > same thing. Even checked a number theory book I have lying around. Any
> > pointers?
> 
> MOD and REM give different results when their two arguments have different
> signs.  e.g.:
> 
> (mod -22 7) --> 6
> (rem -22 7) --> -1
> (mod 22 -7) --> -6
> (rem 22 -7) --> 1

Note that:  (mod (rem -22 7) 7) --> 6 

X modulo  Y is the  equivalence class of  the following relation  R of
which X is an element (a representant):

    p R q <=> p�Y - q�Y = 0

(meaning that p and q give the same remainder when divided by Y).


By convention,  we use  the representant of  the modulo class  that is
between 0 and Y.


Now, some fun:  what are (mod -22 -7) and (rem -22 -7) ?

-- 
__Pascal_Bourguignon__                   http://www.informatimago.com/
----------------------------------------------------------------------
Do not adjust your mind, there is a fault in reality.
From: Pascal Bourguignon
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <87znm6hixv.fsf@thalassa.informatimago.com>
Pascal Bourguignon <····@thalassa.informatimago.com> writes:

> ···@jpl.nasa.gov (Erann Gat) writes:
> 
> > In article <··············@javamonkey.com>, Peter Seibel
> > <·····@javamonkey.com> wrote:
> > 
> > > ... math is hard![1] Today I'm banging my head against trying to get
> > > an intuitive sense of the difference between REM and MOD. As in, when
> > > would you want to use vs the other. Anyone have any good examples they
> > > want to share with me? (I realize that for positive numbers and
> > > divisors, they behave the same.
> > > 
> > > Also, the spec says:
> > > 
> > >   "mod and rem are generalizations of the modulus and remainder
> > >   functions respectively."
> > > 
> > > but I haven't been able to find a definitive definition of "the"
> > > modulus and remainder functions. Most folks seem to think they're the
> > > same thing. Even checked a number theory book I have lying around. Any
> > > pointers?
> > 
> > MOD and REM give different results when their two arguments have different
> > signs.  e.g.:
> > 
> > (mod -22 7) --> 6
> > (rem -22 7) --> -1
> > (mod 22 -7) --> -6
> > (rem 22 -7) --> 1
> 
> Note that:  (mod (rem -22 7) 7) --> 6 
> 
> X modulo  Y is the  equivalence class of  the following relation  R of
> which X is an element (a representant):
> 
>     p R q <=> p�Y - q�Y = 0

Of course, everybody will have corrected it internally. It's:

      P R q <=> (p-(p�Y)*Y) = (q-(q�Y)*Y) 

That's what you get for posting just before dropping to bed.

> (meaning that p and q give the same remainder when divided by Y).
> 
> 
> By convention,  we use  the representant of  the modulo class  that is
> between 0 and Y.
> 
> 
> Now, some fun:  what are (mod -22 -7) and (rem -22 -7) ?
> 
> -- 
> __Pascal_Bourguignon__                   http://www.informatimago.com/
> ----------------------------------------------------------------------
> Do not adjust your mind, there is a fault in reality.

-- 
__Pascal_Bourguignon__                   http://www.informatimago.com/
----------------------------------------------------------------------
Do not adjust your mind, there is a fault in reality.
From: Peter Seibel
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <m3znm9oqjc.fsf@javamonkey.com>
Peter Seibel <·····@javamonkey.com> writes:

> ... math is hard![1] Today I'm banging my head against trying to get
> an intuitive sense of the difference between REM and MOD. As in, when
> would you want to use vs the other. Anyone have any good examples they
> want to share with me? (I realize that for positive numbers and
> divisors, they behave the same.

Heh. Well, here's a data point. Not that it actually helps me
understand when to use which one, but it's sort of curious. The %
operator, which exists in C, Java, Perl, and Python, is equivalent to
REM in half of those languages (C and Java) and MOD in the other half
(Perl and Python).

-Peter

-- 
Peter Seibel                                      ·····@javamonkey.com

  The intellectual level needed   for  system design is  in  general
  grossly  underestimated. I am  convinced  more than ever that this
  type of work is very difficult and that every effort to do it with
  other than the best people is doomed to either failure or moderate
  success at enormous expense. --Edsger Dijkstra
From: Michael Hudson
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <7h3u1chirv4.fsf@pc150.maths.bris.ac.uk>
Peter Seibel <·····@javamonkey.com> writes:

> Heh. Well, here's a data point. Not that it actually helps me
> understand when to use which one, but it's sort of curious. The %
> operator, which exists in C, Java, Perl, and Python, is equivalent to
> REM in half of those languages (C and Java) and MOD in the other half
> (Perl and Python).

AFAIK, C89 doesn't specify which you get.  Dunno whether C99 does...

Cheers,
M.

-- 
  QNX... the OS that walks like a duck, quacks like a duck, but is,
  in fact, a platypus. ... the adventures of porting duck software 
  to the platypus were avoidable this time.
                                 -- Chris Klein, alt.sysadmin.recovery
From: Peter Seibel
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <m3ade9nldo.fsf@javamonkey.com>
Michael Hudson <···@python.net> writes:

> Peter Seibel <·····@javamonkey.com> writes:
> 
> > Heh. Well, here's a data point. Not that it actually helps me
> > understand when to use which one, but it's sort of curious. The %
> > operator, which exists in C, Java, Perl, and Python, is equivalent to
> > REM in half of those languages (C and Java) and MOD in the other half
> > (Perl and Python).
> 
> AFAIK, C89 doesn't specify which you get.  Dunno whether C99 does...

Yes. Mea Culpa. I figured out that equivalence via experimentation,
not a careful reading of the spec. I don't *really* care what those
languages do; I was just curious. ;-)

-Peter

-- 
Peter Seibel                                      ·····@javamonkey.com

  The intellectual level needed   for  system design is  in  general
  grossly  underestimated. I am  convinced  more than ever that this
  type of work is very difficult and that every effort to do it with
  other than the best people is doomed to either failure or moderate
  success at enormous expense. --Edsger Dijkstra
From: Michael Hudson
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <7h365owgul0.fsf@pc150.maths.bris.ac.uk>
Peter Seibel <·····@javamonkey.com> writes:

> Michael Hudson <···@python.net> writes:
> 
> > Peter Seibel <·····@javamonkey.com> writes:
> > 
> > > Heh. Well, here's a data point. Not that it actually helps me
> > > understand when to use which one, but it's sort of curious. The %
> > > operator, which exists in C, Java, Perl, and Python, is equivalent to
> > > REM in half of those languages (C and Java) and MOD in the other half
> > > (Perl and Python).
> > 
> > AFAIK, C89 doesn't specify which you get.  Dunno whether C99 does...
> 
> Yes. Mea Culpa. I figured out that equivalence via experimentation,
> not a careful reading of the spec. I don't *really* care what those
> languages do; I was just curious. ;-)

I only know because I know Python's implementation contains a comment
along the lines of "now C doesn't specify what we get here, so we
force it"...

Cheers,
M.

-- 
  After a heavy night I travelled on, my face toward home - the comma
  being by no means guaranteed.           -- paraphrased from cam.misc
From: Thomas Stegen
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <3eb2bf43$1@nntphost.cis.strath.ac.uk>
Michael Hudson wrote:
 >
> AFAIK, C89 doesn't specify which you get.  Dunno whether C99 does...
> 

C99 does. Don't remember which you get though...


-- 
Thomas.
        "How long is six months? Ten years?"
            - Denis Johnson, Fiskadoro
From: Gareth McCaughan
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <slrnbasdsl.on.Gareth.McCaughan@g.local>
Peter Seibel wrote:

> ... math is hard![1] Today I'm banging my head against trying to get
> an intuitive sense of the difference between REM and MOD. As in, when
> would you want to use vs the other. Anyone have any good examples they
> want to share with me? (I realize that for positive numbers and
> divisors, they behave the same.)

Conjecture: If you just forget about REM and always
use MOD then you'll essentially never miss REM.

One drawback of REM: it isn't true that

  (eql (integerp (/ (- a b) d))
       (= (rem a d) (rem b d)))

Consider, e.g., a=7, b=-3, d=5. That means that
you can't use REM to canonicalize numbers modulo d.
(Equivalent statement: the values of (REM ... d)
aren't periodic with period |d|, whereas those of
(MOD ... d) are.)

If you have access somehow to old editions of ACM TOPLAS[1]
then you might want to look up a paper by Boute in vol 14
number 2 (April 1992), entitled "The Euclidean definition
of div and mod", in which he observes (1) that some
languages define div/mod in a way that's downright broken,
(2) that the definition based on FLOOR is better in
various ways than the definition based on TRUNCATE,
and (3) that there's a third, rarely used, option that
is arguably better than either.


[1] Transactions on Programming Languages and Systems.

-- 
Gareth McCaughan  ················@pobox.com
.sig under construc
From: larry
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <7b8f89d6.0304290956.cb492c2@posting.google.com>
rem(m,n) is the remainder when you divide n into m period.

mod(m,n) is also the remainder when divide n into m only the answer is
converted modulo n to be in the set {0,1,2,...n-1) if n > 0 or
(0,-1,-2, ....,n+1) if n <0


So rem(-22,7) = -1 because -22 = 7 *-3 + -1
but mod(-22,7) = 6 because the remainder -1 modulo 7 is 6,
i.e. mod(-1,7) = 6

Gareth McCaughan <················@pobox.com> wrote in message news:<······························@g.local>...
> Peter Seibel wrote:
> 
> > ... math is hard![1] Today I'm banging my head against trying to get
> > an intuitive sense of the difference between REM and MOD. As in, when
> > would you want to use vs the other. Anyone have any good examples they
> > want to share with me? (I realize that for positive numbers and
> > divisors, they behave the same.)
> 
> Conjecture: If you just forget about REM and always
> use MOD then you'll essentially never miss REM.
> 
> One drawback of REM: it isn't true that
> 
>   (eql (integerp (/ (- a b) d))
>        (= (rem a d) (rem b d)))
> 
> Consider, e.g., a=7, b=-3, d=5. That means that
> you can't use REM to canonicalize numbers modulo d.
> (Equivalent statement: the values of (REM ... d)
> aren't periodic with period |d|, whereas those of
> (MOD ... d) are.)
> 
> If you have access somehow to old editions of ACM TOPLAS[1]
> then you might want to look up a paper by Boute in vol 14
> number 2 (April 1992), entitled "The Euclidean definition
> of div and mod", in which he observes (1) that some
> languages define div/mod in a way that's downright broken,
> (2) that the definition based on FLOOR is better in
> various ways than the definition based on TRUNCATE,
> and (3) that there's a third, rarely used, option that
> is arguably better than either.
> 
> 
> [1] Transactions on Programming Languages and Systems.
From: Gareth McCaughan
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <slrnbatgs3.on.Gareth.McCaughan@g.local>
"larry" wrote:
> rem(m,n) is the remainder when you divide n into m period.
> 
> mod(m,n) is also the remainder when divide n into m only the answer is
> converted modulo n to be in the set {0,1,2,...n-1) if n > 0 or
> (0,-1,-2, ....,n+1) if n <0

That would be helpful if it were obvious what it means
to divide N into M when M and N can be negative. I don't
think it is. Especially not when N is negative.

-- 
Gareth McCaughan  ················@pobox.com
.sig under construc
From: Andreas Eder
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <m37k9d3uh7.fsf@elgin.eder.de>
Gareth McCaughan <················@pobox.com> writes:

> "larry" wrote:
> > rem(m,n) is the remainder when you divide n into m period.
> > 
> > mod(m,n) is also the remainder when divide n into m only the answer is
> > converted modulo n to be in the set {0,1,2,...n-1) if n > 0 or
> > (0,-1,-2, ....,n+1) if n <0
> 
> That would be helpful if it were obvious what it means
> to divide N into M when M and N can be negative. I don't
> think it is. Especially not when N is negative.

for m, r and n integers (not only natural numbers) m is congruent to r
mod m ( m = r mod n ) if n divides m - r ( n | (m-r) ). Now congruence
is an equivalence relation, so it divides the integers into disjunct
classes ( and there are exactly n such classes). Now both rem and mod just
give you a representative of the congruence class of m mod n - and
they do it in a different way if m or n is negative. Pick the one that
suits you best for your purpose; sometimes you want the smallest
positive representative, sometimes you want the one with smallest
absolute value. It all depends.

Andreas
-- 
Wherever I lay my .emacs, there�s my $HOME.
From: Gareth McCaughan
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <slrnbatru1.on.Gareth.McCaughan@g.local>
Andreas Eder wrote:

> for m, r and n integers (not only natural numbers) m is congruent to r
> mod n ( m = r mod n ) if n divides m - r ( n | (m-r) ). Now congruence
> is an equivalence relation, so it divides the integers into disjunct
> classes ( and there are exactly n such classes). Now both rem and mod just
> give you a representative of the congruence class of m mod n - and
> they do it in a different way if m or n is negative. Pick the one that
> suits you best for your purpose; sometimes you want the smallest
> positive representative, sometimes you want the one with smallest
> absolute value. It all depends.

Unfortunately, REM doesn't give you the one with smallest
absolute value. Nor can what it does be described in terms
of picking one of a fixed set of representatives mod n:

    (rem  7 5) ==> 2
    (rem -3 5) ==> -3

REM is almost never the right thing.

-- 
Gareth McCaughan  ················@pobox.com
.sig under construc
From: larry
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <7b8f89d6.0304300921.7392a495@posting.google.com>
Gareth McCaughan <················@pobox.com> wrote in message news:<······························@g.local>...
> "larry" wrote:
> > rem(m,n) is the remainder when you divide n into m period.
> > 
> > mod(m,n) is also the remainder when divide n into m only the answer is
> > converted modulo n to be in the set {0,1,2,...n-1) if n > 0 or
> > (0,-1,-2, ....,n+1) if n <0
> 
> That would be helpful if it were obvious what it means
> to divide N into M when M and N can be negative. I don't
> think it is. Especially not when N is negative.


Division applies to all numbers positive and negative.
The rule is a/b = abs(a)/abs(b) when a>0 and b> 0 or a<0 and b<0
and a/b = -abs(a)/abs(b) when a<0 and b>0 or a<0 and b > 0
From: Gareth McCaughan
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <slrnbb0gp3.on.Gareth.McCaughan@g.local>
"larry" wrote:

> > > rem(m,n) is the remainder when you divide n into m period.
> > > 
> > > mod(m,n) is also the remainder when divide n into m only the answer is
> > > converted modulo n to be in the set {0,1,2,...n-1) if n > 0 or
> > > (0,-1,-2, ....,n+1) if n <0
> > 
> > That would be helpful if it were obvious what it means
> > to divide N into M when M and N can be negative. I don't
> > think it is. Especially not when N is negative.
> 
> Division applies to all numbers positive and negative.
> The rule is a/b = abs(a)/abs(b) when a>0 and b> 0 or a<0 and b<0
> and a/b = -abs(a)/abs(b) when a<0 and b>0 or a<0 and b > 0

Sorry, I wasn't clear. I do know what division means
for negative numbers! What *isn't* obvious is what
"the remainder when you divide ... by ..." means when
either or both "..." is negative. And, indeed, the
results of MOD and REM are two different possible
ways of specifying a meaning for the phrase. There
is at least one other fairly plausible meaning, which
is what you get if you stipulate that "the remainder"
is always >= 0 and < |denominator|. That doesn't
correspond to either MOD or REM, nor to the second
value returned by any of the division operators.

-- 
Gareth McCaughan  ················@pobox.com
.sig under construc
From: Thomas A. Russ
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <ymik7daoe5s.fsf@sevak.isi.edu>
> "larry" wrote:
>
> > Division applies to all numbers positive and negative.
> > The rule is a/b = abs(a)/abs(b) when a>0 and b> 0 or a<0 and b<0
> > and a/b = -abs(a)/abs(b) when a<0 and b>0 or a<0 and b > 0

Gareth McCaughan <················@pobox.com> writes:

> Sorry, I wasn't clear. I do know what division means
> for negative numbers! What *isn't* obvious is what
> "the remainder when you divide ... by ..." means when
> either or both "..." is negative. And, indeed, the
> results of MOD and REM are two different possible
> ways of specifying a meaning for the phrase. There
> is at least one other fairly plausible meaning, which
> is what you get if you stipulate that "the remainder"
> is always >= 0 and < |denominator|. That doesn't
> correspond to either MOD or REM, nor to the second
> value returned by any of the division operators.

I'm not sure how useful this last definition would be.  The trick, I
think, comes in thinking about how you want this to interact with other
operations, especially negation.

For example, one might want
    (- (newrem a b))
    (newrem (- a) b)
    (newrem a (- b))

to all return the same number.  If I understand the new meaning you
propse, then, for example

     (newrem 4 3)  =  1
     (newrem 4 -3) =  1  or is it 2?

In any case it wouldn't satisfy associativity with division.  Of course,
the MOD function as implemented in Common Lisp doesn't do that either,
so that may not be a killing objection.


-- 
Thomas A. Russ,  USC/Information Sciences Institute          ···@isi.edu    
From: Gareth McCaughan
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <slrnbb334i.on.Gareth.McCaughan@g.local>
Thomas A. Russ wrote:

[I said, to "larry":]
> > Sorry, I wasn't clear. I do know what division means
> > for negative numbers! What *isn't* obvious is what
> > "the remainder when you divide ... by ..." means when
> > either or both "..." is negative. And, indeed, the
> > results of MOD and REM are two different possible
> > ways of specifying a meaning for the phrase. There
> > is at least one other fairly plausible meaning, which
> > is what you get if you stipulate that "the remainder"
> > is always >= 0 and < |denominator|. That doesn't
> > correspond to either MOD or REM, nor to the second
> > value returned by any of the division operators.
> 
> I'm not sure how useful this last definition would be.  The trick, I
> think, comes in thinking about how you want this to interact with other
> operations, especially negation.
> 
> For example, one might want
>     (- (newrem a b))
>     (newrem (- a) b)
>     (newrem a (- b))
> 
> to all return the same number.

One might. That isn't a property of either MOD or REM
in Common Lisp, nor of the other definition I mentioned.
REM satisfies (= (rem (- a) b) (- (rem a b))), because
TRUNC is an even function, but neither of them does
what you ask when the denominator is negated.

Note that the limited version of the symmetry axiom
satisfied by REM is already enough to guarantee that,
at least when the denominator is even, the values
of (newrem ... b) don't form a set of size b that
includes 0. I think it's quite important that (1)
(zerop (newrem 0 b)), and that (2) for fixed b
there are exactly b possible values of (newrem ... b),
because I like to think of this operation as picking
a canonical representative mod b. Your mileage may,
of course, vary.

>                                 If I understand the new meaning you
> propse, then, for example
> 
>      (newrem 4 3)  =  1
>      (newrem 4 -3) =  1  or is it 2?

In case it wasn't clear, I should mention that I wasn't
exactly *proposing* the definition I described, just
observing that it's a reasonably plausible candidate.
Anyway, with that definition we'd have (newrem 4 -3) ==> 1.

> In any case it wouldn't satisfy associativity with division.  Of course,
> the MOD function as implemented in Common Lisp doesn't do that either,
> so that may not be a killing objection.

What do you mean by associativity with division? Do you
mean (= a (+ (newrem a b) (* a (newdiv a b)))) for some
suitable NEWDIV function? If so, then CL's MOD function
does satisfy that, for NEWDIV = FLOOR, and so does REM,
for NEWDIV = TRUNCATE, and so does the other definition,
for a version of NEWDIV that doesn't have a name in CL.

-- 
Gareth McCaughan  ················@pobox.com
.sig under construc
From: Ingvar Mattsson
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <87issvovb1.fsf@gruk.tech.ensign.ftech.net>
Gareth McCaughan <················@pobox.com> writes:

> "larry" wrote:
> 
> > > > rem(m,n) is the remainder when you divide n into m period.
> > > > 
> > > > mod(m,n) is also the remainder when divide n into m only the answer is
> > > > converted modulo n to be in the set {0,1,2,...n-1) if n > 0 or
> > > > (0,-1,-2, ....,n+1) if n <0
> > > 
> > > That would be helpful if it were obvious what it means
> > > to divide N into M when M and N can be negative. I don't
> > > think it is. Especially not when N is negative.
> > 
> > Division applies to all numbers positive and negative.
> > The rule is a/b = abs(a)/abs(b) when a>0 and b> 0 or a<0 and b<0
> > and a/b = -abs(a)/abs(b) when a<0 and b>0 or a<0 and b > 0
> 
> Sorry, I wasn't clear. I do know what division means
> for negative numbers! What *isn't* obvious is what
> "the remainder when you divide ... by ..." means when
> either or both "..." is negative. And, indeed, the
> results of MOD and REM are two different possible
> ways of specifying a meaning for the phrase. There
> is at least one other fairly plausible meaning, which
> is what you get if you stipulate that "the remainder"
> is always >= 0 and < |denominator|. That doesn't
> correspond to either MOD or REM, nor to the second
> value returned by any of the division operators.

Hm, I'd say (intuitively) that a "remainder" is what you need to add
to (the integer) result of b*(a/b) to get a. The modulus is by nature
restricted to be in the non-negative (hrm, I wonder... to a degree a
modulus of negative numbers makes sense, but I can't say where that'd
lead).

//Ingvar
-- 
((lambda (x) `(,x ',x)) '(lambda (x) `(,x ',x)))
	Probably KMP
From: Johan Kullstam
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <87llxpgpf1.fsf@sysengr.res.ray.com>
Ingvar Mattsson <······@cathouse.bofh.se> writes:

> Gareth McCaughan <················@pobox.com> writes:
> 
> > "larry" wrote:
> > 
> > > > > rem(m,n) is the remainder when you divide n into m period.
> > > > > 
> > > > > mod(m,n) is also the remainder when divide n into m only the answer is
> > > > > converted modulo n to be in the set {0,1,2,...n-1) if n > 0 or
> > > > > (0,-1,-2, ....,n+1) if n <0
> > > > 
> > > > That would be helpful if it were obvious what it means
> > > > to divide N into M when M and N can be negative. I don't
> > > > think it is. Especially not when N is negative.
> > > 
> > > Division applies to all numbers positive and negative.
> > > The rule is a/b = abs(a)/abs(b) when a>0 and b> 0 or a<0 and b<0
> > > and a/b = -abs(a)/abs(b) when a<0 and b>0 or a<0 and b > 0
> > 
> > Sorry, I wasn't clear. I do know what division means
> > for negative numbers! What *isn't* obvious is what
> > "the remainder when you divide ... by ..." means when
> > either or both "..." is negative. And, indeed, the
> > results of MOD and REM are two different possible
> > ways of specifying a meaning for the phrase. There
> > is at least one other fairly plausible meaning, which
> > is what you get if you stipulate that "the remainder"
> > is always >= 0 and < |denominator|. That doesn't
> > correspond to either MOD or REM, nor to the second
> > value returned by any of the division operators.
> 
> Hm, I'd say (intuitively) that a "remainder" is what you need to add
> to (the integer) result of b*(a/b) to get a. The modulus is by nature
> restricted to be in the non-negative (hrm, I wonder... to a degree a
> modulus of negative numbers makes sense, but I can't say where that'd
> lead).

It leads to this:

Say I have two numbers in Z/<n> for some n (integer modulo n).
Often, you can work with these in Z and later quotient out by <n>.

now, is  i = j (mod n) or not?

this is what you want
(= (mod i n) (mod j n))

e.g., let i = -1, j = 4 and n = 5
(= (mod -1 5) (mod 4 5))
=> t

(= (rem -1 5) (rem 4 5))
=> nil

rem is an optimization when you don't need the full information of mod.

-- 
Johan KULLSTAM <··········@attbi.com> sysengr
From: Gareth McCaughan
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <slrnbb5hkt.on.Gareth.McCaughan@g.local>
Johan Kullstam wrote:
> It leads to this:
> 
> Say I have two numbers in Z/<n> for some n (integer modulo n).
> Often, you can work with these in Z and later quotient out by <n>.
> 
> now, is  i = j (mod n) or not?
> 
> this is what you want
> (= (mod i n) (mod j n))

Although for that particular purpose

    (zerop (mod (- i j) n))

is probably more efficient :-).

> rem is an optimization when you don't need the full information of mod.

How is REM an optimization? I don't see any reason to
expect it to be faster than MOD.

-- 
Gareth McCaughan  ················@pobox.com
.sig under construc
From: Ingvar Mattsson
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <87y91prr58.fsf@gruk.tech.ensign.ftech.net>
Johan Kullstam <··········@attbi.com> writes:

> Ingvar Mattsson <······@cathouse.bofh.se> writes:
[SNIP]
> > Hm, I'd say (intuitively) that a "remainder" is what you need to add
> > to (the integer) result of b*(a/b) to get a. The modulus is by nature
> > restricted to be in the non-negative (hrm, I wonder... to a degree a
> > modulus of negative numbers makes sense, but I can't say where that'd
> > lead).
> 
> It leads to this:
> 
> Say I have two numbers in Z/<n> for some n (integer modulo n).
> Often, you can work with these in Z and later quotient out by <n>.
> 
> now, is  i = j (mod n) or not?
> 
> this is what you want
> (= (mod i n) (mod j n))
> 
> e.g., let i = -1, j = 4 and n = 5
> (= (mod -1 5) (mod 4 5))
> => t
> 
> (= (rem -1 5) (rem 4 5))
> => nil
> 
> rem is an optimization when you don't need the full information of mod.

I'd even go as far as to say taht they're related but coneptually
different operations. I'd use MOD when I'm dealing with groups or
rings and REM when I'm dealing with numbers. Um. Well... I don't see
them as subtly different versions of the same thing, I see them as
different things that (in many cases) have teh same results.

//Ingvar
-- 
(defmacro fakelambda (args &body body) `(labels ((me ,args ,@body)) #'me))
(funcall (fakelambda (a b) (if (zerop (length a)) b (format nil "~a~a" 
 (aref a 0) (me b (subseq a 1))))) "Js nte iphce" "utaohrls akr")
From: larry
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <7b8f89d6.0305010953.730a5db0@posting.google.com>
Gareth McCaughan <················@pobox.com> wrote in message news:<······························@g.local>...
> "larry" wrote:
> 
> > > > rem(m,n) is the remainder when you divide n into m period.
> > > > 
> > > > mod(m,n) is also the remainder when divide n into m only the answer is
> > > > converted modulo n to be in the set {0,1,2,...n-1) if n > 0 or
> > > > (0,-1,-2, ....,n+1) if n <0
> > > 
> > > That would be helpful if it were obvious what it means
> > > to divide N into M when M and N can be negative. I don't
> > > think it is. Especially not when N is negative.
> > 
> > Division applies to all numbers positive and negative.
> > The rule is a/b = abs(a)/abs(b) when a>0 and b> 0 or a<0 and b<0
> > and a/b = -abs(a)/abs(b) when a<0 and b>0 or a<0 and b > 0
> 
> Sorry, I wasn't clear. I do know what division means
> for negative numbers! What *isn't* obvious is what
> "the remainder when you divide ... by ..." means when
> either or both "..." is negative. And, indeed, the
> results of MOD and REM are two different possible
> ways of specifying a meaning for the phrase. There
> is at least one other fairly plausible meaning, which
> is what you get if you stipulate that "the remainder"
> is always >= 0 and < |denominator|. That doesn't
> correspond to either MOD or REM, nor to the second
> value returned by any of the division operators.


What Ingvar says in the follow up was right.

To give an example that I already gave
the remainder when you divide 7 into -22 is -1 because
-22 = 7 *(-3) + (-1).
i.e. -22 = 7 * integer(-22/7) + (-1) 


If you don't understand this then you really dont understand
what division for negative numbers means.
From: Erann Gat
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <gat-0105031100290001@k-137-79-50-101.jpl.nasa.gov>
In article <····························@posting.google.com>,
··········@hotmail.com (larry) wrote:

> To give an example that I already gave
> the remainder when you divide 7 into -22 is -1 because
> -22 = 7 *(-3) + (-1).
> i.e. -22 = 7 * integer(-22/7) + (-1) 
> 
> 
> If you don't understand this then you really dont understand
> what division for negative numbers means.

One could just as easily argue:

The remainder when you divide 7 into -22 is 6 because
-22 = 7*(-4) + (6)
i.e. -22 = 7 * floor(-22/7) + (6)


I'd rethink that last sentence if I were you.

E.
From: larry
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <7b8f89d6.0305011330.1ad9ed5@posting.google.com>
···@jpl.nasa.gov (Erann Gat) wrote in message news:<····················@k-137-79-50-101.jpl.nasa.gov>...
> In article <····························@posting.google.com>,
> ··········@hotmail.com (larry) wrote:
> 
> > To give an example that I already gave
> > the remainder when you divide 7 into -22 is -1 because
> > -22 = 7 *(-3) + (-1).
> > i.e. -22 = 7 * integer(-22/7) + (-1) 
> > 
> > 
> > If you don't understand this then you really dont understand
> > what division for negative numbers means.
> 
> One could just as easily argue:
> 
> The remainder when you divide 7 into -22 is 6 because
> -22 = 7*(-4) + (6)
> i.e. -22 = 7 * floor(-22/7) + (6)
> 
> 
> I'd rethink that last sentence if I were you.
> 
> E.


No I think YOU should rethink your last sentence.
I said integer part NOT floor.
The definition of remainder of n divided into m is m - n* INTEGER-PART(m/n)
not m - n* FLOOR(m/n)-- this is the mathematical definition of remainder.

INTEGER is not the same as FLOOR
So remainder of 7 into -22 is -22 - 7*INTEGERPART(-22/7) =
-22 -7*(-3) = -22 + 21 = -1 NOT 6.
From: Erann Gat
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <gat-0105031518560001@k-137-79-50-101.jpl.nasa.gov>
In article <···························@posting.google.com>,
··········@hotmail.com (larry) wrote:

> ···@jpl.nasa.gov (Erann Gat) wrote in message
news:<····················@k-137-79-50-101.jpl.nasa.gov>...
> > In article <····························@posting.google.com>,
> > ··········@hotmail.com (larry) wrote:
> > 
> > > To give an example that I already gave
> > > the remainder when you divide 7 into -22 is -1 because
> > > -22 = 7 *(-3) + (-1).
> > > i.e. -22 = 7 * integer(-22/7) + (-1) 
> > > 
> > > 
> > > If you don't understand this then you really dont understand
> > > what division for negative numbers means.
> > 
> > One could just as easily argue:
> > 
> > The remainder when you divide 7 into -22 is 6 because
> > -22 = 7*(-4) + (6)
> > i.e. -22 = 7 * floor(-22/7) + (6)
> > 
> > 
> > I'd rethink that last sentence if I were you.
> > 
> > E.
> 
> 
> No I think YOU should rethink your last sentence.
> I said integer part NOT floor.

No you didn't, you said integer(-22/7) without defining what the integer
function does.

> The definition of remainder of n divided into m is m - n* INTEGER-PART(m/n)
> not m - n* FLOOR(m/n)-- this is the mathematical definition of remainder.

But that just begs the question of what the integer-part of a negative
fraction is.  There is more than one possibility, which is why there are
two remainder functions (mod and rem), and four integer-part functions
(floor, ceiling, round, and truncate).  There is no particular reason to
choose any one of these as "the correct" definition of integer-part
independent of any context.

> INTEGER is not the same as FLOOR

Right.  FLOOR is well defined.  INTEGER is not.

> So remainder of 7 into -22 is -22 - 7*INTEGERPART(-22/7) =
> -22 -7*(-3) = -22 + 21 = -1 NOT 6.

You really should stop pontificating now.  You are starting to look quite
foolish.

E.
From: Barry Margolin
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <aLhsa.43$va6.1129@paloalto-snr1.gtei.net>
In article <····················@k-137-79-50-101.jpl.nasa.gov>,
Erann Gat <···@jpl.nasa.gov> wrote:
>In article <···························@posting.google.com>,
>··········@hotmail.com (larry) wrote:
>> The definition of remainder of n divided into m is m - n* INTEGER-PART(m/n)
>> not m - n* FLOOR(m/n)-- this is the mathematical definition of remainder.
>
>But that just begs the question of what the integer-part of a negative
>fraction is.  There is more than one possibility, which is why there are
>two remainder functions (mod and rem), and four integer-part functions
>(floor, ceiling, round, and truncate).  There is no particular reason to
>choose any one of these as "the correct" definition of integer-part
>independent of any context.

Intuitively (IMHO), integer-part refers to the typical decimal notation,
which consists of an integer part and a fraction, separated by the decimal
point.  Or the common notation 3 2/3 for 11/3 and -1 3/8 for -11/8.
I.e. given a notation that consists of two parts, one of which is an
integer, the integer-part is that one.

Given this, integer-part is equivalent to CL's TRUNCATE.

-- 
Barry Margolin, ··············@level3.com
Genuity Managed Services, a Level(3) Company, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: larry
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <7b8f89d6.0305011335.479f2e0a@posting.google.com>
···@jpl.nasa.gov (Erann Gat) wrote in message news:<····················@k-137-79-50-101.jpl.nasa.gov>...
> In article <····························@posting.google.com>,
> ··········@hotmail.com (larry) wrote:
> 
> > To give an example that I already gave
> > the remainder when you divide 7 into -22 is -1 because
> > -22 = 7 *(-3) + (-1).
> > i.e. -22 = 7 * integer(-22/7) + (-1) 
> > 
> > 
> > If you don't understand this then you really dont understand
> > what division for negative numbers means.
> 
> One could just as easily argue:
> 
> The remainder when you divide 7 into -22 is 6 because
> -22 = 7*(-4) + (6)
> i.e. -22 = 7 * floor(-22/7) + (6)
> 
> 
> I'd rethink that last sentence if I were you.
> 
> E.

No you cannot just as easily argue that because
the term remainder already has a strict mathematical definition which 
is that the remainder of n divided into m is m-n*INTEGER(m/n).

This is the accepted mathematical definition. 
not  m-n*FLOOR(m/n). INTEGER by which I mean take the integer part is not
the same as FLOOR
From: Gareth McCaughan
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <slrnbb37ui.on.Gareth.McCaughan@g.local>
"larry" wrote:

> No you cannot just as easily argue that because
> the term remainder already has a strict mathematical definition which 
> is that the remainder of n divided into m is m-n*INTEGER(m/n).
> 
> This is the accepted mathematical definition. 
> not  m-n*FLOOR(m/n). INTEGER by which I mean take the integer part is not
> the same as FLOOR

Oh, really?

H E Rose, "A course in number theory", published by
Oxford University Press in 1988:

    DEFINITION If x is a real number then [x] denotes the
    largest integer less than or equal to x. [x] is called
    the *integer part* of x.

G H Hardy & E M Wright, "An introduction to the theory
of numbers", published by Oxford University Press in
1979 (this is the 5th edition):

    We write [x] for the "integral part of x", the largest
    integer which does not exceed x.

R L Graham, D E Knuth, O Patashnik, "Concrete mathematics",
published by Addison-Wesley in 1989:

    We start by covering the floor (greatest integer)
    and ceiling (least integer) functions, which are
    defined for all real x as follows:
        [x] = the greatest integer less than or equal to x
    ...
    We sometimes call [x] the *integer part* of x,
    since x = [x] + {x}.

(GKP actually use a notation for the floor function
that I can't represent in ordinary ASCII; imagine
square brackets without the horizontal part at the
top. There's a corresponding notation for the ceiling
function.)

Would you like to reconsider your statement that *the*
mathematical definition of "integer part" is not the
same as that of "floor"?

-- 
Gareth McCaughan  ················@pobox.com
.sig under construc
From: Gareth McCaughan
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <slrnbb33vo.on.Gareth.McCaughan@g.local>
"larry" wrote:

> > > Division applies to all numbers positive and negative.
> > > The rule is a/b = abs(a)/abs(b) when a>0 and b> 0 or a<0 and b<0
> > > and a/b = -abs(a)/abs(b) when a<0 and b>0 or a<0 and b > 0
> > 
> > Sorry, I wasn't clear. I do know what division means
> > for negative numbers! What *isn't* obvious is what
> > "the remainder when you divide ... by ..." means when
> > either or both "..." is negative. And, indeed, the
> > results of MOD and REM are two different possible
> > ways of specifying a meaning for the phrase. There
> > is at least one other fairly plausible meaning, which
> > is what you get if you stipulate that "the remainder"
> > is always >= 0 and < |denominator|. That doesn't
> > correspond to either MOD or REM, nor to the second
> > value returned by any of the division operators.
> 
> What Ingvar says in the follow up was right.
> 
> To give an example that I already gave
> the remainder when you divide 7 into -22 is -1 because
> -22 = 7 *(-3) + (-1).
> i.e. -22 = 7 * integer(-22/7) + (-1) 

It is also true that -22 = 7*(-4) + 6. The
function you have blithely written down as
"integer(-22/7)" *doesn't have a unique
obviously-best definition*.

> If you don't understand this then you really dont understand
> what division for negative numbers means.

Please don't patronize me. I have a PhD in pure
mathematics from one of the world's great universities,
and I also happen to understand perfectly well what
division for negative numbers means.

-- 
Gareth McCaughan  ················@pobox.com
.sig under construc
From: Florian Weimer
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <8765oxutdp.fsf@deneb.enyo.de>
Peter Seibel <·····@javamonkey.com> writes:

> Also, the spec says:
>
>   "mod and rem are generalizations of the modulus and remainder
>   functions respectively."
>
> but I haven't been able to find a definitive definition of "the"
> modulus and remainder functions. Most folks seem to think they're the
> same thing. Even checked a number theory book I have lying around. Any
> pointers?

Here's an excerpt from the Ada Reference Manual.  I'm not sure if it's
"the" definition, though.

4.5.5 Multiplying Operators
***************************

                           Static Semantics
  1. The multiplying operators * (multiplication), / (division), mod
     (modulus), and rem (remainder) are predefined for every specific
     integer type T:

  2.      function "*"  (Left, Right : T) return T
          function "/"  (Left, Right : T) return T
          function "mod"(Left, Right : T) return T
          function "rem"(Left, Right : T) return T

  3. Signed integer multiplication has its conventional meaning.

  4. Signed integer division and remainder are defined by the relation:

  5.      A = (A/B)*B + (A rem B)

  6. where (A rem B) has the sign of A and an absolute value less than
     the absolute value of B. Signed integer division satisfies the
     identity:

  7.      (-A)/B = -(A/B) = A/(-B)

  8. The signed integer modulus operator is defined such that the
     result of A mod B has the sign of B and an absolute value less
     than the absolute value of B; in addition, for some signed integer
     value N, this result satisfies the relation:

  9.      A = B*N + (A mod B)

[...]

     NOTES [not normative!]

 23. (17) For positive A and B, A/B is the quotient and A rem B is the
     remainder when A is divided by B. The following relations are
     satisfied by the rem operator:

 24.        A  rem (-B) =   A rem B
          (-A) rem   B  = -(A rem B)

 25. (18) For any signed integer K, the following identity holds:

 26.        A mod B   =   (A + K*B) mod B

 27. The relations between signed integer division, remainder, and
     modulus are illustrated by the following table:

 28.        A    B  A/B  A rem B  A mod B    A   B  A/B  A rem B  A mod B

 29.        10   5   2      0        0      -10  5   -2     0        0
            11   5   2      1        1      -11  5   -2    -1        4
            12   5   2      2        2      -12  5   -2    -2        3
            13   5   2      3        3      -13  5   -2    -3        2
            14   5   2      4        4      -14  5   -2    -4        1

 30.        A    B  A/B  A rem B  A mod B    A   B  A/B  A rem B  A mod B
            10  -5   -2     0        0      -10 -5    2     0        0
            11  -5   -2     1       -4      -11 -5    2    -1       -1
            12  -5   -2     2       -3      -12 -5    2    -2       -2
            13  -5   -2     3       -2      -13 -5    2    -3       -3
            14  -5   -2     4       -1      -14 -5    2    -4       -4
From: larry
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <7b8f89d6.0305021352.1055421c@posting.google.com>
Florian Weimer <··@deneb.enyo.de> wrote in message news:<··············@deneb.enyo.de>...
> Peter Seibel <·····@javamonkey.com> writes:
> 
> > Also, the spec says:
> >
> >   "mod and rem are generalizations of the modulus and remainder
> >   functions respectively."
> >
> > but I haven't been able to find a definitive definition of "the"
> > modulus and remainder functions. Most folks seem to think they're the
> > same thing. Even checked a number theory book I have lying around. Any
> > pointers?
What do you mean --you can't find anything definitive.
The spec goes on to say:

mod performs the operation floor on number and divisor and returns the
remainder of the floor operation.

rem performs the operation truncate on number and divisor and returns
the remainder of the truncate operation.

You can't get more definitive than that!
You can rewrite the above as the following to be more explicit.

mod(m,n) = m - n * floor(m/n)
and
rem(m,n) = m- n * truncate(m/n)

which is what I posted before.
From: Gareth McCaughan
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <slrnbb604a.on.Gareth.McCaughan@g.local>
"larry" wrote:

[Peter Seibel:]
> > > Also, the spec says:
> > >
> > >   "mod and rem are generalizations of the modulus and remainder
> > >   functions respectively."
> > >
> > > but I haven't been able to find a definitive definition of "the"
> > > modulus and remainder functions. Most folks seem to think they're the
> > > same thing. Even checked a number theory book I have lying around. Any
> > > pointers?
>
> What do you mean --you can't find anything definitive.
> The spec goes on to say:

He wasn't saying that he couldn't find definitions of MOD and REM
in the spec. He was saying that the spec says, presumably with
the hope of helping readers to match its definitions of MOD and
REM up with what they already know, that they are "generalizations
of the modulus and remainder function", and that he hasn't been
able to find any sign outside the spec that "the modulus and
remainder function" have generally-agreed definitions.

In the absence of such generally-agreed definitions, it's
not clear that the spec's reference to "the modulus and
remainder functions" really clarifies anything much.

> mod performs the operation floor on number and divisor and returns the
> remainder of the floor operation.
> 
> rem performs the operation truncate on number and divisor and returns
> the remainder of the truncate operation.
> 
> You can't get more definitive than that!
> You can rewrite the above as the following to be more explicit.
> 
> mod(m,n) = m - n * floor(m/n)
> and
> rem(m,n) = m- n * truncate(m/n)
> 
> which is what I posted before.

No, it isn't what you posted before. It differs from what
you posted before, for instance, in that it is neither
ambiguous nor wrong.

Well, actually it is slightly wrong, because what it
really says is that

    (mod m n) is the same as (- m (* n (floor m n)))
    (rem m n) is the same as (- m (* n (truncate m n)))

whereas your "more explicit" version says that

    (mod m n) is the same as (- m (* n (floor (/ m n))))
    (rem m n) is the same as (- m (* n (truncate (/ m n))))

I think (floor m n) and (floor (/ m n)) are permitted
to produce different results when given floating-point
operands, though I suspect they never do in practice. :-)

-- 
Gareth McCaughan  ················@pobox.com
.sig under construc
From: Raymond Toy
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <4nu1c94i65.fsf@edgedsp4.rtp.ericsson.se>
>>>>> "Gareth" == Gareth McCaughan <················@pobox.com> writes:

    Gareth> I think (floor m n) and (floor (/ m n)) are permitted
    Gareth> to produce different results when given floating-point
    Gareth> operands, though I suspect they never do in practice. :-)

Of course they give different values.  The second value of each is
very likely different. :-)

Ray
From: Peter Seibel
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <m3of2kj0dg.fsf@javamonkey.com>
··········@hotmail.com (larry) writes:

> Florian Weimer <··@deneb.enyo.de> wrote in message news:<··············@deneb.enyo.de>...
> > Peter Seibel <·····@javamonkey.com> writes:
> > 
> > > Also, the spec says:
> > >
> > >   "mod and rem are generalizations of the modulus and remainder
> > >   functions respectively."
> > >
> > > but I haven't been able to find a definitive definition of "the"
> > > modulus and remainder functions. Most folks seem to think they're the
> > > same thing. Even checked a number theory book I have lying around. Any
> > > pointers?

> What do you mean --you can't find anything definitive.
> The spec goes on to say:
> 
> mod performs the operation floor on number and divisor and returns the
> remainder of the floor operation.
> 
> rem performs the operation truncate on number and divisor and returns
> the remainder of the truncate operation.
> 
> You can't get more definitive than that!

Maybe I was paying *too* much attention to (possibily irrelevant)
detail but here's how I read it:

  mod and rem are generalizations of the modulus and remainder
  functions respectively.

So I take it that as saying that "mod" is somehow different from
"modulus" and similarly "rem" is different from "remainder".

The spec goes on, in the definitions you quoted, to define "mod" and
"rem", *not* "modulus" and "remainder". 

Because "modulus" and "remainder" were just refered to, without
definition (that I could find) I assumed those names were referring to
something believed to be in common knowledge outside of Common Lisp,
such as in mathematics.

-Peter


-- 
Peter Seibel                                      ·····@javamonkey.com

  The intellectual level needed   for  system design is  in  general
  grossly  underestimated. I am  convinced  more than ever that this
  type of work is very difficult and that every effort to do it with
  other than the best people is doomed to either failure or moderate
  success at enormous expense. --Edsger Dijkstra
From: Matt Curtin
Subject: Re: Barbie and I both say ...
Date: 
Message-ID: <867k92eu50.fsf@rowlf.interhack.net>
Peter Seibel <·····@javamonkey.com> writes:

> Also, the spec says:
> 
>   "mod and rem are generalizations of the modulus and remainder
>   functions respectively."
> 
> but I haven't been able to find a definitive definition of "the"
> modulus and remainder functions. Most folks seem to think they're the
> same thing.

Remainder rounds toward zero, modulus rounds toward negative infinity.
The % operator in C and C++ is "remainder", not "modulus" as
incorrectly asserted in some references.  Someone told me that C99
specifically (and correctly) called it remainder, but I haven't seen
it myself.  Sun's Java specs correctly identify % as remainder.

C++ with %:
  i =  15, j =  12, i % j =  3
  i = -15, j =  12, i % j = -3
  i =  15, j = -12, i % j =  3
  i = -15, j = -12, i % j = -3

Common Lisp with REM:

  (rem 15 12)   =>  3
  (rem -15 12)  => -3
  (rem 15 -12)  =>  3
  (rem -15 -12) => -3

Common Lisp with MOD:

  (mod 15 12)   =>  3
  (mod -15 12)  =>  9
  (mod 15 -12)  => -9
  (mod -15 -12) => -3

This behavior is correct.

See TACP, volumes 1 and 2, note "mod" and "remainder" in the indices.

-- 
Matt Curtin, CISSP, IAM, INTP.  Keywords: Lisp, Unix, Internet, INFOSEC.
Founder, Interhack Corporation +1 614 545 HACK http://web.interhack.com/
Author of /Developing Trust: Online Privacy and Security/ (Apress, 2001)