From 7048fbdf59509e4f52eff56d7c044ed28eb67727 Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Fri, 26 Jul 2024 11:10:39 -0400 Subject: 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. --- test/ruby/test_integer.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'test/ruby/test_integer.rb') diff --git a/test/ruby/test_integer.rb b/test/ruby/test_integer.rb index dd177090ab..978d56ab3e 100644 --- a/test/ruby/test_integer.rb +++ b/test/ruby/test_integer.rb @@ -442,6 +442,10 @@ class TestInteger < Test::Unit::TestCase 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 -- cgit v1.2.3