123456789_123456789_123456789_123456789_123456789_

Class: Complex

Relationships & Source Files
Namespace Children
Classes:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ::Numeric
Instance Chain:
Inherits: Numeric
Defined in: complex.c

Overview

A complex number can be represented as a paired real number with imaginary unit; a+bi. Where a is real part, b is imaginary part and i is imaginary unit. Real a equals complex a+0i mathematically.

You can create a Complex object explicitly with:

  • A complex literal.

You can convert certain objects to Complex objects with:

  • Method {Kernel.html#method-i-Complex }.

Complex object can be created as literal, and also by using Kernel.Complex, .rect, .polar or to_c method.

2+1i                 #=> (2+1i)
Complex(1)           #=> (1+0i)
Complex(2, 3)        #=> (2+3i)
Complex.polar(2, 3)  #=> (-1.9799849932008908+0.2822400161197344i)
3.to_c               #=> (3+0i)

You can also create complex object from floating-point numbers or strings.

Complex(0.3)         #=> (0.3+0i)
Complex('0.3-0.5i')  #=> (0.3-0.5i)
Complex('2/3+3/4i')  #=> ((2/3)+(3/4)*i)
Complex('1@2')       #=> (-0.4161468365471424+0.9092974268256817i)

0.3.to_c             #=> (0.3+0i)
'0.3-0.5i'.to_c      #=> (0.3-0.5i)
'2/3+3/4i'.to_c      #=> ((2/3)+(3/4)*i)
'1@2'.to_c           #=> (-0.4161468365471424+0.9092974268256817i)

A complex object is either an exact or an inexact number.

Complex(1, 1) / 2    #=> ((1/2)+(1/2)*i)
Complex(1, 1) / 2.0  #=> (0.5+0.5i)

Constant Summary

Class Method Summary

Instance Attribute Summary

::Numeric - Inherited

#finite?

Returns true if num is a finite number, otherwise returns false.

#infinite?

Returns nil, -1, or 1 depending on whether the value is finite, -Infinity, or +Infinity.

#integer?

Returns true if num is an ::Integer.

#negative?

Returns true if self is less than 0, false otherwise.

#nonzero?

Returns self if self is not a zero value, nil otherwise; uses method zero? for the evaluation.

#positive?

Returns true if self is greater than 0, false otherwise.

#real

Returns self.

#real?

Returns true if num is a real number (i.e.

#zero?

Returns true if zero has a zero value, false otherwise.

Instance Method Summary

::Numeric - Inherited

#%

Returns self modulo other as a real number.

#+@

Returns self.

#-@

Unary Minus—Returns the receiver, negated.

#<=>

Returns zero if self is the same as other, nil otherwise.

#abs

Returns the absolute value of self.

#abs2

Returns square of self.

#angle

Alias for Numeric#arg.

#arg

Returns 0 if the value is positive, pi otherwise.

#ceil

Returns the smallest number that is greater than or equal to self with a precision of digits decimal digits.

#clone

Returns self.

#coerce

Returns a 2-element array containing two numeric elements, formed from the two operands self and other, of a common compatible type.

#conj

Returns self.

#conjugate

Alias for Numeric#conj.

#denominator

Returns the denominator (always positive).

#div

Returns the quotient self/other as an integer (via floor), using method / in the derived class of self.

#divmod

Returns a 2-element array [q, r], where.

#dup

Returns self.

#eql?

Returns true if self and other are the same type and have equal values.

#fdiv

Returns the quotient self/other as a float, using method / in the derived class of self.

#floor

Returns the largest number that is less than or equal to self with a precision of digits decimal digits.

#i

Returns Complex(0, self):

#imag

Returns zero.

#imaginary

Alias for Numeric#imag.

#magnitude

Alias for Numeric#abs.

#modulo

Alias for Numeric#%.

#numerator

Returns the numerator.

#phase

Alias for Numeric#arg.

#polar

Returns an array; [num.abs, num.arg].

#quo

Returns the most exact division (rational for integers, float for floats).

#rect

Returns an array; [num, 0].

#rectangular

Alias for Numeric#rect.

#remainder

Returns the remainder after dividing self by other.

#round

Returns self rounded to the nearest value with a precision of digits decimal digits.

#step

Generates a sequence of numbers; with a block given, traverses the sequence.

#to_c

Returns the value as a complex.

#to_int

Returns self as an integer; converts using method #to_i in the derived class.

#truncate

Returns self truncated (toward zero) to a precision of digits decimal digits.

#singleton_method_added

Trap attempts to add methods to ::Numeric objects.

::Comparable - Included

#<

Compares two objects based on the receiver’s #<=> method, returning true if it returns a value less than 0.

#<=

Compares two objects based on the receiver’s #<=> method, returning true if it returns a value less than or equal to 0.

#==

Compares two objects based on the receiver’s #<=> method, returning true if it returns 0.

#>

Compares two objects based on the receiver’s #<=> method, returning true if it returns a value greater than 0.

#>=

Compares two objects based on the receiver’s #<=> method, returning true if it returns a value greater than or equal to 0.

#between?

Returns false if obj #<=> min is less than zero or if obj #<=> max is greater than zero, true otherwise.

#clamp

In (min, max) form, returns min if obj #<=> min is less than zero, max if obj #<=> max is greater than zero, and obj otherwise.

Class Method Details

.polar(abs[, arg]) ⇒ Complex

Returns a complex object which denotes the given polar form.

Complex.polar(3, 0)            #=> (3.0+0.0i)
Complex.polar(3, Math::PI/2)   #=> (1.836909530733566e-16+3.0i)
Complex.polar(3, Math::PI)     #=> (-3.0+3.673819061467132e-16i)
Complex.polar(3, -Math::PI/2)  #=> (1.836909530733566e-16-3.0i)
[ GitHub ]

  
# File 'complex.c', line 692

static VALUE
nucomp_s_polar(int argc, VALUE *argv, VALUE klass)
{
    VALUE abs, arg;

    argc = rb_scan_args(argc, argv, "11", &abs, &arg);
    nucomp_real_check(abs);
    if (argc == 2) {
        nucomp_real_check(arg);
    }
    else {
        arg = ZERO;
    }
    if (RB_TYPE_P(abs, T_COMPLEX)) {
        get_dat1(abs);
        abs = dat->real;
    }
    if (RB_TYPE_P(arg, T_COMPLEX)) {
        get_dat1(arg);
        arg = dat->real;
    }
    return f_complex_polar(klass, abs, arg);
}

.rect(real[, imag]) ⇒ Complex .rectangular(real[, imag]) ⇒ Complex
Also known as: .rectangular

Returns a complex object which denotes the given rectangular form.

Complex.rectangular(1, 2)  #=> (1+2i)
[ GitHub ]

  
# File 'complex.c', line 476

static VALUE
nucomp_s_new(int argc, VALUE *argv, VALUE klass)
{
    VALUE real, imag;

    switch (rb_scan_args(argc, argv, "11", &real, &imag)) {
      case 1:
	nucomp_real_check(real);
	imag = ZERO;
	break;
      default:
	nucomp_real_check(real);
	nucomp_real_check(imag);
	break;
    }

    return nucomp_s_canonicalize_internal(klass, real, imag);
}

.rect(real[, imag]) ⇒ Complex .rectangular(real[, imag]) ⇒ Complex

Alias for .rect.

Instance Attribute Details

#finite?Boolean (readonly)

Returns true if cmp‘s real and imaginary parts are both finite numbers, otherwise returns false.

[ GitHub ]

  
# File 'complex.c', line 1446

static VALUE
rb_complex_finite_p(VALUE self)
{
    get_dat1(self);

    return RBOOL(f_finite_p(dat->real) && f_finite_p(dat->imag));
}

#infinite?Boolean (readonly)

Returns 1 if cmp‘s real or imaginary part is an infinite number, otherwise returns nil.

For example:

   (1+1i).infinite?                   #=> nil
   (Float::INFINITY + 1i).infinite?   #=> 1
[ GitHub ]

  
# File 'complex.c', line 1466

static VALUE
rb_complex_infinite_p(VALUE self)
{
    get_dat1(self);

    if (!f_infinite_p(dat->real) && !f_infinite_p(dat->imag)) {
	return Qnil;
    }
    return ONE;
}

#realNumeric (readonly)

Returns the real part.

Complex(7).real      #=> 7
Complex(9, -4).real  #=> 9
[ GitHub ]

  
# File 'complex.c', line 725

VALUE
rb_complex_real(VALUE self)
{
    get_dat1(self);
    return dat->real;
}

Complex(1).real?) ⇒ Boolean (readonly) Complex(1, 2).real?) ⇒ Boolean

