Decimal to Double?


(Rick M) #1

How do I get a Double from a Decimal?

TIA,

···

--
Rick Mann
rmann@latencyzero.com


(Alex Blewitt) #2

You can wrap it with an NSDecimalNumber, and then cast it to a Double from there:

Double(NSDecimalNumber(decimal:Decimal(1.0)))

Alex

···

On 28 Nov 2016, at 10:13, Rick Mann via swift-users <swift-users@swift.org> wrote:

How do I get a Double from a Decimal?

TIA,

--
Rick Mann
rmann@latencyzero.com

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Rick M) #3

Ugh. Thanks!

···

On Nov 28, 2016, at 02:40 , Alex Blewitt <alblue@apple.com> wrote:

You can wrap it with an NSDecimalNumber, and then cast it to a Double from there:

Double(NSDecimalNumber(decimal:Decimal(1.0)))

Alex

On 28 Nov 2016, at 10:13, Rick Mann via swift-users <swift-users@swift.org> wrote:

How do I get a Double from a Decimal?

TIA,

--
Rick Mann
rmann@latencyzero.com

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--
Rick Mann
rmann@latencyzero.com


(Philippe Hausler) #4

This might be a bit nicer since that is relying on NSNumber bridges. You can bridge to NSDecimalNumber directly like this:

(Decimal(1.0) as NSDecimalNumber).doubleValue

(but perhaps we should consider adding initializers that follow the same pattern as Double that don’t have to bridge to solve the problem)

···

On Nov 28, 2016, at 2:40 AM, Alex Blewitt via swift-users <swift-users@swift.org> wrote:

You can wrap it with an NSDecimalNumber, and then cast it to a Double from there:

Double(NSDecimalNumber(decimal:Decimal(1.0)))

Alex

On 28 Nov 2016, at 10:13, Rick Mann via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

How do I get a Double from a Decimal?

TIA,

--
Rick Mann
rmann@latencyzero.com <mailto:rmann@latencyzero.com>

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(David Sweeris) #5

Does NSDecimalNumber conform to the floating point literal whosywhatsit protocol? If not, wouldn't both of those examples just covert a Double to a decimal and back again? Would there be a purpose to that? Like maybe it'd get rounded differently or something?

Once you have an NSDecimalNumber variable, though, I don't know of any better way than that '.doubleValue' property you and Alex mentioned.

- Dave Sweeris

···

On Nov 28, 2016, at 09:48, Philippe Hausler via swift-users <swift-users@swift.org> wrote:

This might be a bit nicer since that is relying on NSNumber bridges. You can bridge to NSDecimalNumber directly like this:

(Decimal(1.0) as NSDecimalNumber).doubleValue

(but perhaps we should consider adding initializers that follow the same pattern as Double that don’t have to bridge to solve the problem)

On Nov 28, 2016, at 2:40 AM, Alex Blewitt via swift-users <swift-users@swift.org> wrote:

You can wrap it with an NSDecimalNumber, and then cast it to a Double from there:

Double(NSDecimalNumber(decimal:Decimal(1.0)))

Alex

On 28 Nov 2016, at 10:13, Rick Mann via swift-users <swift-users@swift.org> wrote:

How do I get a Double from a Decimal?

TIA,

--
Rick Mann
rmann@latencyzero.com

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Alex Blewitt) #6

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 would be relatively simple for the NSDecimal to add an extension initialiser to Double to allow construction directly from an NSDecimal, but it would have to be done for both the Linux and Darwin implementations.

Alex

···

On 28 Nov 2016, at 17:15, David Sweeris <davesweeris@mac.com> wrote:

On Nov 28, 2016, at 09:48, Philippe Hausler via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

This might be a bit nicer since that is relying on NSNumber bridges. You can bridge to NSDecimalNumber directly like this:

(Decimal(1.0) as NSDecimalNumber).doubleValue

(but perhaps we should consider adding initializers that follow the same pattern as Double that don’t have to bridge to solve the problem)

On Nov 28, 2016, at 2:40 AM, Alex Blewitt via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

You can wrap it with an NSDecimalNumber, and then cast it to a Double from there:

Double(NSDecimalNumber(decimal:Decimal(1.0)))

Alex

On 28 Nov 2016, at 10:13, Rick Mann via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

How do I get a Double from a Decimal?

TIA,

--
Rick Mann
rmann@latencyzero.com <mailto:rmann@latencyzero.com>

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

Does NSDecimalNumber conform to the floating point literal whosywhatsit protocol? If not, wouldn't both of those examples just covert a Double to a decimal and back again? Would there be a purpose to that? Like maybe it'd get rounded differently or something?

Once you have an NSDecimalNumber variable, though, I don't know of any better way than that '.doubleValue' property you and Alex mentioned.

- Dave Sweeris


(Joe Groff) #7

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

···

On Nov 28, 2016, at 9:22 AM, Alex Blewitt via swift-users <swift-users@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)
    }
}


(Joe Groff) #8

That would certainly be more robust than using float literals, though probably slower than constructing from elements using the init(mantissa:exponent:isNegative:) initializer.

-Joe

···

On Nov 28, 2016, at 11:29 AM, David Sweeris <davesweeris@mac.com> wrote:

Sent from my iPhone

On Nov 28, 2016, at 13:21, Joe Groff <jgroff@apple.com> wrote:

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.

That's what I was worried about... What's the "correct" way to express a literal as a Decimal, then? Use a string literal, `"2.1"`, instead of a float literal, `2.1`?


(Joe Groff) #9

Fixing ExpressibleByFloatLiteral has come up several times, but nobody's formally written up the proposal AFAIK.

-Joe

···

On Nov 28, 2016, at 11:42 AM, Nevin Brackett-Rozinsky <nevin.brackettrozinsky@gmail.com> wrote:

On Mon, Nov 28, 2016 at 2:21 PM, Joe Groff via swift-users <swift-users@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?


(David Sweeris) #10

That's what I was worried about... What's the "correct" way to express a literal as a Decimal, then? Use a string literal, `"2.1"`, instead of a float literal, `2.1`?

- Dave Sweeris

···

Sent from my iPhone

On Nov 28, 2016, at 13:21, Joe Groff <jgroff@apple.com> wrote:

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.


(David Sweeris) #11

I'm in the middle of moving, or I'd do it.

- Dave Sweeris

···

On Nov 28, 2016, at 13:43, Joe Groff via swift-users <swift-users@swift.org> wrote:

On Nov 28, 2016, at 11:42 AM, Nevin Brackett-Rozinsky <nevin.brackettrozinsky@gmail.com> wrote:

On Mon, Nov 28, 2016 at 2:21 PM, Joe Groff via swift-users <swift-users@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?

Fixing ExpressibleByFloatLiteral has come up several times, but nobody's formally written up the proposal AFAIK.


(Stephen Canon) #12

Yes. Adding decimal literals would be a nice improvement at some future point, strings are the correct solution until then.

···

On Nov 28, 2016, at 2:29 PM, David Sweeris via swift-users <swift-users@swift.org> wrote:

Sent from my iPhone

On Nov 28, 2016, at 13:21, Joe Groff <jgroff@apple.com> wrote:

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.

That's what I was worried about... What's the "correct" way to express a literal as a Decimal, then? Use a string literal, `"2.1"`, instead of a float literal, `2.1`?


#13

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@swift.org> wrote:

On Mon, Nov 28, 2016 at 2:21 PM, Joe Groff via swift-users < swift-users@swift.org> wrote:

> On Nov 28, 2016, at 9:22 AM, Alex Blewitt via swift-users < > swift-users@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-users