[swift-evolution] [Proposal draft] Enhanced floating-point protocols

Nicola Salmoria nicola.salmoria at gmail.com
Fri Apr 15 01:48:11 CDT 2016

>   /// The quotient of `self` dividing by `rhs`.
>   //  Arithmetic provides a default implementation of this method in terms
>   //  of the mutating `divide` operation.
>   @warn_unused_result
>   func divided(by rhs: Self) -> Self
>   /// Divides `self` by `rhs`.
>   mutating func divide(by rhs: Self)

When dealing with integer arithmetic, I often find useful a `divmod` function which produces a (quotient, remainder) pair.
It could be argued that such a pair is the primary result of division on integers. It would be great to have such a function included in the design.

> /// SignedArithmetic protocol will only be conformed to by signed numbers,
> /// otherwise it would be possible to negate an unsigned value.
> ///
> /// The only method of this protocol has the default implementation in an
> /// extension, that uses a parameterless initializer and subtraction.
> public protocol SignedArithmetic : Arithmetic {
>   func negate() -> Self
> }

It might make sense to also have a

public protocol InvertibleArithmetic : Arithmetic {
  func inverted() -> Self

FloatingPoint would conform to this protocol, returning 1/x, while integer types would not.


More information about the swift-evolution mailing list