Bug #13078
closed
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()
?
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)
- Status changed from Open to Closed
Applied in changeset r57227.
numeric.c: reduce fdiv
- 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.
- Backport changed from 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: DONE to 2.2: UNKNOWN, 2.3: REQUIRED, 2.4: DONE
- 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
Like0
Like0Like0Like0Like0Like0Like0Like0