- Notifications
You must be signed in to change notification settings - Fork59
A lightweight, Arbitrary Precision Arithmetic Library for Swift!
License
mkrd/Swift-BigInt
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Swift-BigInt is a lightweight, and easy-to-use, arbitrary precision arithmetric library for Swift 5.
It supports whole Numbers (BInt) and Fractions (BDouble) with most of the common math operators. Optimized mathematical functions like factorial or gcd are also implemented and are accessible through BIntMath. For more details, please continue reading.
Some benchmarks are located in Benchmarks.swift, note that these are more than 10 times faster in the release mode, compared to the debug mode of Xcode.
We want to hear your opinion about Swift BigInt! If you have a few minutes, please help us with answering a few questions about how you use this project, and tell us your opinion about it. The survey is completely anonymous, and will be used to evaluate which features will be prioritized in the future.
One of the main goals of this library is to be lightweight and independent.
Simply drag and dropSwift-Big-Number-Core.swift
from thesources
folder into your project!
Yes, it's that easy :)
You can use theSwift Package Manager and specify the package dependency in yourPackage.swift
file by adding this:
.package(url: "https://github.com/mkrd/Swift-BigInt.git", from: "2.0.0")
import BigNumber
Put the following in your Podfile:
pod 'Swift-BigInt', '~> 2.0'
import BigNumber
It is recommended to use Xcode 9+ and Swift 4+. Issues have been reported with older versions, so you might want to use an older version of this library if you can't update.
Here is a small example, to showcase some functionalities of this library. If you want to learn more, please continue reading the Usage section below.
leta=BInt(12)letb=BInt("-10000000000000000000000000000000000000000000000000000000000000000")!print(b)>>>-10000000000000000000000000000000000000000000000000000000000000000print(-a* b)>>>120000000000000000000000000000000000000000000000000000000000000000print(BInt(200).factorial())>>>788657867364790503552363213932185062295135977687173263294742533244359449963403342920304284011984623904177212138919638830257642790242637105061926624952829931113462857270763317237396988943922445621451664240254033291864131227428294853277524242407573903240321257405579568660226031904170324062351700858796178922222789623703897374720000000000000000000000000000000000000000000000000
You initialize BInt withInt
,UInt
, andString
. If you use aString
, the initializedBInt
will be an optional type, which will be empty if theString
does not contain an valid number.
BInt(Int)BInt(UInt)BInt(String)?BInt(String, radix: Int)?
leta=BInt(12)print(a)>>>12letb=BInt("-234324176583764598326758236587632649181349105368042856028465298620328782652623")print(b!)>>>-234324176583764598326758236587632649181349105368042856028465298620328782652623letinvalid=BInt("I'm not a number")iflet c= invalid{print(c)}else{print("Not a valid number!")}>>> Not a valid number!letd=BInt("fff", radix:16)print(d)>>>4095
letbig=BInt("-143141341")!big.description // Returns "-143141341"=>print(big) // prints "-143141341"big.toInt() // returns -143141341 (only works when Int.min <= big <= Int.max)big.isPositive() // Returns falsebig.isNegative() // Returns truebig.isZero() // Returns falsebig.negate() // Returns noting, but negates the BInt (mutating func)big.rawData() // Returns internal structure
// Operating on Int and BInt result in a typecast to BInt// AdditionBIntOrInt+ BIntOrInt // Returns BIntBIntOrInt+= BIntOrInt//SubtractionBIntOrInt- BIntOrInt // Returns BIntBIntOrInt-= BIntOrInt// MultiplicationBIntOrInt* BIntOrInt // Returns BIntBIntOrInt*= BIntOrInt// ExponentiationBInt** Int // Returns BInt to the power of Int// ModuloBIntOrInt% BIntOrInt // Returns BIntBInt%= BInt// DivisionBInt/ BInt // Returns BIntBInt/= BInt// ComparingBInt== BIntBInt!= BIntBInt< BIntBInt<= BIntBInt> BIntBInt>= BInt
fact(Int) // Returns factorial as BIntgcd(BInt, BInt) // Returns greatest common divisor as BIntlcm(BInt, BInt) // Returns lowest common multiple as BIntpermutations(BInt, BInt) // Returns BIntcombinations(BInt, BInt) // Returns BInt
BDouble(Int)BDouble(Double)BDouble(String)?BDouble(Int, over: Int)BDouble(String, over: String)?BDouble(String, radix: Int)?
letinteger=BDouble(221)letdouble=BDouble(1.192)letfraction=BDouble(3, over:4)letstringFraction=BDouble("1" over:"3421342675925672365438867862653658268376582356831563158967")!
letbigD=BDouble(-12.32)bigD.description // Returns "-308/25"=>print(bigD) // prints "-308/25"bigD.minimize() // Divides numerator and denominator by their gcd for storage and operation efficiency, usually not necessary, because of automatic minimizationbigD.rawData() // Returns internal structure
// Needs more operators, interoperability with BInt// AdditionBDouble+ BDouble // Returns BDouble// SubtractionBDouble- BDouble // Returns BDouble// MultiplicationBDouble* BDouble // Returns BDouble// DivisionBDouble/ BDouble // Returns BDouble// ComparingBDouble< BDouble/*Important:a < b <==> b > aa <= b <==> b >= abut:a < b <==> !(a >= b)a <= b <==> !(a > b)*/// More will follow
BInt about twice as fast as mini-gmp, as of now (not counting the normal gmp, because it needs to be installed and is not portable). For example, BInt can add numbers about 2 times faster than GMP (272ms vs 530ms for fib(100,000)), and multiplication is more than twice as fast. When given the task of calculating and printing factorials successively, BInt performs significantly better than GMP. In addition, GMP is significantly harder to use, while BInt offers an intuitive interface.
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
About
A lightweight, Arbitrary Precision Arithmetic Library for Swift!