From 2f2c003e03ee45b103d8f0afd83ff9641acf84b6 Mon Sep 17 00:00:00 2001 From: Eric Lagergren Date: Thu, 12 Sep 2019 16:42:58 +0200 Subject: [PATCH 1/2] math: update formatting --- go.mod | 2 ++ math/const.go | 2 -- math/pi.go | 74 ++++++++++++++++++++++++--------------------------- 3 files changed, 37 insertions(+), 41 deletions(-) diff --git a/go.mod b/go.mod index f8e930f..7b86146 100644 --- a/go.mod +++ b/go.mod @@ -8,3 +8,5 @@ require ( github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24 gopkg.in/inf.v0 v0.9.1 ) + +go 1.13 diff --git a/math/const.go b/math/const.go index c152d86..9d3f684 100644 --- a/math/const.go +++ b/math/const.go @@ -77,8 +77,6 @@ func pi(z *decimal.Big, ctx decimal.Context) *decimal.Big { if ctx.Precision <= constPrec { return ctx.Set(z, _Pi) } - - //else we return a new pi const return piChudnovskyBrothers(z, ctx) } diff --git a/math/pi.go b/math/pi.go index 835c9f3..e82c260 100644 --- a/math/pi.go +++ b/math/pi.go @@ -1,8 +1,9 @@ package math import ( - "github.com/ericlagergren/decimal" "math" + + "github.com/ericlagergren/decimal" ) var ( @@ -13,32 +14,6 @@ var ( _10939058860032000 = decimal.New(10939058860032000, 0) ) -/* -piChudnovskyBrothers() return PI using binary splitting on the series definition of -Chudnovsky Brothers - - 426880*sqrt(10005) - Pi = -------------------------------- - 13591409*aSum + 545140134*bSum - - where - 24(6n-5)(2n-1)(6n-1) - a_n = ---------------------- - (640320^3)*n^3 - - and - aSum = sum_(n=0)^n a_n - bSum = sum_(n=0)^n a_n*n - - - -a(n) = 1, -b(n) = 1, -p(0) = 1, p(n) = (13591409 + 545140134n)(5 − 6n)(2n − 1)(6n − 1), -q(0) = 1, q(n) = (n^3)(640320^3)/24 for n > 0 - -*/ - func getPiA(ctx decimal.Context) func(n uint64) *decimal.Big { return func(n uint64) *decimal.Big { //returns A + Bn @@ -97,13 +72,13 @@ func getPiQ(ctx decimal.Context) func(n uint64) *decimal.Big { } var nn, tmp decimal.Big - //when n is super small we can be super fast + // When n is super small we can be super fast. if n < 12 { tmp.SetUint64(n * n * n * 10939058860032000) return &tmp } - // and until bit larger we can still speed up a portion + // And until bit larger we can still speed up a portion. if n < 2642246 { tmp.SetUint64(n * n * n) } else { @@ -117,21 +92,42 @@ func getPiQ(ctx decimal.Context) func(n uint64) *decimal.Big { } } +// piChudnovskyBrothers calculates PI using binary splitting on the series +// definition of the Chudnovsky Brothers' algorithm for computing pi. +// +// 426880*sqrt(10005) +// Pi = -------------------------------- +// 13591409*aSum + 545140134*bSum +// +// where +// 24(6n-5)(2n-1)(6n-1) +// a_n = ---------------------- +// (640320^3)*n^3 +// +// and +// aSum = sum_(n=0)^n a_n +// bSum = sum_(n=0)^n a_n*n +// +// a(n) = 1, +// b(n) = 1, +// p(0) = 1, p(n) = (13591409 + 545140134n)(5 − 6n)(2n − 1)(6n − 1), +// q(0) = 1, q(n) = (n^3)(640320^3)/24 for n > 0 +// func piChudnovskyBrothers(z *decimal.Big, ctx decimal.Context) *decimal.Big { - //since this algorithm's rate of convergence is static calculating the number of - // iteration required will always be faster than the dynamic method of binarysplit + // Since this algorithm's rate of convergence is static, calculating the + // number of iteration required will always be faster than the dynamic + // method of binarysplit. + + calcPrec := ctx.Precision + 16 + niters := uint64(math.Ceil(float64(calcPrec)/14)) + 1 + ctx2 := decimal.Context{Precision: calcPrec} + var value decimal.Big - extraPrecision := 16 - calculatingPrecision := ctx.Precision + extraPrecision - iterationNeeded := uint64(math.Ceil(float64(calculatingPrecision/14.0))) + 1 - ctx2 := decimal.Context{Precision: calculatingPrecision} + BinarySplit(&value, ctx2, 0, niters, getPiA(ctx2), getPiP(ctx2), getPiB(), getPiQ(ctx2)) + s := Sqrt(decimal.WithPrecision(calcPrec), _10005) var tmp decimal.Big - - BinarySplit(&value, ctx2, 0, iterationNeeded, getPiA(ctx2), getPiP(ctx2), getPiB(), getPiQ(ctx2)) - s := Sqrt(decimal.WithPrecision(calculatingPrecision), _10005) ctx2.Mul(&tmp, _426880, s) ctx2.Quo(z, &tmp, &value) - return z.Round(ctx.Precision) } From 2c3e3e1ef942ac4ca58d18a6bc27a0c784d621e8 Mon Sep 17 00:00:00 2001 From: Eric Lagergren Date: Thu, 12 Sep 2019 16:48:44 +0200 Subject: [PATCH 2/2] sql: mark package(s) as deprecated Updates: #128 --- sql/sql.go | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 sql/sql.go diff --git a/sql/sql.go b/sql/sql.go new file mode 100644 index 0000000..6e269c4 --- /dev/null +++ b/sql/sql.go @@ -0,0 +1,5 @@ +// Package sql provides the ability to use Big decimals with SQL databases. +// +// Deprecated: With the addition of the "decimal" interface (see: https://golang.org/issue/30870), +// none of these subpackages are needed. They may be removed in a future version. +package sql