As it stands, there is, as far as I can tell, no modulo or remainder operator for the Decimal type. This seems incongruous with expectations from using floats, float80, double, etc.
It's fairly easy to add with something like:
extension Decimal {
func mod(_ b: Decimal) -> Decimal {
var d = self/(b)
var f : Decimal = 0
NSDecimalRound(&f, &d, 0, .down)
return self-(b*(f))
}
}
(that was just a rough little thing I did for a quick Playground)
but it seems like a weird omission. I would've expected something akin to Double's .remainder to exist. Is there any reason for not having such a method for Decimal?
Also, correct me if I'm wrong here, but Decimal is not arbitrary precision, right? Been looking around online and couldn't find documentation that stated it clearly, but a quick look at the CoreLibs source and it certainly looked like it was fixed precision; This should perhaps have a separate thread, but are we not missing an arbitrary precision data type like BigInt and BigDecimal in the core libraries?
Cheers
xwu
(Xiaodi Wu)
2
They’re straightforward to implement, but the API of Foundation is owned by Apple and adding any methods requires going through a review process. I’ve implemented remainder and truncatingRemainder but they can’t be shipped without that review, and I don’t have time to sort that out.
Correct, this type does not have arbitrary precision. It is also not an IEEE-compliant decimal floating-point type, as it can’t represent infinity. Swift Numerics will eventually add a BigInt type; maybe also a BigDecimal type too.
I see. And CoreFoundation is not really meant to deviate from Foundation I'm guessing. - I've read all the posts and such I could find on contributing to Swift and CoreLibs but I'm still very new here, is there any way I can help with the review process and perhaps assist in getting it into Foundation or is the review process you mention strictly between an Apple maintainer and a PR or?
Having BigInt and perhaps BigDecimal in Swift Numerics would be great. Writing a bit of Go these days, and the "big" package for that is pretty nice. Found a nice GitHub project for BigInteger in Swift, but a neat solution basically accessible everywhere without pulling in an outside dependency would be great.
Been doing some implementation of some crypto for uni; Of course I wouldn't roll my own crypto in practice, but for university purposes I need to, and even if it's not the language we're handing in our assignments in, I like to do quick testing of ideas with Swift because it's so fast to work with and I'm very familiar with it, but for large RSA exponentiations I found myself longing for arbitrary size types
In any case thank you very much for your great reply - I appreciate it a lot :)