Arbitrary (and fixed) Precision Decimal Floating-Point Swift Package

BigDecimal

The BigDecimal package provides arbitrary-precision (with an adjustable upper limit for performance) and fixed-precision decimal arithmetic in Swift.
It is available from GitHub here.

Its functionality falls in the following categories:

  • Arithmetic: addition, subtraction, multiplication, division, remainder and exponentiation
  • Constants: pi, zero, one, ten
  • Functions: exp, log, log10, log2, pow, sqrt, root, factorial, gamma, trig + inverse, hyperbolic + inverse
  • Rounding and scaling according to one of the rounding modes:
    • awayFromZero
    • down
    • towardZero
    • toNearestOrEven
    • toNearestOrAwayFromZero
    • up
  • Comparison: the six standard operators ==, !=, <, <=, >, and >=
  • Conversion: to String, to Double, to Decimal (the Swift Foundation type), to Decimal32 / Decimal64 / Decimal128
  • Support for Decimal32, Decimal64 and Decimal128 values stored as UInt32, UInt64 and UInt128 values respectively, using Densely Packed Decimal (DPD) encoding or Binary Integer Decimal (BID) encoding
  • Support for Decimal32, Decimal64 and Decimal128 mathematical operations
  • Supports the IEEE 754 concepts of Infinity and NaN (Not a Number) with the latter having a signaling option.

For more details please see Big Decimal

7 Likes

Nice!

…I think the doc-comments for factorial and gamma preconditions are missing the word “not”.

(Also, for the initial estimate of sqrt when x is not convertible to a finite Double, if x itself is finite it might be better to halve the exponent.)

Quite correct. I was translating the code from an exception under those conditions to an inverted precondition and forgot to invert this. Thanks.

Great!