Code Monkey home page Code Monkey logo

swiftbiginteger's Introduction

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

swiftbiginteger's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

carabina

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.