Skip to content

Commit a1d1669

Browse files
committed
[Bug #21680] Fix(base**power_of_two).digits(base) bug
Fix wrong condition in base multiplying loop.
1 parent ade2b51 commit a1d1669

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

numeric.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5553,7 +5553,7 @@ rb_int_digits_bigbase(VALUE num, VALUE base)
55535553
}
55545554

55555555
bases = rb_ary_new();
5556-
for (VALUE b = base; int_lt(b, num) == Qtrue; b = rb_int_mul(b, b)) {
5556+
for (VALUE b = base; int_le(b, num) == Qtrue; b = rb_int_mul(b, b)) {
55575557
rb_ary_push(bases, b);
55585558
}
55595559
digits = rb_ary_new_from_args(1, num);

test/ruby/test_bignum.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,9 @@ def test_digits
821821
assert_equal([7215, 2413, 6242], T1024P.digits(10_000).first(3))
822822
assert_equal([11], 11.digits(T1024P))
823823
assert_equal([T1024P - 1, 1], (T1024P + T1024P - 1).digits(T1024P))
824+
bug21680 = '[ruby-core:123769] [Bug #21680]'
825+
assert_equal([0] * 64 + [1], (2**512).digits(256), bug21680)
826+
assert_equal([0] * 128 + [1], (123**128).digits(123), bug21680)
824827
end
825828

826829
def test_digits_for_negative_numbers

0 commit comments

Comments
 (0)