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

Stephen Canon scanon at apple.com
Fri Apr 15 11:35:18 CDT 2016


> On Apr 14, 2016, at 11:48 PM, Nicola Salmoria <nicola.salmoria at gmail.com> wrote:
> 
>>  /// 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.

I believe that it’s present in the latest draft of the Integer protocols.

>> /// 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.

That’s a very reasonable suggestion (and one that can easily be added separately if it doesn’t make it into this batch of changes).

– Steve
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160415/cbc5d905/attachment.html>


More information about the swift-evolution mailing list