[swift-users] Decimal to Double?
Nevin Brackett-Rozinsky
nevin.brackettrozinsky at gmail.com
Mon Nov 28 13:42:12 CST 2016
On Mon, Nov 28, 2016 at 2:21 PM, Joe Groff via swift-users <
swift-users at swift.org> wrote:
> ExpressibleByFloatLiteral uses a binary float initial value and so doesn't
> guarantee decimal accuracy
That seems like a flaw in ExpressibleByFloatLiteral that should be fixed.
Did anyone ever submit a proposal following the discussion in
“[swift-evolution] Allow FloatLiteralType in FloatLiteralConvertible to be
aliased to String” where somebody—*runs to check*…oh it was you!—described
the proper solution?
Nevin
On Mon, Nov 28, 2016 at 2:21 PM, Joe Groff via swift-users <
swift-users at swift.org> wrote:
>
> > On Nov 28, 2016, at 9:22 AM, Alex Blewitt via swift-users <
> swift-users at swift.org> wrote:
> >
> > NSDecimal conforms to the ExpressibleBy(Float|Integer)Literal, but that
> just means it can be created from a literal value, not the other way around:
> >
> > https://github.com/apple/swift-corelibs-foundation/blob/
> 108a5b0006912c6cf6b59e4954255237bf01b67a/Foundation/
> NSDecimal.swift#L319-L329
> >
> >
> >
> > extension Decimal : ExpressibleByFloatLiteral {
> > public init(floatLiteral value: Double) {
> > self.init(value)
> > }
> > }
> >
> > extension Decimal : ExpressibleByIntegerLiteral {
> > public init(integerLiteral value: Int) {
> > self.init(value)
> > }
> > }
>
> It really shouldn't be, since ExpressibleByFloatLiteral uses a binary
> float initial value and so doesn't guarantee decimal accuracy. I'd
> recommend using init(mantissa:exponent:isNegative:) instead, and
> minimizing any conversions between Double and Decimal.
>
> -Joe
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161128/b37d560a/attachment.html>
More information about the swift-users
mailing list