Is there a number type with bigger capacity than u_long/UInt64 in Swift?

后端 未结 4 522
鱼传尺愫
鱼传尺愫 2021-01-19 06:21

Is there a type with bigger capacity than u_long or UInt64 in Swift?

I have a function that takes very big integers to identify a credit card numbe

4条回答
  •  长情又很酷
    2021-01-19 06:36

    I'm working on a BigNumber library with which you can do some big number calculations. Actually the library is based on the GNU Multiple Precision (GMP) library (see: https://gmplib.org) and I wrote an Objective-C / Swift wrapper. Currently big integer mathematics, including a lot of operator overloading, is possible. A code example goes like:

    var err : NSError?
    var bi1 = BigInt(nr: 12468642135797531)
    var bi2 = BigInt(nr: "12345678901011121314151617181920", error: &err)
    var res = bi1 * bi2
    println("Multiply 2 BigInts: bi1 * bi2 = \(res.toString())")
    

    which results in:

    Multiply 2 BigInts: bi1 * bi2 = 153933852140173822960829726365674325601913839520
    

    You can find the library at: https://github.com/githotto/osxgmp

    I think its fairly easy to do some 'credit-card-number' math with even numbers having far more than 28-digits.

提交回复
热议问题