Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
Complex#abs
() -> ::Numeric
abs → float
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 value (magnitude) for self; see polar coordinates:
Example
Complex.polar(-1, 0).abs # => 1.0
If self was created with rectangular coordinates, the returned value is computed, and may be inexact:
Example
Complex.rectangular(1, 1).abs # => 1.4142135623730951 # The square root of 2.