Project

General

Profile

Actions

Bug #19318

closed

Float#round rounds incorrectly for some cases

Added by Eregon (Benoit Daloze) almost 2 years ago. Updated over 1 year ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:111698]

Description

This was discovered by @aardvark179 (Duncan MacGregor).

The following spec in spec/ruby/core/float/round_spec.rb fails on CRuby:

  ruby_bug "", ""..."3.3" do
    # These numbers are neighbouring floating point numbers round a
    # precise value. They test that the rounding modes work correctly
    # round that value and precision is not lost which might cause
    # incorrect results.
    it "does not lose precision during the rounding process" do
      767573.1875850001.round(5, half: nil).should eql(767573.18759)
      767573.1875850001.round(5, half: :up).should eql(767573.18759)
      767573.1875850001.round(5, half: :down).should eql(767573.18759)
      767573.1875850001.round(5, half: :even).should eql(767573.18759)
      -767573.1875850001.round(5, half: nil).should eql(-767573.18759)
      -767573.1875850001.round(5, half: :up).should eql(-767573.18759)
      -767573.1875850001.round(5, half: :down).should eql(-767573.18759)
      -767573.1875850001.round(5, half: :even).should eql(-767573.18759)
      767573.187585.round(5, half: nil).should eql(767573.18759)
      767573.187585.round(5, half: :up).should eql(767573.18759)
      767573.187585.round(5, half: :down).should eql(767573.18758)
      767573.187585.round(5, half: :even).should eql(767573.18758)
      -767573.187585.round(5, half: nil).should eql(-767573.18759)
      -767573.187585.round(5, half: :up).should eql(-767573.18759)
      -767573.187585.round(5, half: :down).should eql(-767573.18758)
      -767573.187585.round(5, half: :even).should eql(-767573.18758)
      767573.1875849998.round(5, half: nil).should eql(767573.18758)
      767573.1875849998.round(5, half: :up).should eql(767573.18758)
      767573.1875849998.round(5, half: :down).should eql(767573.18758)
      767573.1875849998.round(5, half: :even).should eql(767573.18758)
      -767573.1875849998.round(5, half: nil).should eql(-767573.18758)
      -767573.1875849998.round(5, half: :up).should eql(-767573.18758)
      -767573.1875849998.round(5, half: :down).should eql(-767573.18758)
      -767573.1875849998.round(5, half: :even).should eql(-767573.18758)
    end
  end

Yet this test to the best of our knowledge is correct.

This was fixed on master by @mrkn (Kenta Murata) in https://github.com/ruby/ruby/pull/7023 (thanks!).

The question is should we backport this? I think yes.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0