[swift-evolution] [Discussion] Arbitrary precision integer and float literal protocols

David Hart david at hartbit.com
Mon Apr 3 00:26:02 CDT 2017



> On 31 Mar 2017, at 04:21, David Sweeris <davesweeris at mac.com> wrote:
> 
> 
> 
> 
> Sent from my iPhone
> On Mar 30, 2017, at 14:56, David Hart via swift-evolution <swift-evolution at swift.org> wrote:
> 
>> Hello swift-evolution,
>> 
>> I’m working on a proposal for arbitrary precision integer and float literal protocols to complement ExpressibleByIntegerLiteral and ExpressibleByFloatLiteral. I’m looking for feedback and help in verifying the logic - I have doubts about endianness.
>> 
>> Fire away!
>> 
>> https://github.com/hartbit/swift-evolution/blob/arbitrary-precision-integer-float-protocol/proposals/XXXX-arbitrary-precision-integer-float-literal-protocols.md
>> 
>> Arbitrary precision integer and float literal procotols
>> Proposal: SE-XXXX
>> Authors: David Hart
>> Review Manager: TBD
>> Status: TBD
>> Introduction
>> 
>> We propose a pair of protocols extending ExpressibleByIntegerLiteral and ExpressibleByFloatLiteral to allow initializing conforming types with arbitrary precision integer and floating-point literals.
>> 
>> Motivation
>> 
>> The current protocols ExpressibleByIntegerLiteral and ExpressibleByFloatLiteral are simple and work well but don't support arbitrary precision literal values. Replacing those protocols is a non-goal as they provide a simple interface for work well for most cases. Therefore, to support initializing big integer and big float types with literal values, we need new protocols.
>> 
>> Proposed solution
>> 
>> We propose:
>> 
>> renaming FloatingPointSign into Sign,
>> introducing a ExpressibleByArbitraryPrecisionIntegerLiteral protocol that extends ExpressibleByIntegerLiteral and provides a new arbitrary precision initializer,
>> introducing a ExpressibleByArbitraryPrecisionFloatLiteral protocol that extends ExpressibleByFloatLiteral and provides a new arbitrary precision initializer.
>> Here is the corresponding code:
>> 
>> /// The sign of a number.
>> public enum Sign : Int {
>>     
>>     /// The sign for a positive value.
>>     case plus
>>     
>>     /// The sign for a negative value.
>>     case minus
>> }
>> 
>> /// A type that can be initialized with an arbitrary precision integer literal.
>> public protocol ExpressibleByArbitraryPrecisionIntegerLiteral:
>>     ExpressibleByIntegerLiteral {
>>     
>>     /// Creates an instance initialized with the sign and memory value of the
>>     /// arbitrary precision integer literal.
>>     ///
>>     /// Do not call this initializer directly. Instead, initialize a variable or
>>     /// constant using an arbitrary precision integer literal. For example:
>>     ///
>>     ///     let x = 123_456_789_123_456_789_123_456_789
>>     ///
>>     /// In this example, the assignment to the `x` constant calls this integer
>>     /// literal initializer behind the scenes with `sign` as `.plus` and
>>     /// `buffer` as the memory [0x0000000000661EFD, 0xF2E3B19F7C045F15].
>>     ///
>>     /// - Parameters:
>>     ///     sign: The sign of the integer value.
>>     ///     buffer: The memory value of the integer.
>>     init(sign: Sign, buffer: UnsafeBufferPointer<UInt64>)
>> }
>> 
>> /// A type that can be initialized with an arbitrary precision float literal.
>> public protocol ExpressibleByArbitraryPrecisionFloatLiteral:
>>     ExpressibleByFloatLiteral {
>>     
>>     /// Creates an instance initialized with the sign, exponent and
>>     /// significand of the arbitrary precision float literal.
>>     ///
>>     /// Do not call this initializer directly. Instead, initialize a variable or
>>     /// constant using an arbitrary precision integer literal. For example:
>>     ///
>>     ///     let x = 123_456_789_123_456.789_123_456_789
>>     ///
>>     /// In this example, the assignment to the `x` constant calls this float
>>     /// literal initializer behind the scenes with `sign` as `.plus`, `exponent`
>>     /// as the memory [0x0000000000661EFD, 0xF2E3B19F7C045F15] and `significand`
>>     /// with value [0x000000000000000C].
>>     ///
>>     /// - Parameters:
>>     ///     sign: The sign of the integer value.
>>     ///     exponent: The memory value of the exponent.
>>     ///     significand: The memory value of the significand.
>>     init(
>>         sign: Sign,
>>         exponent: UnsafeBufferPointer<UInt64>,
>>         significand: UnsafeBufferPointer<UInt64>)
>> }
>> 
>> 
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> 
> IIRC, some branches of math don't consider 0 to be positive or negative, so I might add a `zero` case in `Sign`.

That will never happen: the literal value 0 on a type conforming to one of those protocols will call the super-protocol's initializer because it is representable with all built-in types.

> Also, I'm not sure new protocols are actually necessary. I think I remember hearing somewhere that LLVM supports integer literals up to something like 400 bits? If so, we'd merely need a type that exposes those bytes to set the relevant types `IntegerLiteralType` associated type.

I am clueless in this regard so please let me know if you find more information.

> - Dave Sweeris 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170403/b30bfea6/attachment.html>


More information about the swift-evolution mailing list