[swift-evolution] [Pitch] 'Double modulo' operator
Tony Allevato
allevato at google.com
Mon May 23 14:30:25 CDT 2016
I've had to write a "true mod" function enough times across different
projects (usually for circular buffer indexing or angular arithmetic) that
it would absolutely support its inclusion in stdlib (for both integer and
floating point types).
The `%` operator historically has been implemented as "remainder" though
(even when called "modulus"), so while it would be nice to be "pure" and
have `%` do the right thing, I think it would be too confusing for users to
have the operator have a subtly different meaning compared to every other
programming language that looks like it.
I'm not crazy about the `%%` spelling, but I can't think of anything better
either at the moment.
On Mon, May 23, 2016 at 12:24 PM Adam Nemecek via swift-evolution <
swift-evolution at swift.org> wrote:
> 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.
>>>
>>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160523/b79a6d30/attachment.html>
More information about the swift-evolution
mailing list