Skip to content
Open
Show file tree
Hide file tree
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 @@ -885,39 +885,20 @@ def ldexp(x, exponent)
# #=> "0.31415926535897932384626433832795e1"
#
def PI(prec)
# Gauss–Legendre algorithm
prec = BigDecimal::Internal.coerce_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
2 changes: 1 addition & 1 deletion sample/pi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# pi.rb
#
# Calculates 3.1415.... (the number of times that a circle's diameter
# will fit around the circle) using J. Machin's formula.
# will fit around the circle)
#

require "bigdecimal"
Expand Down
Loading