swift-numerics icon indicating copy to clipboard operation
swift-numerics copied to clipboard

Decimal floating-point

Open stephentyrone opened this issue 6 years ago • 7 comments
trafficstars

Implement IEEE 754 Decimal128 (and possibly Decimal64 as well, though it's less important). This has a dependency on fixed-width integer types because we want to be able to leverage UInt128 arithmetic for the implementation.

stephentyrone avatar Jun 25 '19 01:06 stephentyrone

We would like to second the request for this feature. For financial applications where values will not exceed the limits of UInt128 and where precision in values is key, having a Decimal128 implementation (or Decimal64) would be very helpful.

paulrs avatar Jun 26 '20 18:06 paulrs

I agree with @paulrs that Decimal128 would be extremely valuable for financial applications.

djehrlich avatar Jun 14 '21 18:06 djehrlich

Especially since Foundation's Decimal causes a fatal error in this simple case:

let d = Decimal(exactly: 0)

and Apple seems unwilling to fix it (I reported it January 17, 2021).

jasonbobier avatar Dec 02 '21 19:12 jasonbobier

@jasonbobier The fatal error was simply that the API was unimplemented; it's been implemented in the open-source swift-corelibs-foundation for some years now and Apple's proprietary implementation will pick up the changes on some timeline of their choosing.

xwu avatar Dec 02 '21 22:12 xwu

@xwu sigh "Some timeline of their choosing" is accurate. I have bugs from 2003 still open with them.

jasonbobier avatar Dec 02 '21 22:12 jasonbobier

Is there any update on this?

KTSnowy avatar May 23 '22 20:05 KTSnowy

I have an implementation of this including Decimal32, Decimal64, and Decimal128. Decimal32 is mostly tested but the other implementations require testing. This is a pure Swift implementation based on the Intel Corp Decimal Floating-Point Math Library v2.2. BTW, this is a IEEE 754 implementation. It supports both BID (Binary Integer Decimal) and DPD (Densely Packed Decimal) encodings. IEEE 754 allows both encodings.

How would I upload this code so others can contribute? See https://github.com/mgriebling/MGDecimal for details.

mgriebling avatar May 26 '22 12:05 mgriebling