Modulo

129 views
Skip to first unread message

Mark Engelberg

unread,
Dec 22, 2008, 7:04:29 AM12/22/08
to clo...@googlegroups.com
Anyone know why there is no modulo or mod function in Clojure's core?
I know there is a rem function, but that's not the same thing. mod
and rem behave differently when the first number is negative.

(mod -2 5) -> 3
(rem -2 5) -> -2

modulo n is important for making things stay in the range from 0 to
n-1, for example, if you're working with vectors of length n. You
need to ensure that negative numbers wrap around to the positive
range. rem does not do this.

I'm more familiar with Scheme than CL, and I know PLT Scheme provides
both modulo and remainder. But according to the Wikipedia
(http://en.wikipedia.org/wiki/Modulo_operation), Common Lisp also
provides both mod and rem functions. So why not Clojure? Is it a
Java defeciency?

Mark Engelberg

unread,
Dec 29, 2008, 12:44:41 AM12/29/08
to clo...@googlegroups.com
So I understand that we're supposed to discuss ideas here to try to
gain "mindshare" which is why last week I brought up the issue with
Clojure missing mod (which works differently from rem on negative
numbers). So really, no one else cares about this, or was the post
just lost in the shuffle of holiday time?

Rich Hickey

unread,
Dec 29, 2008, 1:01:54 AM12/29/08
to Clojure


On Dec 29, 12:44 am, "Mark Engelberg" <mark.engelb...@gmail.com>
wrote:
> So I understand that we're supposed to discuss ideas here to try to
> gain "mindshare" which is why last week I brought up the issue with
> Clojure missing mod (which works differently from rem on negative
> numbers). So really, no one else cares about this, or was the post
> just lost in the shuffle of holiday time?
>
> On Mon, Dec 22, 2008 at 4:04 AM, Mark Engelberg
>
> <mark.engelb...@gmail.com> wrote:
> > Anyone know why there is no modulo or mod function in Clojure's core?
> > I know there is a rem function, but that's not the same thing. mod
> > and rem behave differently when the first number is negative.
>
> > (mod -2 5) -> 3
> > (rem -2 5) -> -2
>
> > modulo n is important for making things stay in the range from 0 to
> > n-1, for example, if you're working with vectors of length n. You
> > need to ensure that negative numbers wrap around to the positive
> > range. rem does not do this.
>
> > I'm more familiar with Scheme than CL, and I know PLT Scheme provides
> > both modulo and remainder. But according to the Wikipedia
> > (http://en.wikipedia.org/wiki/Modulo_operation), Common Lisp also
> > provides both mod and rem functions. So why not Clojure? Is it a
> > Java defeciency?

Holidays here.

Feel free to create an issue for this:

http://code.google.com/p/clojure/issues/list

Patch (from a registered contributor) welcome.

Rich

Vincent Foley

unread,
Dec 29, 2008, 1:25:47 AM12/29/08
to Clojure
I suppose then that we would also need div. Using GHCi here:

Prelude> (-3) `div` 2
-2
Prelude> (-3) `quot` 2
-1

Achim Passen

unread,
Dec 29, 2008, 7:50:44 AM12/29/08
to Clojure
Hi!

I uploaded my attempt at this to the files section of this group:
http://clojure.googlegroups.com/web/mod-sgn.diff

It adds the modulus operator plus, as a by-product, the signum
operator (sgn).

Comments most welcome!

Kind regards,
achim

Rich Hickey

unread,
Dec 29, 2008, 2:35:08 PM12/29/08
to Clojure


On Dec 29, 7:50 am, Achim Passen <achim.pas...@gmail.com> wrote:
> Hi!
>
> I uploaded my attempt at this to the files section of this group:
> http://clojure.googlegroups.com/web/mod-sgn.diff
>
> It adds the modulus operator plus, as a by-product, the signum
> operator (sgn).
>
> Comments most welcome!
>

Thanks! Could you (and everyone) please submit patches as attachments
on the relevant issue?

I want to get away from patches in messages and in the files section.

Thanks,

Rich
Reply all
Reply to author
Forward
0 new messages