From: "sferik (Erik Berlin) via ruby-core" Date: 2026-11-19T16:25:51+00:00 Subject: [ruby-core:127625] [Ruby Bug#22607] Rational#to_f and Integer#fdiv are not correctly rounded for large operands Issue #22607 has been reported by sferik (Erik Berlin). ---------------------------------------- Bug #22607: Rational#to_f and Integer#fdiv are not correctly rounded for large operands https://bugs.ruby-lang.org/issues/22607 * Author: sferik (Erik Berlin) * Status: Open * ruby -v: ruby 4.1.0dev (2026-11-19T13:09:31Z master 1e71e987c2) +PRISM [arm64-darwin27] * Backport: 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN ---------------------------------------- `Rational#to_f`, and `Integer#fdiv` with an Integer argument, don't always return the Float nearest to the quotient when the numerator or the denominator is too large to be exact as a double. ```ruby r = Rational(742515609648393158335, 10**14) r.to_f #=> 7425156.096483931 742515609648393158335.fdiv(10**14) #=> 7425156.096483931 ``` The nearest Float is 7425156.096483932: ```ruby (r - 7425156.096483932.to_r).abs < (r - 7425156.096483931.to_r).abs #=> true ``` I expect `to_f` to return the nearest Float, the way `String#to_f` does for the same number. This isn't limited to contrived numbers. Converting a nanosecond timestamp to seconds is a common case: ```ruby Rational(1733983396674757576, 10**9).to_f #=> 1733983396.6747577, but 1733983396.6747575 is nearer ``` In random samples of 10,000, the result was one unit in the last place off for about a quarter of nanosecond timestamps between 2001 and 2286 divided by `10**9`, and for about a fifth of 25-digit numerators divided by a power of ten. Ruby 3.2 and 3.3 give the same results as master. The cause is in `rb_int_fdiv_double` in numeric.c. When either operand isn't exact as a double, it divides both by their GCD, then converts each of them to double and divides. The two conversions and the division each round, so the result can end up on the wrong side of the halfway point. Dividing by the GCD doesn't help when the reduced operands are still too large, as they are in both examples above. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/