Is there an equivalent to Java's BigInteger class in Swift? I am tying to do large calculations in Swift with positive integers larger than UInt64's maximum number. What is the best way to handle these numbers?
相关问题
- “Zero out” sensitive String data in Swift
- SwiftUI: UIImage (QRCode) does not load after call
- Get the NSRange for the visible text after scroll
- UIPanGestureRecognizer is not working in iOS 13
- What does a Firebase observer actually do?
相关文章
- Using if let syntax in switch statement
- Enum with associated value conforming to CaseItera
- Swift - hide pickerView after value selected
- Is there a Github markdown language identifier for
- How can I vertically align my status bar item text
- Why do I have to cast enums to int in C#?
- Adding TapGestureRecognizer to UILabel in Swift
- Attempt to present UIAlertController on View Contr
You can use the
NSDecimalNumber
class from Cocoa. It is not infinite precision, but it can represent 38 decimal digits of precision, which may be enough for what you need.I wrote library that allows you to work with big integers in Swift. In similar manner like Java's BigInteger. There are also operator overloads to make work more convenient. Example:
https://github.com/kirsteins/BigInteger
I found a prototype of BigInt in the official Swift repository: https://github.com/apple/swift/blob/master/test/Prototypes/BigInt.swift
You can probably just copy it into your project and use it. Maybe some day it will be added to the standard library.
I have written a big integer and big double implementation for swift, that doesn't require any additional library. Just copy it into your project. It supports whole Numbers (BInt) and Fractions (BDouble) with most of the common math operators like addition, subtraction, multiplication, exponentiation, modulus and division. Some optimized math functions like factorial or gcd are also implemented.
Here are some code examples:
You can use it freely without giving me credit, and please contribute if you want.
You can find it here: https://github.com/mkrd/Swift-Big-Integer
Here it is.
https://github.com/dankogai/swift-pons
Actually BigInt is just a part of it. In addition to BigInt you get:
But the best of all, it is protocol-oriented so you can extend whole integer like:
Dan the Number Generator
I'm also 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 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:
which results in:
You can find the library at: https://github.com/githotto/osxgmp