Returns false, even if the complex number has no imaginary part.

[ GitHub ]

  
# File 'complex.c', line 1267

static VALUE
nucomp_real_p_m(VALUE self)
{
    return Qfalse;
}

Instance Method Details

#*(numeric) ⇒ Complex

Performs multiplication.

Complex(2, 3)  * Complex(2, 3)   #=> (-5+12i)
Complex(900)   * Complex(1)      #=> (900+0i)
Complex(-2, 9) * Complex(-9, 2)  #=> (0-85i)
Complex(9, 8)  * 4               #=> (36+32i)
Complex(20, 9) * 9.8             #=> (196.0+88.2i)
[ GitHub ]

  
# File 'complex.c', line 871

VALUE
rb_complex_mul(VALUE self, VALUE other)
{
    if (RB_TYPE_P(other, T_COMPLEX)) {
	VALUE real, imag;
	get_dat2(self, other);

        comp_mul(adat->real, adat->imag, bdat->real, bdat->imag, &real, &imag);

	return f_complex_new2(CLASS_OF(self), real, imag);
    }
    if (k_numeric_p(other) && f_real_p(other)) {
	get_dat1(self);

	return f_complex_new2(CLASS_OF(self),
			      f_mul(dat->real, other),
			      f_mul(dat->imag, other));
    }
    return rb_num_coerce_bin(self, other, '*');
}

