[swift-evolution] Proposal: Remove % operator for floating-point types
crk at akkyra.com
Sat Dec 19 18:47:30 CST 2015
> I don't think this solves the precision issue. It makes it harder to use fmod, but doesn't stop people wanting to, life finds a way. I completely agree with your rationale, but if someone wants mod they'll use mod. The question probably comes down to whether stackoverflow will tell them to use fmod or remainder.
As someone who does mostly numerical programming, I’m okay with the proposed change. I do agree with Andrew, however, that it is unlikely to have much of an effect on whether the floating point modulus operation is used in an enlightened way.
There’s always hope, though, and drawing a clear distinction between the integer and floating point operations seems appropriate in light of the aspirations for Swift to be a teaching language.
I have mixed feelings at best about having instance methods for fmod() and remainder(), though. The draft Swift API design guidelines suggest preferring methods and properties except “when function syntax is part of the established domain notation”. The use of freestanding functions for mathematical operations is well established in other programming languages, and I think it generally is a better match to mathematical notation (i.e., more equation-like).
I’m strongly against adding an identically named mod() for integers. Having the same method for integers and floats is just reintroducing the original problem by making the two operations seem equivalent.
> On Sat, Dec 19, 2015 at 12:10 PM, Joe Groff via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> On Dec 18, 2015, at 3:40 PM, August Joki via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>> On 18 Dec, 2015, at 15:31, Kevin Ballard via swift-evolution <swift-evolution at swift.org <mailto: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 <mailto:scanon at apple.com>> wrote:
>>>>>> On Dec 18, 2015, at 5:57 PM, Chris Lattner <clattner at apple.com <mailto:clattner at apple.com>> wrote:
>>>>>> On Dec 18, 2015, at 1:12 PM, Stephen Canon via swift-evolution <swift-evolution at swift.org <mailto: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?
> We don't offer protocols that abstract over both integer and float arithmetic. They behave differently, and require different algorithms and interfaces. Floating-point types gaining a `mod` member does not necessarily mean integer types should get the same.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-evolution