[swift-evolution] Proposal: Remove % operator for floating-point types

Kevin Ballard kevin at sb.org
Fri Dec 18 17:59:31 CST 2015


On Fri, Dec 18, 2015, at 03:40 PM, August Joki wrote:
>
>> On 18 Dec, 2015, at 15:31, Kevin Ballard via swift-evolution <swift-
>> evolution at swift.org> wrote:
>>
>> On Fri, Dec 18, 2015, at 03:04 PM, Chris Lattner via swift-
>> evolution wrote:
>>>
>>>> On Dec 18, 2015, at 3:03 PM, Stephen Canon <scanon at apple.com>
>>>> wrote:
>>>>
>>>>
>>>>> On Dec 18, 2015, at 5:57 PM, Chris Lattner <clattner at apple.com>
>>>>> wrote:
>>>>>
>>>>> On Dec 18, 2015, at 1:12 PM, Stephen Canon via swift-evolution <swift-
>>>>> evolution at swift.org> wrote:
>>>>>> Hi everybody —
>>>>>>
>>>>>> I’d like to propose removing the “%” operator for floating-point
>>>>>> types.
>>>>>
>>>>> I support removing this - it is actively harmful for a surprising
>>>>> operation like this to have such short and inviting syntax.  As
>>>>> asked downthread, have you given any thought into whether a
>>>>> Decimal type would support this operation?
>>>>
>>>> The same concerns apply to decimal.  It makes sense to have the
>>>> operation (for both binary and decimal floating-point) as
>>>> “Type.remainder(a,b)” or a free function like "remainder(a, b)”,
>>>> but I would prefer not to use the operator “%” for it because it
>>>> behaves very differently from integer %, and in ways that are not
>>>> at all obvious to most users.
>>>
>>> Great, I’d prefer decimal and the float types to have a consistent
>>> interface where possible.  +1 from me on the proposal.
>>
>> Agreed. +1 from me too. The `10.0 % 0.1` behavior is sufficiently
>> surprising that I think that justifies removal by itself.
>>
>> I'm also in favor of adding mod and remainder as instance methods of
>> the floating point types, e.g. `10.0.mod(0.1)` (as opposed to static
>> methods or free functions).
> Shouldn’t that be added to any Arithmetic type?

IntegerArithmeticType already defines the % operator, adding a mod()
method would just be duplication. If we have Float.remainder() then
arguably we might want an integral remainder too, although remainder
turns out to be rarely what people want (at least, for integral
calculations), which is why the % operator is mod.

-Kevin Ballard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151218/8fcb7cae/attachment.html>


More information about the swift-evolution mailing list