Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A lightweight, Arbitrary Precision Arithmetic Library for Swift!

License

NotificationsYou must be signed in to change notification settings

mkrd/Swift-BigInt

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.

Survey Time!

Survey Link

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.

Installation

Drag and Drop

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 :)

Swift Package Manager

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

CocoaPods

Put the following in your Podfile:

pod 'Swift-BigInt', '~> 2.0'
import BigNumber

Compatibility

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.

Getting Started

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

Usage

BInt

Initialization

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)?

Examples

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

BInt offers these struct methods

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

The following Operators work with BInt

// 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

Implemented BInt math functions

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

BDouble allows these constructors

BDouble(Int)BDouble(Double)BDouble(String)?BDouble(Int, over: Int)BDouble(String, over: String)?BDouble(String, radix: Int)?

Examples

letinteger=BDouble(221)letdouble=BDouble(1.192)letfraction=BDouble(3, over:4)letstringFraction=BDouble("1" over:"3421342675925672365438867862653658268376582356831563158967")!

BDouble offers these struct methods

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

The following Operators work with BDouble

// 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

About performance

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.

Contributing

  1. Fork it!
  2. Create your feature branch:git checkout -b my-new-feature
  3. Commit your changes:git commit -am 'Add some feature'
  4. Push to the branch:git push origin my-new-feature
  5. Submit a pull request :D

[8]ページ先頭

©2009-2025 Movatter.jp