#**(numeric) ⇒ Complex

Performs exponentiation.

Complex('i') ** 2              #=> (-1+0i)
Complex(-8) ** Rational(1, 3)  #=> (1.0000000000000002+1.7320508075688772i)
[ GitHub ]

  
# File 'complex.c', line 984

VALUE
rb_complex_pow(VALUE self, VALUE other)
{
    if (k_numeric_p(other) && k_exact_zero_p(other))
	return f_complex_new_bang1(CLASS_OF(self), ONE);

    if (RB_TYPE_P(other, T_RATIONAL) && RRATIONAL(other)->den == LONG2FIX(1))
	other = RRATIONAL(other)->num; /* c14n */

    if (RB_TYPE_P(other, T_COMPLEX)) {
	get_dat1(other);

	if (k_exact_zero_p(dat->imag))
	    other = dat->real; /* c14n */
    }

    if (RB_TYPE_P(other, T_COMPLEX)) {
	VALUE r, theta, nr, ntheta;

	get_dat1(other);

	r = f_abs(self);
	theta = f_arg(self);

	nr = m_exp_bang(f_sub(f_mul(dat->real, m_log_bang(r)),
			      f_mul(dat->imag, theta)));
	ntheta = f_add(f_mul(theta, dat->real),
		       f_mul(dat->imag, m_log_bang(r)));
	return f_complex_polar(CLASS_OF(self), nr, ntheta);
    }
    if (FIXNUM_P(other)) {
        long n = FIX2LONG(other);
        if (n == 0) {
            return nucomp_s_new_internal(CLASS_OF(self), ONE, ZERO);
        }
        if (n < 0) {
            self = f_reciprocal(self);
            other = rb_int_uminus(other);
            n = -n;
        }
        {
            get_dat1(self);
            VALUE xr = dat->real, xi = dat->imag, zr = xr, zi = xi;

            if (f_zero_p(xi)) {
                zr = rb_num_pow(zr, other);
            }
            else if (f_zero_p(xr)) {
                zi = rb_num_pow(zi, other);
                if (n & 2) zi = f_negate(zi);
                if (!(n & 1)) {
                    VALUE tmp = zr;
                    zr = zi;
                    zi = tmp;
                }
            }
            else {
                while (--n) {
                    long q, r;

                    for (; q = n / 2, r = n % 2, r == 0; n = q) {
                        VALUE tmp = f_sub(f_mul(xr, xr), f_mul(xi, xi));
                        xi = f_mul(f_mul(TWO, xr), xi);
                        xr = tmp;
                    }
                    comp_mul(zr, zi, xr, xi, &zr, &zi);
                }
            }
            return nucomp_s_new_internal(CLASS_OF(self), zr, zi);
	}
    }
    if (k_numeric_p(other) && f_real_p(other)) {
	VALUE r, theta;

	if (RB_BIGNUM_TYPE_P(other))
	    rb_warn("in a**b, b may be too big");

	r = f_abs(self);
	theta = f_arg(self);

	return f_complex_polar(CLASS_OF(self), f_expt(r, other),
			       f_mul(theta, other));
    }
    return rb_num_coerce_bin(self, other, id_expt);
}

#+(numeric) ⇒ Complex

Performs addition.

Complex(2, 3)  + Complex(2, 3)   #=> (4+6i)
Complex(900)   + Complex(1)      #=> (901+0i)
Complex(-2, 9) + Complex(-9, 2)  #=> (-11+11i)
Complex(9, 8)  + 4               #=> (13+8i)
Complex(20, 9) + 9.8             #=> (29.8+9i)
[ GitHub ]

  
# File 'complex.c', line 777

VALUE
rb_complex_plus(VALUE self, VALUE other)
{
    if (RB_TYPE_P(other, T_COMPLEX)) {
	VALUE real, imag;

	get_dat2(self, other);

	real = f_add(adat->real, bdat->real);
	imag = f_add(adat->imag, bdat->imag);

	return f_complex_new2(CLASS_OF(self), real, imag);
    }
    if (k_numeric_p(other) && f_real_p(other)) {
	get_dat1(self);

	return f_complex_new2(CLASS_OF(self),
			      f_add(dat->real, other), dat->imag);
    }
    return rb_num_coerce_bin(self, other, '+');
}

#-(numeric) ⇒ Complex

Performs subtraction.

Complex(2, 3)  - Complex(2, 3)   #=> (0+0i)
Complex(900)   - Complex(1)      #=> (899+0i)
Complex(-2, 9) - Complex(-9, 2)  #=> (7+7i)
Complex(9, 8)  - 4               #=> (5+8i)
Complex(20, 9) - 9.8             #=> (10.2+9i)
[ GitHub ]

  
# File 'complex.c', line 811

VALUE
rb_complex_minus(VALUE self, VALUE other)
{
    if (RB_TYPE_P(other, T_COMPLEX)) {
	VALUE real, imag;

	get_dat2(self, other);

	real = f_sub(adat->real, bdat->real);
	imag = f_sub(adat->imag, bdat->imag);

	return f_complex_new2(CLASS_OF(self), real, imag);
    }
    if (k_numeric_p(other) && f_real_p(other)) {
	get_dat1(self);

	return f_complex_new2(CLASS_OF(self),
			      f_sub(dat->real, other), dat->imag);
    }
    return rb_num_coerce_bin(self, other, '-');
}

#-Complex

Returns negation of the value.

-Complex(1, 2)  #=> (-1-2i)
[ GitHub ]

  
# File 'complex.c', line 757

VALUE
rb_complex_uminus(VALUE self)
{
    get_dat1(self);
    return f_complex_new2(CLASS_OF(self),
			  f_negate(dat->real), f_negate(dat->imag));
}

#/(numeric) ⇒ Complex #quo(numeric) ⇒ Complex

Performs division.

Complex(2, 3)  / Complex(2, 3)   #=> ((1/1)+(0/1)*i)
Complex(900)   / Complex(1)      #=> ((900/1)+(0/1)*i)
Complex(-2, 9) / Complex(-9, 2)  #=> ((36/85)-(77/85)*i)
Complex(9, 8)  / 4               #=> ((9/4)+(2/1)*i)
Complex(20, 9) / 9.8             #=> (2.0408163265306123+0.9183673469387754i)
[ GitHub ]

  
# File 'complex.c', line 947

VALUE
rb_complex_div(VALUE self, VALUE other)
{
    return f_divide(self, other, f_quo, id_quo);
}

#<=>(object) ⇒ 0, ...

If cmp‘s imaginary part is zero, and object is also a real number (or a Complex number where the imaginary part is zero), compare the real part of cmp to object. Otherwise, return nil.

Complex(2, 3)  <=> Complex(2, 3)   #=> nil
Complex(2, 3)  <=> 1               #=> nil
Complex(2)     <=> 1               #=> 1
Complex(2)     <=> 2               #=> 0
Complex(2)     <=> 3               #=> -1
[ GitHub ]

  
# File 'complex.c', line 1120

