[swift-evolution] [Pitch] 'Double modulo' operator

Pyry Jahkola pyry.jahkola at iki.fi
Mon May 23 13:59:48 CDT 2016


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/a1bff379/attachment.html>


More information about the swift-evolution mailing list