Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

Complex#to_f

() -> ::Float
to_f → float

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_)).

See also #to_c #to_i #rectangular #to_json also in 4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0