Options for arbitrary precision exact numbers?

I'm writing some financial code that will deal with stock and crypto prices, so I need something more precise than 0.01. I'd like a numeric type that can go to any precision and doesn't round off like Double. Is there something people are using, or some standard options on the menu?

I understand it's a performance hit if it doesn't fit in a CPU register, and that "exact" means I can't do divisions without choosing a precision and getting a remainder. Eg: 10 / 3 might be 0.3 with remainder 0.1, or 0.333 with remainder 0.001, etc.

Here are a few potential options that I know of:

Foundation has Decimal, and one of the GSoC projects this year is Decimal64, but neither is not arbitrary precision.

attaswift/BigInt is arbitrary precision, but it's binary.

1 Like

Thanks! I think I'm more interested in the "exact" part than the "arbitrary precision", so I would use Decimal if I could get it to tell me when it gave a non-exact answer, but it appears you can't do that. For example, this code prints noError and a bunch of 3's. I was thinking it would return lossOfPrecision since it can't represent the answer exactly. Even if it did, I don't see an Int-style form of division to give an exact answer with a remainder. So I'll probably use Ints and maybe that BigInt.

var one = Decimal(1)
var three = Decimal(3)
var res = Decimal(0)
let err = NSDecimalDivide(&res, &one, &three, .plain)
print("err: \(err)")
print("res: \(res)")