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:
$ git clone https://github.com/dankogai/swift-bignum.git
$ cd swift-bignum # 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
}
}
From your Xcode Projects.
Simply add the package from Package Dependencies tab of the Project.
Enter https://github.com/dankogai/swift-bignum in the search field and click [Add Package]
Now you are able to import BigNum.
Now all you have to do is build and enjoy!
If you enconter errors like Missing required module '_NumericShims', try cleaning up your ~/Library/Developer/Xcode/DerivedData.
swift-bignum
Arbitrary-precision arithmetic for Swift, in Swift
Synopsis
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 takeprecision:Int
as an optional argument. When omitted the value ofBigRat.precision
orBigFloat.precision
is used (default:64).BigInt
, an arbitrary-precision interger type is internally used and re-exported so you don’t have toimport BigInt
just for that.BigInt
is also extended with.over()
method so instead of constructingBigRat
directly, you can:Usage
Build
REPL
Simply
or
and in your repl,
From your Xcode Projects.
Simply add the package from Package Dependencies tab of the Project. Enter
https://github.com/dankogai/swift-bignum
in the search field and click[Add Package]
Now you are able to
import BigNum
.Now all you have to do is build and enjoy!
If you enconter errors like
Missing required module '_NumericShims'
, try cleaning up your~/Library/Developer/Xcode/DerivedData
.From Your SwiftPM-Managed Projects
Add the following to the
dependencies
section:and the following to the
.target
argument:Now all you have to do is:
in your code. Enjoy!
Prerequisite
Swift 5 or better, OS X or Linux to build.
BigFloat
andBigRat
.ElementaryFunctions
protocol.FloatingPointMath
protocols but it is replaced by theElementaryFunctions
.