Swiftpack.co - dankogai/swift-bignum as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
dankogai/swift-bignum
Arbitrary-precision arithmetic for Swift, in Swift
.package(url: "https://github.com/dankogai/swift-bignum.git", from: "5.2.3")

Swift 5 MIT LiCENSE build status

swift-bignum

Arbitrary-precision arithmetic for Swift, in Swift

Synopsis

import BigNum
BigRat.sqrt(2, precision:128)  // 240615969168004511545033772477625056927/170141183460469231731687303715884105728
BigFloat.exp(1, precision:128) // 2.718281828459045235360287471352662497759

Description

This module offers two flavors of Arbitrary-precision types that conforms to FloatingPoint.

  • BigRat - Arbitrary-precision rational number.
  • BigFloat - Arbitrary-precision floating point.

In addition to all arithmetic operations that FloatingPoint supports. Most of the functions in <math.h> are offered as static functions. As you see in the synopsis above, all arithmetic functions and operators that are lossy can take precision:Int as an optional argument. When omitted the value of BigRat.precision or BigFloat.precision is used (default:64)

BigInt, an arbitrary-precision interger type is internally used and re-exported so you don't have to import BigInt just for that. BigInt is also extended with .over() method so instead of constructing BigRat directly, you can:

BigInt(3260954456333195553).over(BigInt(2305843009213693952)) // == BigRat.sqrt(2)

Usage

build

$ git clone https://github.com/dankogai/swift-bignum.git
$ cd swift-complex # the following assumes your $PWD is here
$ swift build

REPL

Simply

$ scripts/run-repl.sh

or

$ swift run --repl

and in your repl,

Welcome to Apple Swift version 4.2 (swiftlang-1000.11.37.1 clang-1000.11.45.1). Type :help for assistance.
  1> import BigNum 
  2> BigRat.sqrt(2, precision:128)
$R0: BigNum.BigRat = {
  num = {
    magnitude = {
      kind = array
      storage = 2 values {
        [0] = 6448461645324402335
        [1] = 13043817825332782212
      }
    }
    sign = plus
  }
  den = {
    magnitude = {
      kind = array
      storage = 2 values {
        [0] = 0
        [1] = 9223372036854775808
      }
    }
    sign = plus
  }
}

Xcode

Xcode project is deliberately excluded from the repository because it should be generated via swift package generate-xcodeproj . For convenience, you can

$ scripts/xcode-prep

And the Workspace opens up for you with Playground on top. The playground is written as a manual.

From Your SwiftPM-Managed Projects

Add the following to the dependencies section:

.package(
  url: "https://github.com/dankogai/swift-bignum.git", .branch("main")
)

and the following to the .target argument:

.target(
  name: "YourSwiftyPackage",
  dependencies: ["BigNum"])

Now all you have to do is:

import BigNum

in your code. Enjoy!

Prerequisite

Swift 5 or better, OS X or Linux to build.

Depends on attaswift/BigInt for internal representation of BigFloat and BigRat.

Depends on apple/swift-numerics since version 5.1 for the ElementaryFunctions protocol. Prior versions depended on dankogai/swift-floatingpoint for the FloatingPointMath protocols but it is replaced by the ElementaryFunctions.

GitHub

link
Stars: 11
Last commit: 3 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Dependencies

Related Packages

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics