forked from ericlagergren/decimal
/
pi.go
241 lines (215 loc) · 4.89 KB
/
pi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package benchmarks
import (
"math"
"github.com/apmckinlay/gsuneido/util/dnum"
"github.com/cockroachdb/apd"
"github.com/ericlagergren/decimal"
ssdec "github.com/shopspring/decimal"
"gopkg.in/inf.v0"
)
func adjustPrecision(prec int) int {
return int(math.Ceil(float64(prec) * 1.1))
}
var (
infEight = inf.NewDec(8, 0)
infThirtyTwo = inf.NewDec(32, 0)
)
// PiInf calculates π to the desired precision using gopkg.in/inf.v0.
func PiInf(prec int) *inf.Dec {
var (
lasts = inf.NewDec(0, 0)
t = inf.NewDec(3, 0)
s = inf.NewDec(3, 0)
n = inf.NewDec(1, 0)
na = inf.NewDec(0, 0)
d = inf.NewDec(0, 0)
da = inf.NewDec(24, 0)
work = adjustPrecision(prec)
)
for s.Cmp(lasts) != 0 {
lasts.Set(s)
n.Add(n, na)
na.Add(na, infEight)
d.Add(d, da)
da.Add(da, infThirtyTwo)
t.Mul(t, n)
t.QuoRound(t, d, inf.Scale(work), inf.RoundHalfUp)
s.Add(s, t)
}
// -1 because inf's precision == digits after radix
return s.Round(s, inf.Scale(prec-1), inf.RoundHalfUp)
}
var (
ssdecEight = ssdec.New(8, 0)
ssdecThirtyTwo = ssdec.New(32, 0)
)
// PiShopSpring calculates π to the desired precision using
// github.com/shopspring/decimal.
func PiShopSpring(prec int32) ssdec.Decimal {
var (
lasts = ssdec.New(0, 0)
t = ssdec.New(3, 0)
s = ssdec.New(3, 0)
n = ssdec.New(1, 0)
na = ssdec.New(0, 0)
d = ssdec.New(0, 0)
da = ssdec.New(24, 0)
work = int32(adjustPrecision(int(prec)))
)
for s.Cmp(lasts) != 0 {
lasts = s
n = n.Add(na)
na = na.Add(ssdecEight)
d = d.Add(da)
da = da.Add(ssdecThirtyTwo)
t = t.Mul(n)
t = t.DivRound(d, work)
s = s.Add(t)
}
// -1 because shopSpring's prec == digits after radix
return s.Round(prec - 1)
}
var (
dnumEight = dnum.New(+1, 8, 0)
dnumThirtyTwo = dnum.New(+1, 32, 0)
)
// PiDnum calculates π to its maximum precision of 16 digits using
// github.com/apmckinlay/gsuneido/util/dnum.
func PiDnum() dnum.Dnum {
var (
lasts = dnum.New(+1, 0, 0)
t = dnum.New(+1, 3, 0)
s = dnum.New(+1, 3, 0)
n = dnum.New(+1, 1, 0)
na = dnum.New(+1, 0, 0)
d = dnum.New(+1, 0, 0)
da = dnum.New(+1, 24, 0)
)
for dnum.Compare(s, lasts) != 0 {
lasts = s
n = dnum.Add(n, na)
na = dnum.Add(na, dnumEight)
d = dnum.Add(d, da)
da = dnum.Add(da, dnumThirtyTwo)
t = dnum.Mul(t, n)
t = dnum.Div(t, d)
s = dnum.Add(s, t)
}
return s
}
// PiFloat calculates π to its maximum precision of 19 digits using Go's native
// float64.
func PiFloat64() float64 {
var (
lasts = 0.0
t = 3.0
s = 3.0
n = 1.0
na = 0.0
d = 0.0
da = 24.0
)
for s != lasts {
lasts = s
n += na
na += 8
d += da
da += 32
t = (t * n) / d
s = t
}
return s
}
var (
eight = decimal.New(8, 0)
thirtyTwo = decimal.New(32, 0)
)
// PiDecimal_Go calculates π to the desired precision using
// github.com/ericlagergren/decimal with the operating mode set to Go.
func PiDecimal_Go(prec int) *decimal.Big {
var (
ctx = decimal.Context{
Precision: adjustPrecision(prec),
OperatingMode: decimal.Go,
}
lasts = new(decimal.Big)
t = decimal.New(3, 0)
s = decimal.New(3, 0)
n = decimal.New(1, 0)
na = new(decimal.Big)
d = new(decimal.Big)
da = decimal.New(24, 0)
eps = decimal.New(1, prec)
)
for {
lasts.Copy(s)
ctx.Add(n, n, na)
ctx.Add(na, na, eight)
ctx.Add(d, d, da)
ctx.Add(da, da, thirtyTwo)
ctx.Mul(t, t, n)
ctx.Quo(t, t, d)
ctx.Add(s, s, t)
if ctx.Sub(lasts, s, lasts).CmpAbs(eps) < 0 {
return s.Round(prec)
}
}
}
// PiDecimal_GDA calculates π to the desired precision using
// github.com/ericlagergren/decimal with the operating mode set to GDA.
func PiDecimal_GDA(prec int) *decimal.Big {
var (
ctx = decimal.Context{
Precision: adjustPrecision(prec),
OperatingMode: decimal.GDA,
}
lasts = new(decimal.Big)
t = decimal.New(3, 0)
s = decimal.New(3, 0)
n = decimal.New(1, 0)
na = new(decimal.Big)
d = new(decimal.Big)
da = decimal.New(24, 0)
)
for s.Cmp(lasts) != 0 {
lasts.Copy(s)
ctx.Add(n, n, na)
ctx.Add(na, na, eight)
ctx.Add(d, d, da)
ctx.Add(da, da, thirtyTwo)
ctx.Mul(t, t, n)
ctx.Quo(t, t, d)
ctx.Add(s, s, t)
}
return s.Round(prec)
}
var (
apdEight = apd.New(8, 0)
apdThirtyTwo = apd.New(32, 0)
)
// PiAPD calculates π to the desired precision using github.com/cockroachdb/apd.
func PiAPD(prec uint32) *apd.Decimal {
var (
ctx = apd.BaseContext.WithPrecision(uint32(adjustPrecision(int(prec))))
lasts = apd.New(0, 0)
t = apd.New(3, 0)
s = apd.New(3, 0)
n = apd.New(1, 0)
na = apd.New(0, 0)
d = apd.New(0, 0)
da = apd.New(24, 0)
)
for s.Cmp(lasts) != 0 {
lasts.Set(s)
ctx.Add(n, n, na)
ctx.Add(na, na, apdEight)
ctx.Add(d, d, da)
ctx.Add(da, da, apdThirtyTwo)
ctx.Mul(t, t, n)
ctx.Quo(t, t, d)
ctx.Add(s, s, t)
}
ctx.Precision = prec
ctx.Round(s, s)
return s
}