Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Integer arithmetic: mod
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Chris Hecker <checker@d...>
Subject: Re: [Caml-list] Integer arithmetic: mod

> Now, I could live with -1 mod 10 = -1, but why is it platform-dependent?

There are two ways to define mod for negative numbers.  They're both mathematically valid (meaning you can write equations with them both).

Most computer languages (and chips) simply say "(a/b)*b + a mod b = a" and leave it at that.  Unfortunately, people (and language and chip designers) assume (-4)/3 = -1 (truncate towards zero) rather than -2 (floor), and that seals the fate of (-4) mod 3 to be -1 instead of 2.

Here's a good discussion:
http://forum.swarthmore.edu/dr.math/problems/anne.4.28.99.html

>The reason is that whenever such a behaviour is platform-dependent, you
>can't use it at all, as long as you want to have your programs portable.
>Beside that the speed penalty (are there other problems with this
>approach?) would probably be not too bad, would it?

I think caml did the right thing (just let the cpu do it) with a bad situation (inconsistencies).  You're free to define your own versions if the built in ones don't work for you, and this doesn't penalize everybody (some people like the "remainder" result rather than the "floor" result).

Chris

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr