Uploaded image for project: 'Swift'
  1. Swift
  2. SR-3317

Literal protocol for decimal literals should support precise decimal accuracy

    XMLWordPrintable

    Details

      Description

      A decimal literal such as 1.23 currently constructs a value via the ExpressibleByFloatLiteral protocol, which eagerly parses the literal into a binary floating-point value, losing accuracy. ExpressibleByFloatLiteral is unsuitable for supporting decimal float or fixed-point types. We should introduce an ExpressibleByDecimalLiteral protocol that can receive the significand, exponent, and sign components of a decimal literal as precise integers, so that a conforming type can produce a precise result. It could look something like this:

      protocol ExpressibleByDecimalLiteral {
        associatedtype Exponent: ExpressibleByIntegerLiteral
        associatedtype Significand: ExpressibleByIntegerLiteral
        init(negative: Bool, significand: Significand, exponent: Exponent)
      }
      

      ExpressibleByFloatLiteral could be made into a refinement of ExpressibleByDecimalLiteral as well.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              jckarter Joe Groff
              Votes:
              1 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: