[swift-evolution] [Pitch] 'Double modulo' operator
Adam Nemecek
adamnemecek at gmail.com
Mon May 23 14:23:36 CDT 2016
That kind of breaks backwards compatibility.
Also currently in swift, the % operator is called the remainder operator,
not the modulo operator, so technically the current implementation is
correct. In Haskell for example, there are two functions, rem (as in
remainder) and mod (as in modulo). I guess we could leave % to mean
remainder and introduce a mod function?
On Mon, May 23, 2016 at 11:59 AM, Pyry Jahkola <pyry.jahkola at iki.fi> wrote:
> I wouldn't mind if the standard `%` operator worked like this and there
> would be another top-level function `mod(p, q)` that worked like `%` in C.
> The only times that I've ever needed the modulo operation (
> https://en.wikipedia.org/wiki/Modulo_operation) to behave *some way* on
> negative values it's always been the suggested
> remainder-of-flooring-division way.
>
> On the other hand, there's a performance penalty (avoidable by using the
> other mod operation), so I can understand if not everyone agrees.
>
> And like Steve said below, then we'll need a flooring division function
> (or operator) as well. And a flooring `(f, r) = divmod(p, q)` too, I
> suppose.
>
> In any case, I'm probably +1 if a well-thought proposal is brought to the
> table.
>
> — Pyry
>
> Adam Nemecek wrote:
>
> Would you want to make this a function? Or an operator but a different one?
>
> On Mon, May 23, 2016 at 7:30 AM, Stephen Canon <scanon at apple.com> wrote:
>
>> I’m not really sold on the `%%` spelling, but I think the operation
>> itself is worth exposing. This is the remainder of a “flooring” division
>> (as opposed to the C-family “truncating” division[1]). If we do provide
>> it, we should also provide the accompanying divide operation.
>>
>> – Steve
>>
>> [1] there are several other ways to define division beyond these two:
>> remainder is always positive, remainder is closest to zero, etc.
>> Truncating and flooring division are the most common by a wide margin,
>> however.
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160523/05764d7c/attachment.html>
More information about the swift-evolution
mailing list