[swift-evolution] Integers and their protocols

Xiaodi Wu xiaodi.wu at gmail.com
Sun Jun 18 14:47:06 CDT 2017


On Sun, Jun 18, 2017 at 05:30 Ben Rimmington <me at benrimmington.com> wrote:

> 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?


Based on a cursory thinking through only:

The first one, yes (if we observe that Integer is now BinaryInteger), but
probably inefficiently as it would have to use `words`, I think. If the
requirement were on BinaryFloatingPoint and T were constrained to
BinaryInteger & FixedWidthInteger, I think it could be more efficient. Of
course, both could be added. We could not remove the concrete initializer
from a UInt source, though, as initializing from `words` would rely on that.

The second one, I think so, but I haven't tried to make sure.


> 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)
> >   */
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170618/b743aef0/attachment.html>


More information about the swift-evolution mailing list