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 #1

Updated by Eregon (Benoit Daloze) almost 2 years ago

  • Status changed from Open to Closed

Updated by nagachika (Tomoyuki Chikanaga) over 1 year ago

  • Backport changed from 2.7: UNKNOWN, 3.0: REQUIRED, 3.1: REQUIRED, 3.2: REQUIRED to 3.0: REQUIRED, 3.1: REQUIRED, 3.2: DONTNEED

I don't think the fixes for this kind of long standing bugs should not be backported to stable branches.
usa-san, what is your opinion?

Updated by usa (Usaku NAKAMURA) over 1 year ago

  • Backport changed from 3.0: REQUIRED, 3.1: REQUIRED, 3.2: DONTNEED to 3.0: WONTFIX, 3.1: WONTFIX, 3.2: DONTNEED

hmmm, the potential impact it could have on existing scripts, I feel like this should be kept as a feature of sorts in stable branch...
sorry. but thanks for letting me know.

Actions #4

Updated by nagachika (Tomoyuki Chikanaga) over 1 year ago

  • Backport changed from 3.0: WONTFIX, 3.1: WONTFIX, 3.2: DONTNEED to 3.0: WONTFIX, 3.1: WONTFIX, 3.2: WONTFIX
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0