End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Complex#abs
cmp.abs → real
complex.c
View on GitHub ↗
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);
}
Returns the absolute part of its polar form.
Example
Complex(-1).abs #=> 1 Complex(3.0, -4.0).abs #=> 5.0