static VALUE
nucomp_cmp(VALUE self, VALUE other)
{
    if (nucomp_real_p(self) && k_numeric_p(other)) {
        if (RB_TYPE_P(other, T_COMPLEX) && nucomp_real_p(other)) {
            get_dat2(self, other);
            return rb_funcall(adat->real, idCmp, 1, bdat->real);
        }
        else if (f_real_p(other)) {
            get_dat1(self);
            return rb_funcall(dat->real, idCmp, 1, other);
        }
    }
    return Qnil;
}

#==(object) ⇒ Boolean

Returns true if cmp equals object numerically.

Complex(2, 3)  == Complex(2, 3)   #=> true
Complex(5)     == 5               #=> true
Complex(0)     == 0.0             #=> true
Complex('1/3') == 0.33            #=> false
Complex('1/2') == '1/2'           #=> false
[ GitHub ]

  
# File 'complex.c', line 1082

static VALUE
nucomp_eqeq_p(VALUE self, VALUE other)
{
    if (RB_TYPE_P(other, T_COMPLEX)) {
	get_dat2(self, other);

	return RBOOL(f_eqeq_p(adat->real, bdat->real) &&
			  f_eqeq_p(adat->imag, bdat->imag));
    }
    if (k_numeric_p(other) && f_real_p(other)) {
	get_dat1(self);

	return RBOOL(f_eqeq_p(dat->real, other) && f_zero_p(dat->imag));
    }
    return RBOOL(f_eqeq_p(other, self));
}

#absNumeric #magnitudeNumeric
Also known as: #magnitude

Returns the absolute part of its polar form.

Complex(-1).abs         #=> 1
Complex(3.0, -4.0).abs  #=> 5.0
[ GitHub ]

  
# File 'complex.c', line 1160

VALUE
rb_complex_abs(VALUE self)
{
    get_dat1(self);

    if (f_zero_p(dat->real)) {
	VALUE a = f_abs(dat->imag);
	if (RB_FLOAT_TYPE_P(dat->real) && !RB_FLOAT_TYPE_P(dat->imag))
	    a = f_to_f(a);
	return a;
    }
    if (f_zero_p(dat->imag)) {
	VALUE a = f_abs(dat->real);
	if (!RB_FLOAT_TYPE_P(dat->real) && RB_FLOAT_TYPE_P(dat->imag))
	    a = f_to_f(a);
	return a;
    }
    return rb_math_hypot(dat->real, dat->imag);
}

#abs2Numeric

Returns square of the absolute value.

Complex(-1).abs2         #=> 1
Complex(3.0, -4.0).abs2  #=> 25.0
[ GitHub ]

  
# File 'complex.c', line 1189

static VALUE
nucomp_abs2(VALUE self)
{
    get_dat1(self);
    return f_add(f_mul(dat->real, dat->real),
		 f_mul(dat->imag, dat->imag));
}

#argFloat #angleFloat #phaseFloat

Alias for #arg.

#argFloat #angleFloat #phaseFloat
Also known as: #angle, #phase

Returns the angle part of its polar form.

Complex.polar(3, Math::PI/2).arg  #=> 1.5707963267948966
[ GitHub ]

  
# File 'complex.c', line 1207

VALUE
rb_complex_arg(VALUE self)
{
    get_dat1(self);
    return rb_math_atan2(dat->imag, dat->real);
}

#coerce(other)

This method is for internal use only.
[ GitHub ]

  
# File 'complex.c', line 1137

static VALUE
nucomp_coerce(VALUE self, VALUE other)
{
    if (RB_TYPE_P(other, T_COMPLEX))
	return rb_assoc_new(other, self);
    if (k_numeric_p(other) && f_real_p(other))
        return rb_assoc_new(f_complex_new_bang1(CLASS_OF(self), other), self);

    rb_raise(rb_eTypeError, "%"PRIsVALUE" can't be coerced into %"PRIsVALUE,
	     rb_obj_class(other), rb_obj_class(self));
    return Qnil;
}

#conjComplex #conjugateComplex
Also known as: #conjugate

Returns the complex conjugate.

Complex(1, 2).conjugate  #=> (1-2i)
[ GitHub ]

  
# File 'complex.c', line 1253

VALUE
rb_complex_conjugate(VALUE self)
{
    get_dat1(self);
    return f_complex_new2(CLASS_OF(self), dat->real, f_negate(dat->imag));
}

#conjComplex #conjugateComplex

Alias for #conj.

#denominatorInteger

Returns the denominator (lcm of both denominator - real and imag).

See numerator.

[ GitHub ]

  
# File 'complex.c', line 1281

static VALUE
nucomp_denominator(VALUE self)
{
    get_dat1(self);
    return rb_lcm(f_denominator(dat->real), f_denominator(dat->imag));
}

#eql?(other) ⇒ Boolean

This method is for internal use only.
[ GitHub ]

  
# File 'complex.c', line 1344

static VALUE
nucomp_eql_p(VALUE self, VALUE other)
{
    if (RB_TYPE_P(other, T_COMPLEX)) {
	get_dat2(self, other);

	return RBOOL((CLASS_OF(adat->real) == CLASS_OF(bdat->real)) &&
			  (CLASS_OF(adat->imag) == CLASS_OF(bdat->imag)) &&
			  f_eqeq_p(self, other));

    }
    return Qfalse;
}

#fdiv(numeric) ⇒ Complex

Performs division as each part is a float, never returns a float.

Complex(11, 22).fdiv(3)  #=> (3.6666666666666665+7.333333333333333i)
[ GitHub ]

  
# File 'complex.c', line 963

static VALUE
nucomp_fdiv(VALUE self, VALUE other)
{
    return f_divide(self, other, f_fdiv, id_fdiv);
}

#hash

[ GitHub ]

  
# File 'complex.c', line 1337

static VALUE
nucomp_hash(VALUE self)
{
    return ST2FIX(rb_complex_hash(self));
}

#imagNumeric #imaginaryNumeric
Also known as: #imaginary

Returns the imaginary part.

Complex(7).imaginary      #=> 0
Complex(9, -4).imaginary  #=> -4
[ GitHub ]

  
# File 'complex.c', line 742

VALUE
rb_complex_imag(VALUE self)
{
    get_dat1(self);
    return dat->imag;
}

#imagNumeric #imaginaryNumeric

Alias for #imag.

#inspectString

Returns the value as a string for inspection.

Complex(2).inspect                       #=> "(2+0i)"
Complex('-8/6').inspect                  #=> "((-4/3)+0i)"
Complex('1/2i').inspect                  #=> "(0+(1/2)*i)"
Complex(0, Float::INFINITY).inspect      #=> "(0+Infinity*i)"
Complex(Float::NAN, Float::NAN).inspect  #=> "(NaN+NaN*i)"
[ GitHub ]

  
# File 'complex.c', line 1425

static VALUE
nucomp_inspect(VALUE self)
{
    VALUE s;

    s = rb_usascii_str_new2("(");
    rb_str_concat(s, f_format(self, rb_inspect));
    rb_str_cat2(s, ")");

    return s;
}

#absNumeric #magnitudeNumeric

Alias for #abs.

#marshal_dump (private)

This method is for internal use only.
[ GitHub ]

  
# File 'complex.c', line 1498

static VALUE
nucomp_marshal_dump(VALUE self)
{
    VALUE a;
    get_dat1(self);

    a = rb_assoc_new(dat->real, dat->imag);
    rb_copy_generic_ivar(a, self);
    return a;
}

#numeratorNumeric

Returns the numerator.

    1   2       3+4i  <-  numerator
    - + -i  ->  ----
    2   3        6    <-  denominator

c = Complex('1/2+2/3i')  #=> ((1/2)+(2/3)*i)
n = c.numerator          #=> (3+4i)
d = c.denominator        #=> 6
n / d                    #=> ((1/2)+(2/3)*i)
Complex(Rational(n.real, d), Rational(n.imag, d))
                         #=> ((1/2)+(2/3)*i)

See denominator.

[ GitHub ]

  
# File 'complex.c', line 1306

static VALUE
nucomp_numerator(VALUE self)
{
    VALUE cd;

    get_dat1(self);

    cd = nucomp_denominator(self);
    return f_complex_new2(CLASS_OF(self),
			  f_mul(f_numerator(dat->real),
				f_div(cd, f_denominator(dat->real))),
			  f_mul(f_numerator(dat->imag),
				f_div(cd, f_denominator(dat->imag))));
}

#argFloat #angleFloat #phaseFloat

