End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Rational#fdiv
rat.fdiv(numeric) → float
rational.c
View on GitHub ↗
static VALUE
nurat_fdiv(VALUE self, VALUE other)
{
VALUE div;
if (f_zero_p(other))
return rb_rational_div(self, rb_float_new(0.0));
if (FIXNUM_P(other) && other == LONG2FIX(1))
return nurat_to_f(self);
div = rb_rational_div(self, other);
if (RB_TYPE_P(div, T_RATIONAL))
return nurat_to_f(div);
if (RB_FLOAT_TYPE_P(div))
return div;
return rb_funcall(div, idTo_f, 0);
}
Performs division and returns the value as a Float.
Example
Rational(2, 3).fdiv(1) #=> 0.6666666666666666 Rational(2, 3).fdiv(0.5) #=> 1.3333333333333333 Rational(2).fdiv(3) #=> 0.6666666666666666