Module: BigMathR::Const::PI

Defined in:
lib/bigdecimal/math_r/const/PI_euler.rb,
lib/bigdecimal/math_r/const/PI_chudnovsky.rb,
lib/bigdecimal/math_r/const/PI_ramanujan1.rb,
lib/bigdecimal/math_r/const/PI_ramanujan2.rb,
ext/bigdecimal/math_r/math_r.c

Class Method Summary collapse

Class Method Details

.chudnovsky(prec) ⇒ BigDecimal

Implement by Chudnovsky’s formula

Examples:

BigMathR::Const::PI.chudnovsky(20)
#=> 0.31415926535897932385e1

Parameters:

  • prec (Integer)

    Arbitrary precision

Returns:

  • (BigDecimal)

    Constant PI

Raises:

  • (TypeError)

    not an Integer

  • (RangeError)

    Zero or negative precision



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
# File 'lib/bigdecimal/math_r/const/PI_chudnovsky.rb', line 18

def chudnovsky(prec)
  raise TypeError, "precision must be an Integer" unless prec.class == Integer
  raise RangeError, "Zero or negative precision" if prec <= 0

  zero = BigDecimal(0)
  one = BigDecimal(1)

  n = prec + BigDecimal.double_fig

  m1_640320_3 = BigDecimal(-1/262537412640768000r, n)
  p90856689_711822400 = BigDecimal(90856689/711822400r, n)
  p90856689_711822400 *= BigDecimal(10005).sqrt(n)

  _C = one
  _AB = BigDecimal(13591409/545140134r, n)
  _F = 1r

  d = one
  y = zero

  i = 0

  while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
    m = BigDecimal.double_fig if m < BigDecimal.double_fig

    d = BigDecimal(_F, m).mult(_AB * _C, m)
    y = y + d

    _AB += one
    _C *= m1_640320_3
    i = i.succ
    (6 * i - 5).step(6 * i) {|k| _F *= k}
    (3 * i - 2).step(3 * i) {|k| _F /= k}
    3.times { _F /= i }
  end
  
  y = y.mult(p90856689_711822400, n)
  one.div(y, prec)
end

.euler(prec) ⇒ BigDecimal

Implement by Euler’s formula.

Examples:

BigMathR::Const::PI.euler(20)
#=> 0.31415926535897932385e1

Parameters:

  • prec (Integer)

    Arbitrary precision

Returns:

  • (BigDecimal)

    Constant PI

Raises:

  • (TypeError)

    not an Integer

  • (RangeError)

    Zero or negative precision



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bigdecimal/math_r/const/PI_euler.rb', line 18

def euler(prec)
  raise TypeError, "precision must be an Integer" unless prec.class == Integer
  raise RangeError, "Zero or negative precision" if prec <= 0
  n = BigDecimal.double_fig + prec
  one = BigDecimal(1)
  two = BigDecimal(2)
  s = BigDecimal(0)
  t = one
  t1 = BigDecimal(0)
  t2 = one
  n = prec + BigDecimal.double_fig
  while t.nonzero? && ((m = n - (s.exponent - t.exponent).abs) > 0)
    m = BigDecimal.double_fig if m < BigDecimal.double_fig
    s = s + t
    t1 = t1 + one;  t = t.mult(t1, m)
    t2 = t2 + two;  t = t.div(t2, m)
  end
  two.mult(s, prec)
end

.ramanujan1(prec) ⇒ BigDecimal

Implement by Ramanujan’s formula.

Examples:

BigMathR::Const::PI.ramanujan1(20)
#=> 0.31415926535897932385e1

Parameters:

  • prec (Integer)

    Arbitrary precision

Returns:

  • (BigDecimal)

    Constant PI

Raises:

  • (TypeError)

    not an Integer

  • (RangeError)

    Zero or negative precision



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
# File 'lib/bigdecimal/math_r/const/PI_ramanujan1.rb', line 18

def ramanujan1(prec)
  raise TypeError, "precision must be an Integer" unless prec.class == Integer
  raise RangeError, "Zero or negative precision" if prec <= 0
  n = BigDecimal.double_fig + prec

  i = 0
  r = 1/9801r
  k = 1103

  d = BigDecimal(1)
  y = BigDecimal(0)

  while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
    m = BigDecimal.double_fig if m < BigDecimal.double_fig
    d = BigDecimal(r * k, m)
    y = y + d
    i = i.succ
    k += 26390
    r *= (2 * i - 1) * (4 * i - 3) * (4 * i - 1)
    r /= 96059601
    r /= 32
    3.times { r /= i }
  end
  BigDecimal(2).sqrt(n).div(y * BigDecimal(4), prec)
end

.ramanujan2(prec) ⇒ BigDecimal

Implement by Ramanujan’s formula.

Examples:

BigMathR::Const::PI.ramanujan2(20)
#=> 0.31415926535897932385e1

Parameters:

  • prec (Integer)

    Arbitrary precision

Returns:

  • (BigDecimal)

    Constant PI

Raises:

  • (TypeError)

    not an Integer

  • (RangeError)

    Zero or negative precision



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
# File 'lib/bigdecimal/math_r/const/PI_ramanujan2.rb', line 18

def ramanujan2(prec)
  raise TypeError, "precision must be an Integer" unless prec.class == Integer
  raise RangeError, "Zero or negative precision" if prec <= 0
  n = BigDecimal.double_fig + prec

  i = 0
  r = 1/882r
  k = 1123

  d = BigDecimal(1)
  y = BigDecimal(0)

  while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
    m = BigDecimal.double_fig if m < BigDecimal.double_fig
    d = BigDecimal(r * k, m)
    y = y + d
    i = i.succ
    k += 21460
    r = -r
    r *= (2 * i - 1) * (4 * i - 3) * (4 * i - 1)
    r /= 777924
    r /= 32
    3.times { r /= i }
  end
  BigDecimal(4).div(y, prec)
end