Library provides an implementation for Big Integer with basic arithmetics and conveniences. BigInteger
is a threadsafe immutable struct and implements Comparable
, Equatable
, CustomStringConvertible
and Hashable
protocols.
Build with respect of IEEE-754 standard
Project have no dependencies.
BigIntegerSwift is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'BigIntSwift'
From integer primitives
let a = BigInteger(1234567)
a.sign // true
let b = BigInteger(UInt64(12345678901234))
b.sign // true
From string
let a = BigInteger("-123456789012345678901234567890")
a.sign // false
Shortcut for Big Integer with zero magnitude. -Zero is could be handy in case of devision of value on -infinity
BigInteger.zero // 0
-BigInteger.zero // -0
Shortcut for "Non Arifhmetical value". A method or operator returns nan
when the result of an operation is undefined. For example, the result of dividing zero by zero is NaN
BigInteger.nan // nan
Shortcut for Big Integer with infinit magnitude
BigInteger.infinit // inf
-BigInteger.infinit // -inf
let a = BigInteger(42)
let b = BigInteger(-42)
a != b // true
a == -b // true
a > b // true
b <= a // true
a != -42 // true
UInt(42) == -b // true
a > 0 // true
0 <= a // true
let a = BigInteger(42)
let b = BigInteger(-42)
a + b // 0
b + 42 // 0
b + UInt(42) // 0
a += b // a = 0
let a = BigInteger(42)
let b = BigInteger(-42)
a - b // 84
b - 42 // -84
b - UInt(42) // -84
a -= b // a = 84
let a = BigInteger(42)
let b = BigInteger(-42)
a * b // -1764
b * (-1) // 42
b * UInt(2) // 84
a *= b // a = -1764
let a = BigInteger(145)
let b = BigInteger(-12)
a / -b // 12
a / 0 // inf
BigInteger.zero / 0 // nan
b / (-1) // 12
b / UInt(2) // 6
a /= b // a = -12
let a = BigInteger(145)
let b = BigInteger(-12)
a % -b // 1
b % (-1) // 0
b % UInt(2) // 6
a %= b // a = -1
let a = BigInteger(-12)
let b = BigInteger(2)
a ^ b // 144
a ^ 3 // -1398
a ^ -3 // -1398 (sign ignored)
Todo
To run the example project, clone the repo, and run pod install
from the Example directory first.
You can check tests for more examples like awesome Fibonacci 20000 calculation in ~10 sec (4179 decimals)
Vladimir Abramichev, [email protected]
BigIntegerSwift is available under the MIT license. See the LICENSE file for more info.
Swift Image taken from Icon pack by Icons8
- Add pod usage examples
- Optimise storing of values