From: "ko1 (Koichi Sasada) via ruby-core" Date: 2026-12-02T08:30:28+00:00 Subject: [ruby-core:127796] [Ruby Bug#22702] Integer#divmod` / `Float#divmod` with an infinite divisor returns quotient -1, disagreeing with `#div` and the documentation Issue #22702 has been reported by ko1 (Koichi Sasada). ---------------------------------------- Bug #22702: Integer#divmod` / `Float#divmod` with an infinite divisor returns quotient -1, disagreeing with `#div` and the documentation https://bugs.ruby-lang.org/issues/22702 * Author: ko1 (Koichi Sasada) * Status: Open * Backport: 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN ---------------------------------------- When the dividend is finite and the divisor is an infinity of the opposite sign, `divmod` returns `-1` as the quotient, while `div` (and `(x / y).floor`, which the `Float#div mod` documentation gives as the definition of the quotient) returns `0`. ```ruby p (-7).divmod(Float::INFINITY) #=> [-1, Infinity] p (-7).div(Float::INFINITY) #=> 0 p ((-7) / Float::INFINITY).floor #=> 0 p (-7) % Float::INFINITY #=> Infinity p 7.divmod(-Float::INFINITY) #=> [-1, -Infinity] p (-7.0).divmod(Float::INFINITY) #=> [-1, Infinity] p (-7.0).div(Float::INFINITY) #=> 0 # Rational receivers behave differently again: p Rational(-7).divmod(Float::INFINITY) #=> [0, NaN] ``` **Actual:** `(-7).divmod(Float::INFINITY)` is `[-1, Infinity]`, while `(-7).div(Float::INFINITY)` is `0`. **Expected:** `x.divmod(y)[0] == x.div(y)` should hold, as the documentation says (`q = (self/other).floor`, `r = self % other`). Which side should change is a spec question: 1. Keep `%` as it is (ruby/spec requires `4.2 % -Float::INFINITY == -Float::INFINITY`) and make the `divmod` quotient `0`, giving `[0, Infinity]`. `q*y + r == x` cannot hold for infinite `y` anyway. 2. Make `div` return `-1` too (as Python does: `-7 // float('inf') == -1.0`), so it agrees with `divmod` and `%`, and update the documentation. Also, a `Rational` receiver returns `NaN` for both `%` and the `divmod` remainder, unlike `Integer` and `Float`. **Cause (presumed):** `flodivmod()` in `numeric.c` sets `mod = x` and `div = 0` when `isinf(y) && !isinf(x)`, then the sign adjustment `if (y*mod < 0) { mod += y; div -= 1.0; }` turns them into `�Infinity` and `-1`. `Integer#div(Float)` (`fix_divide` -> `flo_floor`) and `Float#div` take `floor(x / y)` and never apply that adjustment. The special case comes from [Bug #6044] (4c2e2d8bde). **Version:** `ruby 4.1.0dev (2026-12-01T20:47:52Z master 0d8198a37c) +PRISM [x86_64-linux]` NOTE: This report is generated by Claude code and I have no idea / opinion. -- 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/