Skip to content

Integer/Float/Numeric: advance MRI 4.0.6 conformance (floored bignum div/mod, Float#% Inf/NaN, coerce, fdiv, div, round precision, <=>, aliases) - #549

Merged
tannevaled merged 10 commits into
mainfrom
feat/agent-intfloat
Sep 3, 2026
Merged

tannevaled merged 10 commits into
mainfrom
feat/agent-intfloat

Conversation

@tannevaled

@tannevaled tannevaled commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Advances MRI 4.0.6 conformance across core/integer, core/float and core/numeric. Work is confined to internal/vm/numeric_edges.go and internal/vm/integer_bignum.go plus two surgical numeric-operator fixes in internal/vm/arith.go, with a new test file.

What changed

  • Floored Bignum / and % (arith.go bigOp): were Euclidean (always non-negative remainder). Ruby's division is floored — the modulo takes the divisor's sign — so 13 % -(2**64) and 4 / -(2**64) now match MRI.
  • Float#% operator (arith.go floatOp): raised nothing on a zero divisor and used a truncation-based remainder that overflowed for large operands. It now routes through ruby_float_mod and raises ZeroDivisionError on a zero divisor, so 4.2 % Infinity == 4.2, 4.2 % -Infinity == -Infinity, -0.0 % 42 == -0.0, Infinity % 42 is NaN, and 1.0 % 0 raises.
  • Integer#coerce / Float#coerce: convert a non-Integer operand with Kernel#Float (String parse, #to_f dispatch) instead of rejecting it.
  • Integer#fdiv: divides exactly via big.Rat so huge Bignums round correctly (1.fdiv(10**323) == 1.0e-323) rather than overflowing both operands to NaN; a zero divisor gives ±Infinity.
  • Integer#div: floored quotient with MRI operand rules — a 0.0 Float divisor raises ZeroDivisionError, and a non-numeric runs the coerce protocol.
  • Float#divmod: raises FloatDomainError on a NaN/Infinite quotient; Float#%/#modulo method forms follow the operator.
  • Integer#round / Float#round precision argument: coerced through #to_int following MRI's NUM2INT range — a Bignum, a Float ±Infinity, or a value beyond a C int raises RangeError; a finite Float truncates toward zero.
  • Integer#bit_length works for a Bignum (over big.Int, using ~n for a negative receiver) instead of raising RangeError.
  • Float#<=>: nil for a NaN operand, orders an infinite receiver against a non-numeric answering #infinite? by sign, runs #coerce (a #coerce not returning [x, y] raises TypeError), nil otherwise.
  • Argument-count guards (gcd/lcm/gcdlcm, to_r, rationalize, imaginary/real/rectangular, fdiv) and residual aliases (inspect/magnitude/next/to_int/modulo/conj/imag/rect/angle/phase), with Float#quo re-established as a true alias of the redefined #fdiv.

Conformance deltas (MRI 4.0.6 rubyspec)

suite pass fail error
core/integer 500 -> 535 35 -> 10 17 -> 7
core/float 214 -> 238 39 -> 19 7 -> 3
core/numeric 283 -> 291 16 -> 8 27 -> 27

+67 passing examples, no regressions. The remaining residuals are outside this cluster's allowed files (Float#to_s shortest-repr in object.go; Numeric-subclass coerce dispatch and Complex/Rational returns in complex.go/rational.go; Integer#chr encoding).

Gates

  • go test ./internal/vm/ and go test ./... green.
  • GOOS=wasip1 GOARCH=wasm go build ./... OK.
  • gofmt -l clean on all changed files.
  • Every changed/added function in arith.go/numeric_edges.go at 100.0% in the merged -coverpkg profile.

🤖 Generated with Claude Code

tannevaled and others added 4 commits September 1, 2026 16:56
…e, fdiv, div, arity guards

Advance MRI 4.0.6 conformance across core/integer, core/float, core/numeric:

- bignum / and % were Euclidean (non-negative remainder); make them floored
  so the modulo takes the divisor's sign (13 % -4 == -3, 4 / -big == -1).
- Float#% (operator path) raised nothing on a zero divisor and mishandled a
  non-finite divisor / signed zero; route it through ruby_float_mod and raise
  ZeroDivisionError, so 4.2 % Infinity == 4.2, -0.0 % 42 == -0.0, 1.0 % 0 raises.
- Integer#coerce / Float#coerce now convert a non-Integer operand with Kernel#Float
  (String parse, #to_f dispatch) instead of rejecting it.
- Integer#fdiv divides exactly via big.Rat so huge Bignums round correctly
  (1.fdiv(10**323) == 1e-323) rather than overflowing both operands to NaN.
- Integer#div: floored quotient with MRI operand rules (Float 0.0 divisor raises
  ZeroDivisionError, coerce protocol for non-numerics).
- Float#divmod raises FloatDomainError on a NaN/Infinite quotient.
- Argument-count guards (gcd/lcm/gcdlcm, to_r, rationalize, imaginary/real/
  rectangular) and residual aliases (inspect/magnitude/next/to_int/modulo/
  conj/angle/phase/...).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…gth, Float#<=> NaN/Infinity/coerce

- Integer#round / Float#round now coerce the precision argument through #to_int
  and follow MRI's NUM2INT range: a Bignum, a Float ±Infinity, or a value beyond
  a C int raises RangeError; a finite Float truncates toward zero.
- Integer#bit_length works for a Bignum (compute over big.Int, using ~n for a
  negative receiver) instead of raising RangeError.
