From 9f2378959e5c5b5c39c9993f1a84e5304ff113d6 Mon Sep 17 00:00:00 2001 From: Kenta Murata <3959+mrkn@users.noreply.github.com> Date: Mon, 26 Dec 2022 21:02:47 +0900 Subject: numeric.c: Fix round_half_even for specific values (#7023) Handle the integert and the float parts separately in round_half_even to prevent error occursions in floating point calculation. --- test/ruby/test_float.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'test/ruby') diff --git a/test/ruby/test_float.rb b/test/ruby/test_float.rb index fdc5d28ed7..35cde1e951 100644 --- a/test/ruby/test_float.rb +++ b/test/ruby/test_float.rb @@ -486,6 +486,17 @@ class TestFloat < Test::Unit::TestCase assert_equal(-1.26, -1.255.round(2)) end + def test_round_half_even_with_precision + assert_equal(767573.18759, 767573.1875850001.round(5, half: :even)) + assert_equal(767573.18758, 767573.187585.round(5, half: :even)) + assert_equal(767573.18758, 767573.1875849998.round(5, half: :even)) + assert_equal(767573.18758, 767573.187575.round(5, half: :even)) + assert_equal(-767573.18759, -767573.1875850001.round(5, half: :even)) + assert_equal(-767573.18758, -767573.187585.round(5, half: :even)) + assert_equal(-767573.18758, -767573.1875849998.round(5, half: :even)) + assert_equal(-767573.18758, -767573.187575.round(5, half: :even)) + end + def test_floor_with_precision assert_equal(+0.0, +0.001.floor(1)) assert_equal(-0.1, -0.001.floor(1)) -- cgit v1.2.3