[swift-evolution] [Pitch] 'Double modulo' operator
Adam Nemecek
adamnemecek at gmail.com
Mon May 23 16:04:01 CDT 2016
Seems like making it an operator will cause more issues than it solves. I think that floordiv and mod functions are probably the way to go. I'm going to wait a little longer for more feedback and write up a proposal at some point.
On Mon, May 23, 2016 at 1:20 PM -0700, "Pyry Jahkola" <pyry.jahkola at iki.fi> wrote:
You have both good points.
In that case, if we still want to define new operators here (which might not please everybody), then I'd suggest finding a common prefix or suffix character (e.g. `^/` and `^%`) for both the floored division and floored modulo (for one, because we can't define `//` as an operator).
But I think it'd be clearest to just make them both top-level functions, e.g. `floordiv(p, q)` and `floormod(p, q)`. If anyone uses either a lot, and likes to make it terse, it's easy to define a private operator wrapper for it.
— Pyry
Tony Allevato wrote:
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/8c14b412/attachment-0001.html>
More information about the swift-evolution
mailing list