Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Float#+
(::BigDecimal) -> ::BigDecimal
(::Complex) -> ::Complex
(::Numeric) -> ::Float
self + other → float or complex
numeric.c
View on GitHub ↗
VALUE
rb_float_plus(VALUE x, VALUE y)
{
if (FIXNUM_P(y)) {
return DBL2NUM(RFLOAT_VALUE(x) + (double)FIX2LONG(y));
}
else if (RB_BIGNUM_TYPE_P(y)) {
return DBL2NUM(RFLOAT_VALUE(x) + rb_big2dbl(y));
}
else if (RB_FLOAT_TYPE_P(y)) {
return DBL2NUM(RFLOAT_VALUE(x) + RFLOAT_VALUE(y));
}
else {
return rb_num_coerce_bin(x, y, '+');
}
}
Returns the sum of self and other; the result may be inexact (see Float):
Example
3.14 + 0 # => 3.14 3.14 + 1 # => 4.140000000000001 -3.14 + 0 # => -3.14 -3.14 + 1 # => -2.14 3.14 + -3.14 # => 0.0 -3.14 + -3.14 # => -6.28 3.14 + Complex(1, 0) # => (4.140000000000001+0i) 3.14 + Rational(1, 1) # => 4.140000000000001