Module: BigMathR::Const::E

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

Class Method Summary collapse

Class Method Details

.euler(prec) ⇒ BigDecimal

Implement by continued fraction.

Examples:

BigMathR::Const::E.euler(20)
#=> 0.271828182845904523536e1

Parameters:

  • prec (Integer)

    Arbitrary precision

Returns:

  • (BigDecimal)

    Constant e.

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
37
38
39
40
41
42
# File 'lib/bigdecimal/math_r/const/E_euler.rb', line 16

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

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

  c = one
  b = one
  p1 = one;  q1 = zero;  p2 = two;  q2 = one;
  i = one
  loop do
    prev = p2
    t = p1 * c + p2 * b;  p1 = p2;  p2 = t;
    t = q1 * c + q2 * b;  q1 = q2;  q2 = t;
    if q2.nonzero?
      p1 = p1.div(q2, n);  q1 = q1.div(q2, n);  p2 = p2.div(q2, n);  q2 = one;
    end
    i += one
    c = one
    b = (i + one) % 3 == 0 ? ((i.to_i.succ) / 3) * two : one
    break unless q2.nonzero? && prev != p2
  end
  p2.round(prec)
end

.euler_number(prec) ⇒ BigDecimal

Implement by series expansion: sum_(n=0)^infty 1/n!

Examples:

BigMathR::Const::E.napier(20)
#=> 0.27182818284590452354e1

Parameters:

  • prec (Integer)

    Arbitrary precision

Returns:

  • (BigDecimal)

    Natural logarithm of 2

Raises:

  • (TypeError)

    not an Integer

  • (RangeError)

    Zero or negative precision



160
161
162
163
164
# File 'ext/bigdecimal/math_r/const.c', line 160

static VALUE
__impl_const_e_napier(VALUE unused_obj, VALUE prec)
{
	return E_napier(prec);
}

.napier(prec) ⇒ BigDecimal

Implement by series expansion: sum_(n=0)^infty 1/n!

Examples:

BigMathR::Const::E.napier(20)
#=> 0.27182818284590452354e1

Parameters:

  • prec (Integer)

    Arbitrary precision

Returns:

  • (BigDecimal)

    Natural logarithm of 2

Raises:

  • (TypeError)

    not an Integer

  • (RangeError)

    Zero or negative precision



160
161
162
163
164
# File 'ext/bigdecimal/math_r/const.c', line 160

static VALUE
__impl_const_e_napier(VALUE unused_obj, VALUE prec)
{
	return E_napier(prec);
}