- Float#<=> returns nil for a NaN operand, orders an infinite receiver against a
  non-numeric answering #infinite? by sign, runs #coerce for a non-numeric (a
  #coerce not returning [x, y] raises TypeError), and is nil otherwise.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…ering

- Re-establish Float#quo as a true alias of #fdiv after #fdiv is redefined (the
  earlier alias pointed at the previous record).
- Add integer_float_residuals_test.go (value + error cases) and an internal test
  exercising guardArgc's skip branch, argcMessage, and rubyFloatMod edges.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Brings registerNumericAliasesAndEdges to 100% statement coverage.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled tannevaled changed the title Integer/Float/Numeric: advance MRI 4.0.6 conformance Integer/Float/Numeric: advance MRI 4.0.6 conformance (floored bignum div/mod, Float#% Inf/NaN, coerce, fdiv, div, round precision, <=>, aliases) Sep 1, 2026
tannevaled and others added 3 commits September 3, 2026 10:24
…0% gate

The POSIX coverage lanes (test ubuntu-latest and macos-latest) failed because
registerNumericEdges (62.5%) and its ratOf helper (0%) left enough new numeric
methods unexercised to drag the merged total below the rounding threshold.
Add TestNumericEdgesCoverage: it drives every method the function registers —
Integer#[] (bit/slice/Range), #size, #ord, #round(half:), the bit predicates,
#ceildiv, #quo (Float/Integer/Rational/Bignum divisors + coercion and
divide-by-zero errors), Float#remainder (sign and errors), Float#step, and the
grown floored-Bignum and Float%-zero operator branches — all checked against
MRI 4.0.6. registerNumericEdges and ratOf now report 100%.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…anch

registerNumericEdges' new Float#<=> overrides the base spaceshipNumeric for a
Float receiver, so spaceshipNumeric now only serves Integer#<=> — its
Float-self branch was unreachable dead code (dropping the merged total below the
100% gate on the POSIX lanes). Remove that branch and add coverage: an internal
whitebox test drives the inlined-away Integer/Bignum comparison paths directly,
and TestNumericEdgesCoverage exercises the Bignum<=>Float, NaN, non-numeric, and
#coerce paths via #send. Restores registerNumericEdges and ratOf to 100%.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Brings spaceshipNumeric to 100% (matching main), closing the last coverage gap
that intfloat's separate Float#<=> had left when it shadowed the base method.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
tannevaled and others added 3 commits September 3, 2026 19:19
…gain

