Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 12 additions & 31 deletions lib/bigdecimal/math.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,39 +198,20 @@ def atan(x, prec)
# #=> "0.31415926535897932384626433832795e1"
#
def PI(prec)
# Gauss–Legendre algorithm
BigDecimal::Internal.validate_prec(prec, :PI)
n = prec + BigDecimal.double_fig
zero = BigDecimal("0")
one = BigDecimal("1")
two = BigDecimal("2")

m25 = BigDecimal("-0.04")
m57121 = BigDecimal("-57121")

pi = zero

d = one
k = one
t = BigDecimal("-80")
while d.nonzero? && ((m = n - (pi.exponent - d.exponent).abs) > 0)
m = BigDecimal.double_fig if m < BigDecimal.double_fig
t = t*m25
d = t.div(k,m)
k = k+two
pi = pi + d
end

d = one
k = one
t = BigDecimal("956")
while d.nonzero? && ((m = n - (pi.exponent - d.exponent).abs) > 0)
m = BigDecimal.double_fig if m < BigDecimal.double_fig
t = t.div(m57121,n)
d = t.div(k,m)
pi = pi + d
k = k+two
n = prec + BigDecimal.double_fig
a = BigDecimal(1)
b = BigDecimal(0.5, 0).sqrt(n)
s = BigDecimal(0.25, 0)
t = 1
while a != b && (a - b).exponent > 1 - n
c = (a - b).div(2, n)
a, b = (a + b).div(2, n), (a * b).sqrt(n)
s = s.sub(c * c * t, n)
t *= 2
end
pi.mult(1, prec)
(a * b).div(s, prec)
end

# call-seq:
Expand Down
Loading