Repository navigation
Complex/Rational: advance MRI 4.0.6 conformance (constructors, #/ + #quo, division, to_s/inspect, exponent, ==, marshal_dump) - #544
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Advances rbgo (pure-Go Ruby VM, CGO=0) MRI 4.0.6 conformance on the
core/complexandcore/rationalrubyspec clusters. Confined tointernal/vm/complex.go,internal/vm/rational.go, a new test file, and oneminimal, noted change in
arith.go(below).Result
Every measured gap between rbgo and MRI 4.0.6 across
core/complex/*_spec.rband
core/rational/*_spec.rbis closed: the MRI-vs-rbgo per-spec pass/fail/errordiff is now 0 (was 11 differing specs, +16 examples).
Per-spec pass deltas (before → after):
quo 0→1, rectangular 11→12, to_s 7→8
Changes
Complex()/Complex.rectangularaccept a user subclass of
Numericand a zero-imaginaryComplexcomponent(via a shared
realComponenthelper) instead of only float-convertiblevalues; a non-real part is a
TypeError("not a real"). A loneComplexargument is returned unchanged.
#/+#quoaliasing.Complex#/andRational#/are now real methods(routed through the operator fast path) with
#quoas the true alias sharingone
Methodrecord, soinstance_method(:quo) == instance_method(:/).Complex#/by a real (zero-imaginary) divisordivides each component directly, so
Complex(20,40)/0.0 == Complex(Infinity,Infinity)with correct signs (not0/0 = NaN); exact partsstay exact and an exact-0 divisor still raises
ZeroDivisionError.#to_s/#inspectover Numeric subclasses. Built from the parts' own#to_s/#inspect(sign fromimag < 0, a*beforeiwhen the imaginarystring does not end in a digit); built-in parts keep the exact object-level
formatter (Float
-0.0/Infinity/NaN).Rational#**(Rational or Floatexponent) returns the principal
Complexroot when the base is negative andthe exponent is not a whole number.
#marshal_dump. PrivateComplex#marshal_dump/Rational#marshal_dumpreturn
[real, imag]/[numerator, denominator].Marshal.dumpbytes areunchanged (already the MRI
Uformat).Complex/RationaljoinInteger/Float/Bignumin the reflexive==case, socomplex == obj/rational == objdelegate toobj == selffor a non-numeric right operand(MRI's
Complex#==/Rational#==), keeping component-wise equality against anumber. 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 -lclean on changed files.-coverpkgprofile(valueIsNumeric, realComponent, makeComplex, complexRectangular, complexOp,
complexFormat, formattableComplexPart, strVal, ratPow, realPow, binaryOp,
registerComplex, registerRational).
core/integer,core/float,core/numeric, andcore/marshalComplex/Rational cases unchanged vs MRI.FLOORuntouched.🤖 Generated with Claude Code