8000 [Backport 3.3] [Bug #20654] Fix floor and ceil when ndigits is large by peterzhu2118 · Pull Request #11277 · ruby/ruby · GitHub
[go: up one dir, main page]

Skip to content

[Backport 3.3] [Bug #20654] Fix floor and ceil when ndigits is large #11277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix floor when ndigits is large
[Bug #20654]

This commit fixes Integer#floor and Float#floor when the number is
negative and ndigits is large such that 10**ndigits is a bignum.

Previously, it would return 0 in such cases. However, this would cause
unexpected behaviour such as:

    puts -1.floor(-5) # => -100000
    puts -1.floor(-10) # => -10000000000
    puts -1.floor(-20) # => 0

This commit changes the last result so that it will return
-100000000000000000000.
  • Loading branch information
peterzhu2118 committed Jul 29, 2024
commit 94d3bd962f802f9eb78aa346eafce6a843ff8b96
16 changes: 7 additions & 9 deletions numeric.c
Original file line number Diff line number Diff line change
Expand Up @@ -2373,11 +2373,7 @@ rb_int_round(VALUE num, int ndigits, enum ruby_num_rounding_mode mode)
static VALUE
rb_int_floor(VALUE num, int ndigits)
{
VALUE f;

if (int_round_zero_p(num, ndigits))
return INT2FIX(0);
f = int_pow(10, -ndigits);
VALUE f = int_pow(10, -ndigits);
if (FIXNUM_P(num) && FIXNUM_P(f)) {
SIGNED_VALUE x = FIX2LONG(num), y = FIX2LONG(f);
int neg = x < 0;
Expand All @@ -2386,11 +2382,13 @@ rb_int_floor(VALUE num, int ndigits)
if (neg) x = -x;
return LONG2NUM(x);
}
if (RB_FLOAT_TYPE_P(f)) {
/* then int_pow overflow */
return INT2FIX(0);
else {
bool neg = int_neg_p(num);
if (neg) num = rb_int_minus(rb_int_plus(rb_int_uminus(num), f), INT2FIX( 8000 1));
num = rb_int_mul(rb_int_div(num, f), f);
if (neg) num = rb_int_uminus(num);
return num;
}
return rb_int_minus(num, rb_int_modulo(num, f));
}

static VALUE
Expand Down
4 changes: 4 additions & 0 deletions test/ruby/test_float.rb
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,10 @@ def test_floor_with_precision
assert_raise(TypeError) {1.0.floor(nil)}
def (prec = Object.new).to_int; 2; end
assert_equal(0.99, 0.998.floor(prec))

assert_equal(-10000000000, -1.0.floor(-10), "[Bug #20654]")
assert_equal(-100000000000000000000, -1.0.floor(-20), "[Bug #20654]")
assert_equal(-100000000000000000000000000000000000000000000000000, -1.0.floor(-50), "[Bug #20654]")
end

def test_ceil_with_precision
Expand Down
4 changes: 4 additions & 0 deletions test/ruby/test_integer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,10 @@ def test_floor

assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1111, 1111_1111_1111_1111_1111_1111_1111_1111.floor(1))
assert_int_equal(10**400, (10**400).floor(1))

assert_int_equal(-10000000000, -1.floor(-10), "[Bug #20654]")
assert_int_equal(-100000000000000000000, -1.floor(-20), "[Bug #20654]")
assert_int_equal(-100000000000000000000000000000000000000000000000000, -1.floor(-50), "[Bug #20654]")
end

def test_ceil
Expand Down
0