Project

General

Profile

Actions

Bug #22702

open

Integer#divmod` / `Float#divmod` with an infinite divisor returns quotient -1, disagreeing with `#div` and the documentation

Bug #22702: Integer#divmod` / `Float#divmod` with an infinite divisor returns quotient -1, disagreeing with `#div` and the documentation

Added by ko1 (Koichi Sasada) about 5 hours ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:127796]

Description

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.

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.

No data to display

Actions

Also available in: PDF Atom