instance method
Complex#+
(::BigDecimal) -> ::Complex
(::Numeric) -> ::Complex
self + other → numeric
complex.c
View on GitHub ↗
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, '+');
}
Returns the sum of self and other:
Example
Complex(1, 2) + 0 # => (1+2i) Complex(1, 2) + 1 # => (2+2i) Complex(1, 2) + -1 # => (0+2i) Complex(1, 2) + 1.0 # => (2.0+2i) Complex(1, 2) + Complex(2, 1) # => (3+3i) Complex(1, 2) + Complex(2.0, 1.0) # => (3.0+3.0i) Complex(1, 2) + Rational(1, 1) # => ((2/1)+2i) Complex(1, 2) + Rational(1, 2) # => ((3/2)+2i)
For a computation involving Floats, the result may be inexact (see Float#+):
Example
Complex(1, 2) + 3.14 # => (4.140000000000001+2i)