Skip to content

Complex/Rational: advance MRI 4.0.6 conformance (constructors, #/ + #quo, division, to_s/inspect, exponent, ==, marshal_dump) - #544

Merged
tannevaled merged 2 commits into
mainfrom
feat/agent-numeric
Sep 1, 2026
Merged

tannevaled merged 2 commits into
mainfrom
feat/agent-numeric

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Advances rbgo (pure-Go Ruby VM, CGO=0) MRI 4.0.6 conformance on the
core/complex and core/rational rubyspec clusters. Confined to
internal/vm/complex.go, internal/vm/rational.go, a new test file, and one
minimal, noted change in arith.go (below).

Result

Every measured gap between rbgo and MRI 4.0.6 across core/complex/*_spec.rb
and core/rational/*_spec.rb is closed: the MRI-vs-rbgo per-spec pass/fail/error
diff is now 0 (was 11 differing specs, +16 examples).

Per-spec pass deltas (before → after):

  • complex/divide 8→9, equal_value 3→5, inspect 2→4, marshal_dump 0→2,
    quo 0→1, rectangular 11→12, to_s 7→8
  • rational/equal_value 3→4, exponent 22→24, marshal_dump 0→2, quo 0→1

Changes

  • Constructors accept any real Numeric. Complex() / Complex.rectangular
    accept a user subclass of Numeric and a zero-imaginary Complex component
    (via a shared realComponent helper) instead of only float-convertible
    values; a non-real part is a TypeError ("not a real"). A lone Complex
    argument is returned unchanged.
  • #/ + #quo aliasing. Complex#/ and Rational#/ are now real methods
    (routed through the operator fast path) with #quo as the true alias sharing
    one Method record, so instance_method(:quo) == instance_method(:/).
  • Real-divisor division. Complex#/ by a real (zero-imaginary) divisor
    divides each component directly, so Complex(20,40)/0.0 == Complex(Infinity,Infinity) with correct signs (not 0/0 = NaN); exact parts
    stay exact and an exact-0 divisor still raises ZeroDivisionError.
  • #to_s / #inspect over Numeric subclasses. Built from the parts' own
    #to_s/#inspect (sign from imag < 0, a * before i when the imaginary
    string does not end in a digit); built-in parts keep the exact object-level
    formatter (Float -0.0 / Infinity / NaN).
  • Negative-base fractional exponent. Rational#** (Rational or Float
    exponent) returns the principal Complex root when the base is negative and
    the exponent is not a whole number.
  • #marshal_dump. Private Complex#marshal_dump / Rational#marshal_dump
    return [real, imag] / [numerator, denominator]. Marshal.dump bytes are
    unchanged (already the MRI U format).
  • arith.go (minimal, noted). Complex/Rational join
    Integer/Float/Bignum in the reflexive == case, so complex == obj /
    rational == obj delegate to obj == self for a non-numeric right operand
    (MRI's Complex#== / Rational#==), keeping component-wise equality against a
    number. This is the only edit outside the two target files.

Verification

  • go test ./internal/vm/ green; all other ./... packages green.
  • GOOS=wasip1 GOARCH=wasm go build ./... OK; gofmt -l clean on changed files.
  • Every changed function is 100.0% covered in the -coverpkg profile
    (valueIsNumeric, realComponent, makeComplex, complexRectangular, complexOp,
    complexFormat, formattableComplexPart, strVal, ratPow, realPow, binaryOp,
    registerComplex, registerRational).
  • No regressions: core/integer, core/float, core/numeric, and
    core/marshal Complex/Rational cases unchanged vs MRI. FLOOR untouched.

🤖 Generated with Claude Code

tannevaled and others added 2 commits September 1, 2026 09:43
Close the measurable gaps between rbgo and MRI 4.0.6 on core/complex and
core/rational rubyspec:

- Complex() / Complex.rectangular accept any real Numeric (including a
  user subclass of Numeric) and a zero-imaginary Complex component, via a
  shared realComponent helper, rather than only float-convertible values.
- Complex#/ is a real method (routed through the operator) with #quo as its
  true alias sharing one Method record; same for Rational#/ and #quo, so
  Complex/Rational.instance_method(:quo) == instance_method(:/).
- Complex#/ by a real (zero-imaginary) divisor divides each component
  directly, so /0.0 yields signed Infinity per component (not 0/0 NaN).
- Complex#to_s / #inspect build the string from the parts' own #to_s /
  #inspect (sign from imag<0, trailing * before i for a non-digit tail)
  when a part is a Numeric subclass; built-in parts keep the exact
  object-level formatter (-0.0 / Infinity / NaN).
- Rational#** (and Rational ** Float) return the principal Complex root
  when the base is negative and the exponent is not a whole number.
- Complex#marshal_dump / Rational#marshal_dump (private) return
  [real, imag] / [numerator, denominator]; Marshal bytes unchanged.
- arith.go (minimal, noted): Complex/Rational join Integer/Float/Bignum in
  the reflexive `==` case so `complex == obj` / `rational == obj` delegate
  to `obj == self` for a non-numeric right operand (MRI's Complex#== /
  Rational#==), keeping component-wise equality against a number.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Add internal/vm/numeric_residuals_test.go exercising every new branch:
constructor acceptance of real Numerics and zero-imaginary Complex parts,
#/ + #quo aliasing, real-divisor division (including /0.0 → Infinity),
#to_s/#inspect over Numeric subclasses (sign, the `*i` tail, strVal
fallback), negative-base fractional exponentiation, marshal_dump, and the
reflexive #== fallback. All changed functions reach 100% coverage.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 783f293 into main Sep 1, 2026
7 checks passed
@tannevaled
tannevaled deleted the feat/agent-numeric branch September 1, 2026 08:56
tannevaled added a commit that referenced this pull request Sep 1, 2026
Locks in #544 (Complex + Rational), #545 (Hash methods), #546 (Regexp,
MatchData and match globals). Measured 18102 passing on the pinned corpus,
up from 17983.

Co-authored-by: Claude Opus 4.8 <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