Module: BigMathR::Const::LOG2

Defined in:
lib/bigdecimal/math_r/const/LOG2_bbp2007.rb,
ext/bigdecimal/math_r/math_r.c

Class Method Summary collapse

Class Method Details

.bbp2002(prec) ⇒ BigDecimal

Implement by BBP’s formula (Borwein and Bailey 2002)

Examples:

BigMathR::Const::LOG2.bbp2002(20)
#=> 0.69314718055994530942e0

Parameters:

  • prec (Integer)

    Arbitrary precision

Returns:

  • (BigDecimal)

    Natural logarithm of 2

Raises:

  • (TypeError)

    not an Integer

  • (RangeError)

    Zero or negative precision



194
195
196
197
198
# File 'ext/bigdecimal/math_r/const.c', line 194

static VALUE
__impl_const_log2_bbp2002(VALUE unused_obj, VALUE prec)
{
	return LOG2_BBP2002(prec);
}

.bbp2007(prec) ⇒ BigDecimal

Implement by BBP’s formula (Bailey et al. 2007)

Examples:

BigMathR::Const::LOG2.bbp2007(20)
#=> 0.69314718055994530942e0

Parameters:

  • prec (Integer)

    Arbitrary precision

Returns:

  • (BigDecimal)

    Natural logarithm of 2

Raises:

  • (TypeError)

    not an Integer

  • (RangeError)

    Zero or negative precision



16
17
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/LOG2_bbp2007.rb', line 16

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

  n = prec + BigDecimal.double_fig
  one = BigDecimal(1)
  two = BigDecimal(2)
  i1 = one
  i2 = two
  d = one
  y = BigDecimal(0)

  while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
    m = BigDecimal.double_fig if m < BigDecimal.double_fig
    d = one.div(i1 * i2, m)
    y = y + d
    i1 += one
    i2 *= two
  end
  y.round(prec)
end