Module: BigMathR::Power

Defined in:
ext/bigdecimal/math_r/power.c,
ext/bigdecimal/math_r/math_r.c

Overview

A module that provides the power functions. It is used internally.

Synopsis

The function names defined are the same as those in the C/C++ standard.

  • Powers: :pow :cpow

  • Square root: :sqrt :csqrt

  • Cubic root: :ccbrt

Follow, the name defined in C/C++ standard though, the function names are different.

  • Cubic root: :cuberoot ( cbrt() for C/C++ )

Reference

C-gengo ni yoru hyoujun algorithm jiten (Japanese) - Haruhiko Okumura (Gijutu Hyouron-sha)

Class Method Summary collapse

Class Method Details

.ccbrt(z, prec) ⇒ Complex

Computes complex cubic root of z.

Examples:

BigMathR::PowerRoot.ccbrt(-2, 20)
#=> (0.6299605249474365824e0+0.10911236359717214036e1i)

Parameters:

  • z (Numeric)

    Numerical argument

  • prec (Integer)

    Arbitrary precision

Returns:

Raises:

  • (ArgumentError)

    Occurs when prec is not a positive integer.

  • (TypeError)

    Occurs when z is not a numeric class.

Since:

  • 0.1.0



171
172
173
174
175
176
# File 'ext/bigdecimal/math_r/power.c', line 171

static VALUE
__impl_power_ccbrt(VALUE unused_obj, VALUE z, VALUE prec)
{
  z = rb_num_canonicalize(z, prec, ARG_COMPLEX, ARG_RAWVALUE);
        return ccbrt_formula(z, prec);
}

.cpow(z, w, prec) ⇒ Complex

Computes the w power of z in complex number.

Examples:

BigMathR::Power.cpow(3i, 4i, 20)
#=> (-0.58378184013294584771e-3-0.17738492380526556875e-2i)

Parameters:

  • z (Numeric)

    Numerical argument

  • w (Numeric)

    Numerical argument

  • prec (Integer)

    Arbitrary precision

Returns:

Raises:

  • (ArgumentError)

    Occurs when prec is not a positive integer.

  • (TypeError)

    Occurs when z is not a numeric class.

  • (TypeError)

    Occurs when w is not a numeric class.

Since:

  • 0.2.0



90
91
92
93
94
# File 'ext/bigdecimal/math_r/power.c', line 90

static VALUE
__impl_power_cpow(VALUE unused_obj, VALUE z, VALUE w, VALUE prec)
{
  return cpow_formula(z, w, prec);
}

.csqrt(z, prec) ⇒ Complex

Computes complex square root of z.

Examples:

BigMathR::PowerRoot.csqrt(-1, 20) == Complex::I
#=> true

Parameters:

  • z (Numeric)

    Numerical argument

  • prec (Integer)

    Arbitrary precision

Returns:

Raises:

  • (ArgumentError)

    Occurs when prec is not a positive integer.

  • (TypeError)

    Occurs when z is not a numeric class.

Since:

  • 0.1.0



130
131
132
133
134
135
# File 'ext/bigdecimal/math_r/power.c', line 130

static VALUE
__impl_power_csqrt(VALUE unused_obj, VALUE z, VALUE prec)
{
  z = rb_num_canonicalize(z, prec, ARG_COMPLEX, ARG_RAWVALUE);
        return csqrt_formula(z, prec);
}

.cuberoot(x, prec) ⇒ BigDecimal

Computes real-valued cubic root of x.

Examples:

BigMathR::PowerRoot.cuberoot(-2, 20)
#=> -0.12599210498948731648e1

Parameters:

  • x (Numeric)

    Numerical argument

  • prec (Integer)

    Arbitrary precision

Returns:

  • (BigDecimal)

    Real solution

Raises:

  • (ArgumentError)

    Occurs when prec is not a positive integer.

  • (TypeError)

    Occurs when x is not a numeric class.

Since:

  • 0.1.0



150
151
152
153
154
155
# File 'ext/bigdecimal/math_r/power.c', line 150

static VALUE
__impl_power_cuberoot(VALUE unused_obj, VALUE x, VALUE prec)
{
  x = rb_num_canonicalize(x, prec, ARG_REAL, ARG_RAWVALUE);
  return cuberoot_newton(x, prec);
}

.pow(x, y, prec) ⇒ BigDecimal

Computes the y power of x.

Examples:

BigMathR::Power.pow(1/2r, 1/2r, 20) * 2
#=> 0.141421356237309504876e1

Parameters:

  • x (Numeric)

    Numerical argument

  • y (Numeric)

    Numerical argument

  • prec (Integer)

    Arbitrary precision

Returns:

  • (BigDecimal)

    Real solution

Raises:

  • (ArgumentError)

    Occurs when prec is not a positive integer.

  • (TypeError)

    Occurs when x is not a numeric class.

  • (TypeError)

    Occurs when y is not a numeric class.

Since:

  • 0.2.0



69
70
71
72
73
# File 'ext/bigdecimal/math_r/power.c', line 69

static VALUE
__impl_power_pow(VALUE unused_obj, VALUE x, VALUE y, VALUE prec)
{
  return pow_formula(x, y, prec);
}

.sqrt(x, prec) ⇒ BigDecimal

Computes square root of x.

Examples:

BigMathR::PowerRoot.sqrt(2, 20)
#=> 0.14142135623730950488e1

Parameters:

  • x (Numeric)

    Numerical argument

  • prec (Integer)

    Arbitrary precision

Returns:

  • (BigDecimal)

    Real solution

Raises:

  • (ArgumentError)

    Occurs when prec is not a positive integer.

  • (TypeError)

    Occurs when x is not a numeric class.

Since:

  • 0.1.0



109
110
111
112
113
114
115
# File 'ext/bigdecimal/math_r/power.c', line 109

static VALUE
__impl_power_sqrt(VALUE unused_obj, VALUE x, VALUE prec)
{
  VALUE y;
  y = rb_bigmath_sqrt(x, prec);
  return rb_num_round(y, prec);
}