End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Float#modulo
modulo(p1)
numeric.c
View on GitHub ↗
static VALUE
flo_mod(VALUE x, VALUE y)
{
double fy;
if (FIXNUM_P(y)) {
fy = (double)FIX2LONG(y);
}
else if (RB_BIGNUM_TYPE_P(y)) {
fy = rb_big2dbl(y);
}
else if (RB_FLOAT_TYPE_P(y)) {
fy = RFLOAT_VALUE(y);
}
else {
return rb_num_coerce_bin(x, y, '%');
}
return DBL2NUM(ruby_float_mod(RFLOAT_VALUE(x), fy));
}
Alias of #%
Returns self modulo other as a float.
For float f and real number r, these expressions are equivalent:
Example
f % r f-r*(f/r).floor f.divmod(r)[1]
See Numeric#divmod.
Examples:
Example
10.0 % 2 # => 0.0 10.0 % 3 # => 1.0 10.0 % 4 # => 2.0 10.0 % -2 # => 0.0 10.0 % -3 # => -2.0 10.0 % -4 # => -2.0 10.0 % 4.0 # => 2.0 10.0 % Rational(4, 1) # => 2.0
Float#modulo is an alias for Float#%.