[rust-dev] Division and modulo for signed numbers

Graydon Hoare graydon at mozilla.com
Thu Apr 25 09:25:30 PDT 2013

On 13-04-25 07:52 AM, Diggory Hardy wrote:
>> My opinion (that nobody will follow, but I still give it) is that integers
>> should not have the "/" operator at all. This was one of the bad choices of
>> C (or maybe of a previous language).
> Hmm, maybe, though I can imagine plenty of people being surprised at that.
> What really gets me though is that % is commonly called the "mod" operator and
> yet has nothing to do with modular arithmatic (I actually wrote a blog post
> about it a few months back: [1]). If it were my choice I'd either make "x % y"
> do real modular arithmatic (possibly even throwing if y is not positive) or
> have no % operator (just mod and rem keywords).

While it's true that people often pronounce % as "mod", the fact is most 
of the languages in the lineage we're looking at treat it as "rem".


50 languages in that list expose 'remainder' and 19 of them map it to 
'%'. As well, as a "systems language", it _is_ salient that the 
instructions on the CPUs we're targeting and the code generator IR for 
said machines (LLVM) expose a remainder operation, not a modulo one. Of 
the 35 languages that expose _anything_ that does "proper mod", only 
interpreted/script languages (TCL, Perl, Python, Ruby, Lua, Rexx, Pike 
and Dart) call it %. That's not our family. I'm sorry; if we're arguing 
over "what the % symbol means", it means remainder in "our" language 
family (the one including C, C++, C#, D, Go, F#, Java, Scala).

(more gruesome comparisons available here: 

There are other questions to answer in this thread. We had a complex set 
of conversations yesterday on IRC concerning exposure of multiple named 
methods for the "other variants" -- ceiling, floor and truncating 
division, in particular. We may need to expose all 3, and it might be 
the case that calling any of them 'quot' is just misleading; it's not 
clear to me yet whether there's a consistent method _name_ to assign '/' 
to (floating point divide seems to do the opposite of integer divide on 
chips that have both).

But I don't think it's wise to map % to 'mod' if we're exposing both 
'mod' and 'rem'. That's a separate issue and one with (I think) a 
simpler answer for us.


More information about the Rust-dev mailing list