[rust-dev] Division and modulo for signed numbers

Matthieu Monrocq matthieu.monrocq at gmail.com
Thu Apr 25 10:12:16 PDT 2013


I was thinking about the mapping of / and % and indeed maybe the simplest
option is not to map them.

Of course, having an infix syntax would make things easier: 5 % 3 vs 5 rem
3 vs 5.rem(3), in increasing order of typed keys (and visual noise for the
latter ?).

On the other hand, if there is no mapping I can imagine people keeping
asking whether to use mod or rem...

-- Matthieu



On Thu, Apr 25, 2013 at 6:25 PM, Graydon Hoare <graydon at mozilla.com> wrote:

> 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".
>
> http://en.wikipedia.org/wiki/**Modulo_operation<http://en.wikipedia.org/wiki/Modulo_operation>
>
> 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: http://rigaux.org/language-**
> study/syntax-across-languages/**Mthmt.html#MthmtcDBQAM<http://rigaux.org/language-study/syntax-across-languages/Mthmt.html#MthmtcDBQAM>)
>
> 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.
>
> -Graydon
>
>
> ______________________________**_________________
> Rust-dev mailing list
> Rust-dev at mozilla.org
> https://mail.mozilla.org/**listinfo/rust-dev<https://mail.mozilla.org/listinfo/rust-dev>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20130425/ea74fed6/attachment.html>


More information about the Rust-dev mailing list