Project

General

Profile

Bug #13078

with Integer a and b, a.fdiv(b) sometimes inaccurate than Rational(a, b).to_f

Added by metanest (Makoto Kishimoto) almost 3 years ago. Updated over 2 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:78855]

Description

For example,

a = 3
b = 0x20_0000_0000_0001
p a.fdiv(b)
p Rational(a, b).to_f

#=>
3.3306690738754696e-16
3.330669073875469e-16

I'm hacking to solve this problem, see diff view.
https://github.com/ruby/ruby/compare/trunk...metanest:fdiv_spike

Associated revisions

Revision 1778ed59
Added by nobu (Nobuyoshi Nakada) almost 3 years ago

numeric.c: reduce fdiv

  • numeric.c (rb_int_fdiv_double): reduce first for more precise result. [ruby-core:78886] [Bug #13078]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57227 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 57227
Added by nobu (Nobuyoshi Nakada) almost 3 years ago

numeric.c: reduce fdiv

  • numeric.c (rb_int_fdiv_double): reduce first for more precise result. [ruby-core:78886] [Bug #13078]

Revision 57227
Added by nobu (Nobuyoshi Nakada) almost 3 years ago

numeric.c: reduce fdiv

  • numeric.c (rb_int_fdiv_double): reduce first for more precise result. [ruby-core:78886] [Bug #13078]

Revision 57227
Added by nobu (Nobuyoshi Nakada) almost 3 years ago

numeric.c: reduce fdiv

  • numeric.c (rb_int_fdiv_double): reduce first for more precise result. [ruby-core:78886] [Bug #13078]

Revision ea52cac1
Added by naruse (Yui NARUSE) over 2 years ago

merge revision(s) 57227: [Backport #13078]

    numeric.c: reduce fdiv

    * numeric.c (rb_int_fdiv_double): reduce first for more precise
      result.  [ruby-core:78886] [Bug #13078]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_4@57840 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 57840
Added by naruse (Yui NARUSE) over 2 years ago

merge revision(s) 57227: [Backport #13078]

numeric.c: reduce fdiv

* numeric.c (rb_int_fdiv_double): reduce first for more precise
  result.  [ruby-core:78886] [Bug #13078]

History

Updated by wanabe (_ wanabe) almost 3 years ago

The accuracy of Rational(a, b).to_f seems to be due to reduction of fraction.
How about using Rational object, or its internal function f_gcd() and f_idiv()?

Updated by nobu (Nobuyoshi Nakada) almost 3 years ago

I thought the reduction would be slower, but actually not significant.

$ ruby -rbenchmark -e 'a = 3; b = -0x20_0000_0000_0001; n=10_000_000; Benchmark.bm(13) {|x|x.report("Integer#fdiv"){n.times{a.fdiv(b)}}; x.report("Rational#to_f"){n.times{Rational(a, b).to_f}}}'
                    user     system      total        real
Integer#fdiv    3.420000   0.000000   3.420000 (  3.446264)
Rational#to_f   3.450000   0.010000   3.460000 (  3.465592)
#3

Updated by nobu (Nobuyoshi Nakada) almost 3 years ago

  • Status changed from Open to Closed

Applied in changeset r57227.


numeric.c: reduce fdiv

  • numeric.c (rb_int_fdiv_double): reduce first for more precise result. [ruby-core:78886] [Bug #13078]

Updated by naruse (Yui NARUSE) over 2 years ago

  • Backport changed from 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN to 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: DONE

ruby_2_4 r57840 merged revision(s) 57227.

Updated by nagachika (Tomoyuki Chikanaga) over 2 years ago

  • Backport changed from 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: DONE to 2.2: UNKNOWN, 2.3: REQUIRED, 2.4: DONE
#7

Updated by usa (Usaku NAKAMURA) over 2 years ago

  • Backport changed from 2.2: UNKNOWN, 2.3: REQUIRED, 2.4: DONE to 2.2: REQUIRED, 2.3: REQUIRED, 2.4: DONE

Also available in: Atom PDF