Proposal: Remove % operator for floating-point types

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@swift.org <mailto:swift-evolution@swift.org>> wrote:

On Dec 18, 2015, at 3:40 PM, August Joki via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

-August

On 18 Dec, 2015, at 15:31, Kevin Ballard via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@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@apple.com <mailto:scanon@apple.com>> wrote:

On Dec 18, 2015, at 5:57 PM, Chris Lattner <clattner@apple.com <mailto:clattner@apple.com>> wrote:

On Dec 18, 2015, at 1:12 PM, Stephen Canon via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@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.

-Joe

It occurs to me that we already have precedent for a Swift global
function (not operator) that implements a mathematic operation, and that
is abs(). There is an AbsoluteValuable protocol that offers a static
abs() method that can be used to override the default implementation
(which is comparing the value against zero and negating it), but the
intended usage is always to invoke the global function.

That said, I wouldn't be opposed to changing this around so abs() is a
method of SignedNumberType (and AbsoluteValuable would go away
entirely, since types that conform to SignedNumberType can just
override the method).

I also think there's a slight difference between "basic" functions like
abs() and mod() (things that are useful in non-mathematical code) versus
more general "math-y" functions like sin(). But I don't have strong
feelings on this; as you say, the draft guidelines do say to use
functions when function syntax is the established domain notation.

-Kevin Ballard

···

On Sat, Dec 19, 2015, at 04:47 PM, Charles Kissinger via swift-evolution wrote:

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@swift.org> wrote:

On Dec 18, 2015, at 3:40 PM, August Joki via swift-evolution <swift- >>>> evolution@swift.org> wrote:

-August

On 18 Dec, 2015, at 15:31, Kevin Ballard via swift-evolution <swift- >>>>> evolution@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@apple.com> >>>>>>> wrote:

On Dec 18, 2015, at 5:57 PM, Chris Lattner <clattner@apple.com> >>>>>>>> wrote:

On Dec 18, 2015, at 1:12 PM, Stephen Canon via swift-evolution >>>>>>>> <swift-evolution@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.

-Joe

_________________________________________________
swift-evolution mailing list swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution