Class: Complex

Inherits:
Numeric show all
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.

In ruby, you can create complex object with Complex, Complex::rect, Complex::polar or to_c method.

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

I =
f_complex_new_bang2(rb_cComplex, ZERO, ONE)

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Numeric

#%, #+@, #<=>, #ceil, #div, #divmod, #floor, #i, #initialize_copy, #integer?, #modulo, #nonzero?, #remainder, #round, #singleton_method_added, #step, #to_c, #to_int, #truncate, #zero?

Methods included from Comparable

#<, #<=, #>, #>=, #between?

Class Method Details

.polar(abs[, arg]) ⇒ Object

Returns a complex object which denotes the given polar form.



# File 'complex.c'

/*
 * call-seq:
 *    Complex.polar(abs[, arg])  ->  complex
 *
 * Returns a complex object which denotes the given polar form.
 */
static VALUE
nucomp_s_polar(int argc, VALUE *argv, VALUE klass)
{
    VALUE abs, arg;

    switch (rb_scan_args(argc, argv, "11", &abs, &arg)) {
      case 1:
    nucomp_real_check(abs);
    arg = ZERO;
    break;
      default:
    nucomp_real_check(abs);
    nucomp_real_check(arg);
    break;
    }
    return f_complex_polar(klass, abs, arg);
}

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

Returns a complex object which denotes the given rectangular form.



# File 'complex.c'

/*
 * call-seq:
 *    Complex.rect(real[, imag])         ->  complex
 *    Complex.rectangular(real[, imag])  ->  complex
 *
 * Returns a complex object which denotes the given rectangular form.
 */
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]) ⇒ Object .rectangular(real[, imag]) ⇒ Object

Returns a complex object which denotes the given rectangular form.



# File 'complex.c'

/*
 * call-seq:
 *    Complex.rect(real[, imag])         ->  complex
 *    Complex.rectangular(real[, imag])  ->  complex
 *
 * Returns a complex object which denotes the given rectangular form.
 */
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);
}

Instance Method Details

#*(numeric) ⇒ Object

Performs multiplication.



# File 'complex.c'

/*
 * call-seq:
 *    cmp * numeric  ->  complex
 *
 * Performs multiplication.
 */
static VALUE
nucomp_mul(VALUE self, VALUE other)
{
    if (k_complex_p(other)) {
    VALUE real, imag;

    get_dat2(self, other);

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

    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) ⇒ Object

Performs exponentiation.

For example:

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


# File 'complex.c'

/*
 * call-seq:
 *    cmp ** numeric  ->  complex
 *
 * Performs exponentiation.
 *
 * For example:
 *
 *     Complex('i') ** 2             #=> (-1+0i)
 *     Complex(-8) ** Rational(1,3)  #=> (1.0000000000000002+1.7320508075688772i)
 */
static VALUE
nucomp_expt(VALUE self, VALUE other)
{
    if (k_exact_zero_p(other))
    return f_complex_new_bang1(CLASS_OF(self), ONE);

    if (k_rational_p(other) && f_one_p(f_denominator(other)))
    other = f_numerator(other); /* c14n */

    if (k_complex_p(other)) {
    get_dat1(other);

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

    if (k_complex_p(other)) {
    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 (k_fixnum_p(other)) {
    if (f_gt_p(other, ZERO)) {
        VALUE x, z;
        long n;

        x = self;
        z = x;
        n = FIX2LONG(other) - 1;

        while (n) {
        long q, r;

        while (1) {
            get_dat1(x);

            q = n / 2;
            r = n % 2;

            if (r)
            break;

            x = f_complex_new2(CLASS_OF(self),
                       f_sub(f_mul(dat->real, dat->real),
                         f_mul(dat->imag, dat->imag)),
                       f_mul(f_mul(TWO, dat->real), dat->imag));
            n = q;
        }
        z = f_mul(z, x);
        n--;
        }
        return z;
    }
    return f_expt(f_reciprocal(self), f_negate(other));
    }
    if (k_numeric_p(other) && f_real_p(other)) {
    VALUE r, theta;

    if (k_bignum_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) ⇒ Object

Performs addition.



# File 'complex.c'

/*
 * call-seq:
 *    cmp + numeric  ->  complex
 *
 * Performs addition.
 */
static VALUE
nucomp_add(VALUE self, VALUE other)
{
    return f_addsub(self, other, f_add, '+');
}

#-(numeric) ⇒ Object

Performs subtraction.



# File 'complex.c'

/*
 * call-seq:
 *    cmp - numeric  ->  complex
 *
 * Performs subtraction.
 */
static VALUE
nucomp_sub(VALUE self, VALUE other)
{
    return f_addsub(self, other, f_sub, '-');
}

#-Object

Returns negation of the value.



# File 'complex.c'

/*
 * call-seq:
 *    -cmp  ->  complex
 *
 * Returns negation of the value.
 */
static VALUE
nucomp_negate(VALUE self)
{
  get_dat1(self);
  return f_complex_new2(CLASS_OF(self),
            f_negate(dat->real), f_negate(dat->imag));
}

#/(numeric) ⇒ Object #quo(numeric) ⇒ Object

Performs division.

For example:

Complex(10.0) / 3  #=> (3.3333333333333335+(0/1)*i)
Complex(10)   / 3  #=> ((10/3)+(0/1)*i)  # not (3+0i)


# File 'complex.c'

/*
 * call-seq:
 *    cmp / numeric     ->  complex
 *    cmp.quo(numeric)  ->  complex
 *
 * Performs division.
 *
 * For example:
 *
 *     Complex(10.0) / 3  #=> (3.3333333333333335+(0/1)*i)
 *     Complex(10)   / 3  #=> ((10/3)+(0/1)*i)  # not (3+0i)
 */
static VALUE
nucomp_div(VALUE self, VALUE other)
{
    return f_divide(self, other, f_quo, id_quo);
}

#==(object) ⇒ Boolean

Returns true if cmp equals object numerically.

Returns:

  • (Boolean)


# File 'complex.c'

/*
 * call-seq:
 *    cmp == object  ->  true or false
 *
 * Returns true if cmp equals object numerically.
 */
static VALUE
nucomp_eqeq_p(VALUE self, VALUE other)
{
    if (k_complex_p(other)) {
    get_dat2(self, other);

    return f_boolcast(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 f_boolcast(f_eqeq_p(dat->real, other) && f_zero_p(dat->imag));
    }
    return f_eqeq_p(other, self);
}

#absObject #magnitudeObject

Returns the absolute part of its polar form.



# File 'complex.c'

/*
 * call-seq:
 *    cmp.abs        ->  real
 *    cmp.magnitude  ->  real
 *
 * Returns the absolute part of its polar form.
 */
static VALUE
nucomp_abs(VALUE self)
{
    get_dat1(self);

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

#abs2Object

Returns square of the absolute value.



# File 'complex.c'

/*
 * call-seq:
 *    cmp.abs2  ->  real
 *
 * Returns square of the absolute value.
 */
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

Returns the angle part of its polar form.

Overloads:



# File 'complex.c'

/*
 * call-seq:
 *    cmp.arg    ->  float
 *    cmp.angle  ->  float
 *    cmp.phase  ->  float
 *
 * Returns the angle part of its polar form.
 */
static VALUE
nucomp_arg(VALUE self)
{
    get_dat1(self);
    return m_atan2_bang(dat->imag, dat->real);
}

#argFloat #angleFloat #phaseFloat

Returns the angle part of its polar form.

Overloads:



# File 'complex.c'

/*
 * call-seq:
 *    cmp.arg    ->  float
 *    cmp.angle  ->  float
 *    cmp.phase  ->  float
 *
 * Returns the angle part of its polar form.
 */
static VALUE
nucomp_arg(VALUE self)
{
    get_dat1(self);
    return m_atan2_bang(dat->imag, dat->real);
}

#coerceObject

:nodoc:



# File 'complex.c'

/* :nodoc: */
static VALUE
nucomp_coerce(VALUE self, VALUE other)
{
    if (k_numeric_p(other) && f_real_p(other))
    return rb_assoc_new(f_complex_new_bang1(CLASS_OF(self), other), self);
    if (TYPE(other) == T_COMPLEX)
    return rb_assoc_new(other, self);

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

#complex?Object

:nodoc:



# File 'complex.c'

/* :nodoc: */
static VALUE
nucomp_true(VALUE self)
{
    return Qtrue;
}

#conjObject #conjugateObject

Returns the complex conjugate.



# File 'complex.c'

/*
 * call-seq:
 *    cmp.conj       ->  complex
 *    cmp.conjugate  ->  complex
 *
 * Returns the complex conjugate.
 */
static VALUE
nucomp_conj(VALUE self)
{
    get_dat1(self);
    return f_complex_new2(CLASS_OF(self), dat->real, f_negate(dat->imag));
}

#conjObject #conjugateObject

Returns the complex conjugate.



# File 'complex.c'

/*
 * call-seq:
 *    cmp.conj       ->  complex
 *    cmp.conjugate  ->  complex
 *
 * Returns the complex conjugate.
 */
static VALUE
nucomp_conj(VALUE self)
{
    get_dat1(self);
    return f_complex_new2(CLASS_OF(self), dat->real, f_negate(dat->imag));
}

#denominatorInteger

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

See numerator.

Returns:



# File 'complex.c'

/*
 * call-seq:
 *    cmp.denominator  ->  integer
 *
 * Returns the denominator (lcm of both denominator, real and imag).
 *
 * See numerator.
 */
static VALUE
nucomp_denominator(VALUE self)
{
    get_dat1(self);
    return rb_lcm(f_denominator(dat->real), f_denominator(dat->imag));
}

#eql?Object

:nodoc:



# File 'complex.c'

/* :nodoc: */
static VALUE
nucomp_eql_p(VALUE self, VALUE other)
{
    if (k_complex_p(other)) {
    get_dat2(self, other);

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

    }
    return Qfalse;
}

#exact?Object

:nodoc:



# File 'complex.c'

/* :nodoc: */
static VALUE
nucomp_exact_p(VALUE self)
{
    get_dat1(self);
    return f_boolcast(k_exact_p(dat->real) && k_exact_p(dat->imag));
}

#fdiv(numeric) ⇒ Object

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

For example:

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


# File 'complex.c'

/*
 * call-seq:
 *    cmp.fdiv(numeric)  ->  complex
 *
 * Performs division as each part is a float, never returns a float.
 *
 * For example:
 *
 *     Complex(11,22).fdiv(3)  #=> (3.6666666666666665+7.333333333333333i)
 */
static VALUE
nucomp_fdiv(VALUE self, VALUE other)
{
    return f_divide(self, other, f_fdiv, id_fdiv);
}

#hashObject

:nodoc:



# File 'complex.c'

/* :nodoc: */
static VALUE
nucomp_hash(VALUE self)
{
    st_index_t v, h[2];
    VALUE n;

    get_dat1(self);
    n = rb_hash(dat->real);
    h[0] = NUM2LONG(n);
    n = rb_hash(dat->imag);
    h[1] = NUM2LONG(n);
    v = rb_memhash(h, sizeof(h));
    return LONG2FIX(v);
}

#imagObject #imaginaryObject

Returns the imaginary part.



# File 'complex.c'

/*
 * call-seq:
 *    cmp.imag       ->  real
 *    cmp.imaginary  ->  real
 *
 * Returns the imaginary part.
 */
static VALUE
nucomp_imag(VALUE self)
{
    get_dat1(self);
    return dat->imag;
}

#imagObject #imaginaryObject

Returns the imaginary part.



# File 'complex.c'

/*
 * call-seq:
 *    cmp.imag       ->  real
 *    cmp.imaginary  ->  real
 *
 * Returns the imaginary part.
 */
static VALUE
nucomp_imag(VALUE self)
{
    get_dat1(self);
    return dat->imag;
}

#inexact?Object

:nodoc:



# File 'complex.c'

/* :nodoc: */
static VALUE
nucomp_inexact_p(VALUE self)
{
    return f_boolcast(!nucomp_exact_p(self));
}

#inspectString

Returns the value as a string for inspection.

Returns:



# File 'complex.c'

/*
 * call-seq:
 *    cmp.inspect  ->  string
 *
 * Returns the value as a string for inspection.
 */
static VALUE
nucomp_inspect(VALUE self)
{
    VALUE s;

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

    return s;
}

#absObject #magnitudeObject

Returns the absolute part of its polar form.



# File 'complex.c'

/*
 * call-seq:
 *    cmp.abs        ->  real
 *    cmp.magnitude  ->  real
 *
 * Returns the absolute part of its polar form.
 */
static VALUE
nucomp_abs(VALUE self)
{
    get_dat1(self);

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

#marshal_dumpObject

:nodoc:



# File 'complex.c'

/* :nodoc: */
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;
}

#marshal_loadObject

:nodoc:



# File 'complex.c'

/* :nodoc: */
static VALUE
nucomp_marshal_load(VALUE self, VALUE a)
{
    get_dat1(self);
    Check_Type(a, T_ARRAY);
    dat->real = RARRAY_PTR(a)[0];
    dat->imag = RARRAY_PTR(a)[1];
    rb_copy_generic_ivar(self, a);
    return self;
}

#numeratorNumeric

Returns the numerator.

For example:

    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.

Returns:



# File 'complex.c'

/*
 * call-seq:
 *    cmp.numerator  ->  numeric
 *
 * Returns the numerator.
 *
 * For example:
 *
 *        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.
 */
static VALUE
nucomp_numerator(VALUE self)
{
    VALUE cd;

    get_dat1(self);

    cd = f_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

Returns the angle part of its polar form.

Overloads:



# File 'complex.c'

/*
 * call-seq:
 *    cmp.arg    ->  float
 *    cmp.angle  ->  float
 *    cmp.phase  ->  float
 *
 * Returns the angle part of its polar form.
 */
static VALUE
nucomp_arg(VALUE self)
{
    get_dat1(self);
    return m_atan2_bang(dat->imag, dat->real);
}

#polarArray

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

Returns:



# File 'complex.c'

/*
 * call-seq:
 *    cmp.polar  ->  array
 *
 * Returns an array; [cmp.abs, cmp.arg].
 */
static VALUE
nucomp_polar(VALUE self)
{
    return rb_assoc_new(f_abs(self), f_arg(self));
}

#quoObject

#rationalize([eps]) ⇒ Object

Returns the value as a rational if possible. An optional argument eps is always ignored.



# File 'complex.c'

/*
 * call-seq:
 *    cmp.rationalize([eps])  ->  rational
 *
 * Returns the value as a rational if possible.  An optional argument
 * eps is always ignored.
 */
static VALUE
nucomp_rationalize(int argc, VALUE *argv, VALUE self)
{
    rb_scan_args(argc, argv, "01", NULL);
    return nucomp_to_r(self);
}

#realObject

Returns the real part.



# File 'complex.c'

/*
 * call-seq:
 *    cmp.real  ->  real
 *
 * Returns the real part.
 */
static VALUE
nucomp_real(VALUE self)
{
    get_dat1(self);
    return dat->real;
}

#real?false

Returns false.

Returns:

  • (false)


# File 'complex.c'

/*
 * call-seq:
 *    cmp.real?  ->  false
 *
 * Returns false.
 */
static VALUE
nucomp_false(VALUE self)
{
    return Qfalse;
}

#rectArray #rectangularArray

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

Overloads:



# File 'complex.c'

/*
 * call-seq:
 *    cmp.rect         ->  array
 *    cmp.rectangular  ->  array
 *
 * Returns an array; [cmp.real, cmp.imag].
 */
static VALUE
nucomp_rect(VALUE self)
{
    get_dat1(self);
    return rb_assoc_new(dat->real, dat->imag);
}

#rectArray #rectangularArray

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

Overloads:



# File 'complex.c'

/*
 * call-seq:
 *    cmp.rect         ->  array
 *    cmp.rectangular  ->  array
 *
 * Returns an array; [cmp.real, cmp.imag].
 */
static VALUE
nucomp_rect(VALUE self)
{
    get_dat1(self);
    return rb_assoc_new(dat->real, dat->imag);
}

#to_fFloat

Returns the value as a float if possible.

Returns:



# File 'complex.c'

/*
 * call-seq:
 *    cmp.to_f  ->  float
 *
 * Returns the value as a float if possible.
 */
static VALUE
nucomp_to_f(VALUE self)
{
    get_dat1(self);

    if (k_inexact_p(dat->imag) || f_nonzero_p(dat->imag)) {
    VALUE s = f_to_s(self);
    rb_raise(rb_eRangeError, "can't convert %s into Float",
         StringValuePtr(s));
    }
    return f_to_f(dat->real);
}

#to_iInteger

Returns the value as an integer if possible.

Returns:



# File 'complex.c'

/*
 * call-seq:
 *    cmp.to_i  ->  integer
 *
 * Returns the value as an integer if possible.
 */
static VALUE
nucomp_to_i(VALUE self)
{
    get_dat1(self);

    if (k_inexact_p(dat->imag) || f_nonzero_p(dat->imag)) {
    VALUE s = f_to_s(self);
    rb_raise(rb_eRangeError, "can't convert %s into Integer",
         StringValuePtr(s));
    }
    return f_to_i(dat->real);
}

#to_rObject

Returns the value as a rational if possible.



# File 'complex.c'

/*
 * call-seq:
 *    cmp.to_r  ->  rational
 *
 * Returns the value as a rational if possible.
 */
static VALUE
nucomp_to_r(VALUE self)
{
    get_dat1(self);

    if (k_inexact_p(dat->imag) || f_nonzero_p(dat->imag)) {
    VALUE s = f_to_s(self);
    rb_raise(rb_eRangeError, "can't convert %s into Rational",
         StringValuePtr(s));
    }
    return f_to_r(dat->real);
}

#to_sString

Returns the value as a string.

Returns:



# File 'complex.c'

/*
 * call-seq:
 *    cmp.to_s  ->  string
 *
 * Returns the value as a string.
 */
static VALUE
nucomp_to_s(VALUE self)
{
    return f_format(self, f_to_s);
}

#conjObject #conjugateObject

Returns the complex conjugate.



# File 'complex.c'

/*
 * call-seq:
 *    cmp.conj       ->  complex
 *    cmp.conjugate  ->  complex
 *
 * Returns the complex conjugate.
 */
static VALUE
nucomp_conj(VALUE self)
{
    get_dat1(self);
    return f_complex_new2(CLASS_OF(self), dat->real, f_negate(dat->imag));
}