Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Complex#to_f
() -> ::Float
to_f → float
complex.c
View on GitHub ↗
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);
}
Returns the value of self.real as a Float, if possible:
Example
Complex.rect(1, 0).to_f # => 1.0 Complex.rect(1, Rational(0, 1)).to_f # => 1.0
Raises RangeError if self.imag is not exactly zero (either Integer(0) or Rational(0, n)).