Latest 1.0.1
Homepage https://github.com/descorp/SwiftBigInteger
License MIT
Platforms ios 8.0, osx 10.9, requires ARC
Authors

swift icon

BigIntegerSwift

Standart
Build Status
Swift 4
Version
License
Platform
codecov

Description

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

Requirements

Project have no dependencies.

Installation

BigIntegerSwift is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'BigIntSwift'

Initialisation

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

Basic values

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

Comparison

        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

Addition

        let a = BigInteger(42)
        let b = BigInteger(-42)        

        a + b           //  0
        b + 42          //  0
        b + UInt(42)    //  0
        a += b          //  a = 0

Substraction

        let a = BigInteger(42)
        let b = BigInteger(-42)        

        a - b           //  84
        b - 42          // -84
        b - UInt(42)    // -84
        a -= b          //  a = 84       

Multiplication

        let a = BigInteger(42)
        let b = BigInteger(-42)        

        a * b           // -1764
        b * (-1)        //  42
        b * UInt(2)     //  84
        a *= b          //  a = -1764

Division

        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

Reminder

        let a = BigInteger(145)
        let b = BigInteger(-12)        

        a % -b          //  1
        b % (-1)        //  0
        b % UInt(2)     //  6
        a %= b          //  a = -1

Power

        let a = BigInteger(-12)
        let b = BigInteger(2)        

        a ^ b           //  144
        a ^ 3           //  -1398
        a ^ -3          //  -1398 (sign ignored)

Example

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)

Author

Vladimir Abramichev, [email protected]

License and References

BigIntegerSwift is available under the MIT license. See the LICENSE file for more info.

Swift Image taken from Icon pack by Icons8

Todo

  • Add pod usage examples
  • Optimise storing of values

Latest podspec

{
    "name": "BigIntSwift",
    "version": "1.0.1",
    "summary": "Implementation of Big Integer for Swift",
    "description": "Library to perform arithmetical operations on integer values of enormous size.nPod provides basic arithmetical operation with Big Integer values like addition, multiplication and substraction.",
    "homepage": "https://github.com/descorp/SwiftBigInteger",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Vladimir Abramichev": "[email protected]"
    },
    "source": {
        "git": "https://github.com/descorp/SwiftBigInteger.git",
        "tag": "1.0.1"
    },
    "platforms": {
        "ios": "8.0",
        "osx": "10.9"
    },
    "requires_arc": true,
    "pod_target_xcconfig": {
        "SWIFT_VERSION": "3.0"
    },
    "source_files": "Source/**/*",
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This