summaryrefslogtreecommitdiff
path: root/test/ruby
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2021-07-26 11:20:27 -0700
committerJeremy Evans <code@jeremyevans.net>2021-08-06 15:03:51 -0700
commitd16b68cb2204eeb5af8bd39149202b630374c67f (patch)
treed14b41ad62a2ef2a5b2fc40f3917ffebf83d2d80 /test/ruby
parent0d3520b063b304708699d3b7ea82b0a5b0279555 (diff)
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 <mame@ruby-lang.org>
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/4682
Diffstat (limited to 'test/ruby')
-rw-r--r--test/ruby/test_numeric.rb8
1 files changed, 8 insertions, 0 deletions
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