Skip to content

[SR-3317] Literal protocol for decimal literals should support precise decimal accuracy #45905

@jckarter

Description

@jckarter
Previous ID SR-3317
Radar rdar://problem/31414910
Original Reporter @jckarter
Type Bug
Status Resolved
Resolution Duplicate
Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, AffectsABI, StarterProposal
Assignee None
Priority Medium

md5: 9de0502502b261886bad92d969cec324

duplicates:

  • SR-920 Re-design builtin compiler protocols for literal convertible types

is duplicated by:

  • SR-5678 Decimal initializer result is unexpected

relates to:

  • SR-3970 Rework ExpressibleBy(Integer|Float)Literal protocols

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    affects ABIFlag: Affects ABIcompilerThe Swift compiler itselfstandard libraryArea: Standard library umbrellaswift evolution proposal neededFlag → feature: A feature that warrants a Swift evolution proposal

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions