From d16b68cb2204eeb5af8bd39149202b630374c67f Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Mon, 26 Jul 2021 11:20:27 -0700 Subject: Use Rational for Float#round with ndigits > 14 ndigits higher than 14 can result in values that are slightly too large due to floating point limitations. Converting to rational for the calculation and then back to float fixes these issues. Fixes [Bug #14635] Fixes [Bug #17183] Co-authored by: Yusuke Endoh --- test/ruby/test_numeric.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'test/ruby') diff --git a/test/ruby/test_numeric.rb b/test/ruby/test_numeric.rb index b5486d387c..0593cb535d 100644 --- a/test/ruby/test_numeric.rb +++ b/test/ruby/test_numeric.rb @@ -200,6 +200,14 @@ class TestNumeric < Test::Unit::TestCase assert_nil(a <=> :foo) end + def test_float_round_ndigits + bug14635 = "[ruby-core:86323]" + f = 0.5 + 31.times do |i| + assert_equal(0.5, f.round(i+1), bug14635 + " (argument: #{i+1})") + end + end + def test_floor_ceil_round_truncate a = Class.new(Numeric) do def to_f; 1.5; end -- cgit v1.2.3