Alias for #arg.

#polarArray

Returns an array; [cmp.abs, cmp.arg].

Complex(1, 2).polar  #=> [2.23606797749979, 1.1071487177940904]
[ GitHub ]

  
# File 'complex.c', line 1238

static VALUE
nucomp_polar(VALUE self)
{
    return rb_assoc_new(f_abs(self), f_arg(self));
}

#quo

[ GitHub ]

#rationalize([eps]) ⇒ Rational

Returns the value as a rational if possible (the imaginary part should be exactly zero).

Complex(1.0/3, 0).rationalize  #=> (1/3)
Complex(1, 0.0).rationalize    # RangeError
Complex(1, 2).rationalize      # RangeError

See to_r.

[ GitHub ]

  
# File 'complex.c', line 1644

static VALUE
nucomp_rationalize(int argc, VALUE *argv, VALUE self)
{
    get_dat1(self);

    rb_check_arity(argc, 0, 1);

    if (!k_exact_zero_p(dat->imag)) {
       rb_raise(rb_eRangeError, "can't convert %"PRIsVALUE" into Rational",
                self);
    }
    return rb_funcallv(dat->real, id_rationalize, argc, argv);
}

#rectArray #rectangularArray
Also known as: #rectangular

Returns an array; [cmp.real, cmp.imag].

Complex(1, 2).rectangular  #=> [1, 2]
[ GitHub ]

  
# File 'complex.c', line 1223

static VALUE
nucomp_rect(VALUE self)
{
    get_dat1(self);
    return rb_assoc_new(dat->real, dat->imag);
}

#rectArray #rectangularArray

Alias for #rect.

#to_cself

Returns self.

Complex(2).to_c      #=> (2+0i)
Complex(-8, 6).to_c  #=> (-8+6i)
[ GitHub ]

  
# File 'complex.c', line 1667

static VALUE
nucomp_to_c(VALUE self)
{
    return self;
}

#to_fFloat

Returns the value as a float if possible (the imaginary part should be exactly zero).

Complex(1, 0).to_f    #=> 1.0
Complex(1, 0.0).to_f  # RangeError
Complex(1, 2).to_f    # RangeError
[ GitHub ]

  
# File 'complex.c', line 1594

static VALUE
nucomp_to_f(VALUE self)
{
    get_dat1(self);

    if (!k_exact_zero_p(dat->imag)) {
	rb_raise(rb_eRangeError, "can't convert %"PRIsVALUE" into Float",
		 self);
    }
    return f_to_f(dat->real);
}

#to_iInteger

Returns the value as an integer if possible (the imaginary part should be exactly zero).

Complex(1, 0).to_i    #=> 1
Complex(1, 0.0).to_i  # RangeError
Complex(1, 2).to_i    # RangeError
[ GitHub ]

  
# File 'complex.c', line 1571

static VALUE
nucomp_to_i(VALUE self)
{
    get_dat1(self);

    if (!k_exact_zero_p(dat->imag)) {
	rb_raise(rb_eRangeError, "can't convert %"PRIsVALUE" into Integer",
		 self);
    }
    return f_to_i(dat->real);
}

#to_rRational

Returns the value as a rational if possible (the imaginary part should be exactly zero).

Complex(1, 0).to_r    #=> (1/1)
Complex(1, 0.0).to_r  # RangeError
Complex(1, 2).to_r    # RangeError

See rationalize.

[ GitHub ]

  
# File 'complex.c', line 1619

static VALUE
nucomp_to_r(VALUE self)
{
    get_dat1(self);

    if (!k_exact_zero_p(dat->imag)) {
	rb_raise(rb_eRangeError, "can't convert %"PRIsVALUE" into Rational",
		 self);
    }
    return f_to_r(dat->real);
}

#to_sString

Returns the value as a string.

Complex(2).to_s                       #=> "2+0i"
Complex('-8/6').to_s                  #=> "-4/3+0i"
Complex('1/2i').to_s                  #=> "0+1/2i"
Complex(0, Float::INFINITY).to_s      #=> "0+Infinity*i"
Complex(Float::NAN, Float::NAN).to_s  #=> "NaN+NaN*i"
[ GitHub ]

  
# File 'complex.c', line 1407

static VALUE
nucomp_to_s(VALUE self)
{
    return f_format(self, rb_String);
}