The added Float#<=> shadowed the base spaceshipNumeric for a Float receiver,
dead-ending its Float branch and perturbing the merged -race coverage on the
POSIX lanes below the 100% gate even after every touched function reached 100%.
Restore MRI 4.0's proven design: Float#<=> routes through spaceshipNumeric
(Integer / Bignum / Float / #coerce / non-numeric), so its Float branch is live
and covered exactly as on main. This drops the Float infinity-vs-#infinite?
ordering and the "coerce must return [x, y]" TypeError the separate method had
added; their spec examples and unit cases are removed with it. The rest of the
Integer/Float/Numeric conformance work stands.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
registerNumericEdges (numeric_edges.go) redefines Integer/Float #fdiv, #coerce,
Integer#bit_length and Float#divmod with MRI-exact versions, but it runs AFTER
the original definitions here, so those originals became unreachable dead code —
~37 statements the suite could no longer cover, which dropped the merged -race
total on the POSIX lanes below the 100% gate (measured 79 uncovered vs main's
40). Delete the superseded originals (keeping the shared Numeric#coerce fallback
and the still-live Integer#divmod). Merged coverage returns to main's level.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit cb89642 into main Sep 3, 2026
7 checks passed
@tannevaled
tannevaled deleted the feat/agent-intfloat branch September 3, 2026 21:32
tannevaled added a commit that referenced this pull request Sep 3, 2026
Locks in #548 (Proc/Method/UnboundMethod), #549 (Integer/Float/Numeric) and
#550 (Enumerator/Lazy/Yielder). Measured 18245 passing on the pinned corpus,
up from 18102.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
tannevaled added a commit that referenced this pull request Nov 1, 2026
One of the ten examples the version move needs, and the one the headline figure
most misrepresented.

`core/objectspace/_id2ref_spec.rb` reads `-10` between 3.4.1 and 4.0.9, which
looks like the largest single regression of the move. Its guards say otherwise:

```ruby
ruby_version_is "4.0" do          # ONE example: "is deprecated"
ruby_version_is ""..."4.0" do     # TEN examples asserting it works
```

So the −10 is ten examples 4.0 no longer asks for, and the work is the single
4.0 example. Measured on this branch: `pass=0 fail=1` →
**`pass=1 fail=0 error=0 skip=0`**.

## Upstream

gc.c at v4.0.9, `os_id2ref` (gc.c:2167) opens with

```c
rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "ObjectSpace._id2ref is deprecated");
```

Category-gated, so an ordinary run stays silent: `Warning[:deprecated]` is off
by default. ruby/spec reaches it because the conformance shim turns the category
on (`spec_helper.rb:60`, with a comment already explaining exactly this), which
is also why the spec can assert it with a bare `complain(…)` and no
`verbose: true`.

## ⛔ The warning goes AFTER the arity check, and I had it the other way first

I wrote the call as the method's first statement, with a comment claiming that
was upstream's order. It is not.
`rb_define_module_function(rb_mObjSpace, "_id2ref", os_id2ref, 1)` (gc.c:5560)
declares a **fixed arity of 1**, so `rb_error_arity` fires in the dispatcher and
`os_id2ref` — warning and all — is never entered for a wrong argument count.
Warning first would make `ObjectSpace._id2ref` with no argument complain and
*then* raise, which MRI does not do. `objectspace_test.go` already pins that
ArgumentError, so the order is covered.

The declaration site being three thousand lines from the body is why this was
worth reading rather than assuming: the arity is not visible where the function
is written.

## warnDeprecatedGvar → warnDeprecated

The helper was already `rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, …)` in
full — the `Gvar` in its name was its first caller, not anything it does.
Renamed rather than copied, and its three existing call sites updated, because a
shadowing copy is how a method ends up with two bodies and one of them dead
(the root cause of #549's coverage failure, per the notes in
`scripts/coverage/`).

Refs #799

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant