[swift-evolution] Integers and their protocols

Ben Rimmington me at benrimmington.com
Sun Jun 18 05:30:42 CDT 2017


Re: <https://gist.github.com/xwu/d68baefaae9e9291d2e65bd12ad51be2#integers-from-a-floating-point-source>

There are also some commented-out requirements, which were accepted for SE-0067.

(One requirement is obsoleted by the Numeric protocol).

Can these be implemented yet, based on your experience?

FloatingPoint
-------------

<https://github.com/apple/swift/blob/5ec0563c167a720e8c2c99390df6e032a24f2403/stdlib/public/core/FloatingPoint.swift.gyb#L256-L273>

>   /*  TODO: Implement the following APIs once a revised integer protocol is
>       introduced that allows for them to be implemented. In particular, we
>       need to have an "index of most significant bit" operation and "get
>       absolute value as unsigned type" operation on the Integer protocol.
> 
>   /// Creates the closest representable value to the given integer.
>   ///
>   /// - Parameter value: The integer to represent as a floating-point value.
>   init<Source: Integer>(_ value: Source)
> 
>   /// Creates a value that exactly represents the given integer.
>   ///
>   /// If the given integer is outside the representable range of the type, the
>   /// result is `nil`.
>   ///
>   /// - Parameter value: The integer to represent as a floating-point value.
>   init?<Source: Integer>(exactly value: Source)
>   */

BinaryFloatingPoint
-------------------

<https://github.com/apple/swift/blob/5ec0563c167a720e8c2c99390df6e032a24f2403/stdlib/public/core/FloatingPoint.swift.gyb#L1476-L1491>

>   /*  TODO: Implement these once it becomes possible to do so (requires revised
>       Integer protocol).
>   /// Creates a new instance from the given value, rounded to the closest
>   /// possible representation.
>   ///
>   /// - Parameter value: A floating-point value.
>   init<Source: BinaryFloatingPoint>(_ value: Source)
> 
>   /// Creates a new instance equivalent to the exact given value.
>   ///
>   /// If the value you pass as `value` can't be represented exactly, the result
>   /// of this initializer is `nil`.
>   ///
>   /// - Parameter value: A floating-point value to represent.
>   init?<Source: BinaryFloatingPoint>(exactly value: Source)
>   */



More information about the swift-evolution mailing list