Arbitrary-precision arithmetic for Swift, in Swift
import BigNum
BigRat.sqrt(2, precision:128) // 240615969168004511545033772477625056927/170141183460469231731687303715884105728
BigFloat.exp(1, precision:128) // 2.718281828459045235360287471352662497759
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).
BigFloat.sqrt(2) // 1.41421356237309504876
BigFloat.precision = 128
BigFloat.sqrt(2) // 1.414213562373095048801688724209698078569
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)
$ git clone https://github.com/dankogai/swift-bignum.git
$ cd swift-bignum # the following assumes your $PWD is here
$ swift build
Simply
$ scripts/run-repl.sh
or
$ swift run --repl
and in your repl,
% swift run --repl
Fetching https://github.com/attaswift/BigInt from cache
Fetching https://github.com/apple/swift-numerics from cache
Fetched https://github.com/attaswift/BigInt (0.51s)
Fetched https://github.com/apple/swift-numerics (0.51s)
Computing version for https://github.com/apple/swift-numerics
Computed https://github.com/apple/swift-numerics at 1.1.0 (0.03s)
Computing version for https://github.com/attaswift/BigInt
Computed https://github.com/attaswift/BigInt at 5.7.0 (0.03s)
Creating working copy for https://github.com/apple/swift-numerics
Working copy of https://github.com/apple/swift-numerics resolved at 1.1.0
Creating working copy for https://github.com/attaswift/BigInt
Working copy of https://github.com/attaswift/BigInt resolved at 5.7.0
Building for debugging...
[59/59] Linking BigNumRun
Build complete! (12.07s)
Launching Swift REPL with arguments: repl -I/Users/dankogai/github/swift-bignum/.build/x86_64-apple-macosx/debug -L/Users/dankogai/github/swift-bignum/.build/x86_64-apple-macosx/debug -lswift-bignum__REPL -I/Users/dankogai/github/swift-bignum/.build/checkouts/swift-numerics/Sources/_NumericsShims/include
Welcome to Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5).
Type :help for assistance.
1> import BigNum
2> var bf = BigFloat.sqrt(2, precision:128)
bf: BigNum.BigFloat = {
scale = -127
mantissa = {
magnitude = {
kind = array
storage = 2 values {
[0] = 6448461645324402335
[1] = 13043817825332782212
}
}
sign = plus
}
}
3> print(bf)
1.414213562373095048801688724209698078569
4>
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
.
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!
Swift 6 and 5, OS X or Linux to build.
- Depends on attaswift/BigInt for internal representation of
BigFloat
andBigRat
. - 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 theElementaryFunctions
.