Code Monkey home page Code Monkey logo

swift-bignum's Introduction

Swift 5 MIT LiCENSE CI via GitHub Actions

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).

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)

Usage

Build

$ 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,

[0/0] Build complete!
Launching Swift REPL with arguments: -I/Users/dankogai/github/swift-bignum/.build/x86_64-apple-macosx/release -L/Users/dankogai/github/swift-bignum/.build/x86_64-apple-macosx/release -lBigNum__REPL -I/Users/dankogai/github/swift-bignum/.build/checkouts/swift-numerics/Sources/_NumericsShims/include
Welcome to Swift version 5.5.2-dev.
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

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:

.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.

swift-bignum's People

Contributors

chinsyo avatar dankogai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

swift-bignum's Issues

maxPrecision support?

From a mail I received

I want to use your BigNum Package to compute fractals with arbitrary Precision! However, I realised that the algorithm gets choked to death by every increasing precision floating point numbers (I use BigFloat). Is there a way to limit the precision of BigFloats. And more importantly to limit the precision of products or sums of big floats (e.g. to 100 digits?)?

I thought about something like:

var temp = BigFloat.init(maxPrecision:100)

Such that temp will get a limit, and thereby the CPU has a chance to perform….

Rounding error

Hello, I found something that seems like a bug in BigNum library to me. I have current version, updated today.

So I have following code, that simulates an operation I have to do. There are printed results in comments. :

let toround = BigFloat(2) / BigFloat(100)
print(toround.description) //0.0199999999999999999996

let pow = BigFloat(10).power(BigInt(2))
var res = toround * pow
print (res.description) //1.999999999999999999959

res = res.rounded(.toNearestOrAwayFromZero)
print (res.description) //1.0

res = res / pow
print (res.description) //0.0099999999999999999998

Problem is the third result, just after .rounded() . 1.999999999999999999959 should have been rounded to 2.0, not 1.0 . Am I doing something wrong, or can you fix the bug, please?

GenericMath algorithms with BigDecimal numbers

First, thanks for the great work you've done on the BigNum library.
I was interested if the generic algorithms used in GenericMath.swift can be used with floating point decimal numbers.
It definitely looks like the precision needs to be changed to digits instead of bits. I assume changing the getEpsilon
would do this?

Power of a negative number

Hi, I found a bug in BigFloat.power() (or BigFloat.pow() ) . It crashes when counting power of a negative value, like this:

        let val = BigFloat(-3)
        let res = val.power(8) //crash
        print(res.description)

I believe that it is due to counting log of a negative value in the implementation of power()

Build issue - Multiplatform Project with Xcode 12 - Release

When using build configuration "Release" for a Multiplatform Xcode 12 project (iOS + MacOS - Apple Silicon + Intel) it appears that Float80 is not supported in the Mac Configuration. See attached Screenshot. Maybe it's related to Apple Silicon...
Screenshot 2020-11-28 at 09 10 42

When building for Debug there is no error message, but Float80 seems to behave wrongly...

SipHash - library not loaded

I am using latest master release of BigNum, via swift package manager. XCode 11.5 (just updated today). Everything works when running app in emulator. But on real device, I get SIGABRT just after the application launches, and following error:

dyld: Library not loaded: @rpath/SipHash.framework/SipHash
Referenced from: /private/var/containers/Bundle/Application/FF881D6F-2AE9-4CAB-9BE8-6709D7E6428E/Calculator2.app/Frameworks/BigNum.framework/BigNum
Reason: image not found

As I understand it, SipHash is required by BigInt, which is the required by BigNum. I was googling whole day, and I came to conclusion that current version of BigInt should have it repaired. But I have only version 3.1.0, because that is what BigNum depends on.

Is it possible to do something with it? If it is possible, can BigNum just allow newer versions of BigInt?

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.