Skip to content

Keep overflowing Integer receivers exact in fdiv(Float) - #274

Merged
284km merged 1 commit into
mainfrom
codex-misc-unowned-numeric
Nov 25, 2026
Merged

284km merged 1 commit into
mainfrom
codex-misc-unowned-numeric

Conversation

@284km

@284km 284km commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Preserve a huge Integer receiver during fdiv(Float) when converting it to a double first would overflow. The recorded test_float_fdiv failure now passes on evaluator and VM. Full local acceptance is complete: 897/897 corpus programs on both engines, with hash indexing ON and OFF.

A1: current-main numeric RED reproduction and causes

The merged inventory PR #243 records the archived report before fixes.
This branch starts from current origin/main a63e47c. The clean reference
is Ruby 4.0.10 a704b7cd95. All selections below use unchanged upstream
files, one file at a time, --seed=1 and MERE_RUBY_GC_TIGHT=1.

The actual ext/-test-/integer C sources were built separately against the
reference headers and Mere C API headers, outside the reference install.
No helper is replaced and no reference gems are installed. A direct
reference probe confirms to_bignum(0) is a real forced Bignum.

The selected test_bignum.rb cases pass 8 tests / 60 assertions on Ruby;
main fails all 8 on evaluator and VM (34 assertions reached). Selected
test_integer.rb#test_pow passes 1 test / 36 assertions on Ruby; main times
out at its existing test deadline on both engines (6 assertions reached).
No deadline, budget, assertion or test size was changed.

Case Both main engines Cause / disposition
test_aref2 Fails negative-width read, expected shifted Bignum but got 1 Integer bit-slice width semantics; separate cause.
test_convert ArgumentError instead of RangeError Radix conversion loses C-int overflow check; separate cause.
test_digits_for_invalid_base_numbers Negative Bignum base is accepted Builtin radix sign validation; separate numeric cause.
test_divide Missing out-of-Float-range warning Explicit Bignum conversion warning; separate cause.
test_float_fdiv Finite 1e300 quotient becomes Infinity Integer receiver is converted before Float division; fix in this PR.
test_interrupt_during_to_s Expected thread.raise is not delivered during conversion Scheduler interrupt boundary; investigate separately.
test_to_f Missing out-of-Float-range warning Same conversion-warning cause as test_divide.
test_to_s2 ECHILD instead of RangeError Process wait flags lose C-int overflow check; investigate with integer argument conversion.
test_pow Existing per-test timeout after 6 assertions Huge negative exponent work/interrupt path; investigate separately.

First cause: overflowing Integer receiver in fdiv

CRuby first converts the receiver; when that overflows, its fdiv path
converts the Float divisor to its exact binary fraction and divides the
integer magnitudes. It does not divide Infinity by the divisor. The
reference also raises ZeroDivisionError for a zero Float divisor and
FloatDomainError for an infinite divisor on this overflow path, while
preserving NaN. These controls were reproduced before editing.

Implementation, corpus and final acceptance are recorded below after
validation. The other numeric causes in the table remain separate work.

The invalid-digits-base failure was checked at its exact assertion line
840: it is the ordinary negative Bignum base, not the earlier forced-small
Bignum controls. A standalone n.digits(-n), n = 2**1024 - 1, also fails
without any C API helper. This case stays in the numeric work stream.

fdiv implementation and focused acceptance

The final O2 helper keeps the ordinary finite-receiver conversion path.
When an Integer's private Float conversion overflows, it decomposes a
finite Float divisor into its exact binary fraction and uses the existing
integer-magnitude quotient routine. It preserves NaN and the reference's
overflow-path errors for zero and infinite divisors. Both primitive entry
paths share the helper; no Ruby conversion hook is called for a builtin
Float, and no global receiver or cache is added.

Corpus 1113 covers positive/negative receivers and divisors, finite and
overflowing results, Float::MAX, signed zero, infinities, NaN, ordinary
finite conversion, integer-pair division, coercion and a saved native alias.
Its complete stdout and exit status match clean Ruby on evaluator and VM
with tight GC, on the final O2 binary.

The following unchanged upstream selections were run one file at a time,
with seed 1 and tight GC. Reference and candidate pass all rows; main's
bignum selection retains the one recorded float_fdiv failure. RSS is bytes:

File / selection Main result Candidate result Main evaluator RSS Candidate evaluator RSS Main VM RSS Candidate VM RSS
ruby/test_bignum.rb, four fdiv tests 4 / 13, 1 failure 4 / 13, pass 179453952 178159616 179421184 179912704
ruby/test_integer.rb, fdiv/obj_fdiv 2 / 104, pass 2 / 104, pass 167329792 165969920 168886272 169050112

Build: compiler 9dec2ff6, mgz 661a01d8, ordinary O2. Immutable binary
SHA-256 b85f3c6d04c4978b9ef856952dc76b0fc2b6cfa56fccaff553db709ab77fceb3.
The generated C passes the bracket-depth gate. The full evaluator
sh run_corpus.sh --both and subsequent
MERE_RUBY_VM=1 sh run_corpus.sh --both each pass 897/897 programs with
hash indexing ON and OFF, all source gates, and full record_hygiene.
This loop record is also checked explicitly. No full unit suite or Rails
component suite was run, and no workflow is changed.

Completed full acceptance uses the same immutable O2 binary and runtime source as this PR head. This results-only update edits the PR description without a source/record-only push, following the CI backlog policy. Hosted acceptance and owner merge review remain separate.

@284km
284km marked this pull request as ready for review November 25, 2026 04:43

@284km 284km left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 6c28340. No Critical / Major / Minor findings.

  • Integer#fdiv(Float) with a Bignum receiver whose Float conversion overflows now divides exactly: the finite divisor is taken as its exact binary fraction (flt_rat_parts) and the quotient comes from big_fdiv_s. NaN, ±0.0 and ±Infinity divisors keep Ruby's answers, and receivers that convert to a finite Float keep the old path. Both entry points (prim_method_raw's integer fdiv and the VBig arm) go through the same helper.
  • Ran on the author's build at this head (clean tree, binary newer than every .mere file), on the evaluator and MERE_RUBY_VM=1, against ruby 4.0.10:
    • corpus 1113: identical output on both engines.
    • Extra probes: n.fdiv(1.0), n.fdiv(1e308), (-n).fdiv(3.0), (10**308).fdiv(0.1), (2**1100).fdiv(2.0**100), (10**400).fdiv(5e-324), n.fdiv(7e300), (10**30).fdiv(3.0) and n / 1e200: all identical.
  • Suggestion (not a regression; main gives the same): (10**500).quo(1e200) is still Infinity on both engines, where ruby gives 1.0e+300. Integer#quo with a Float argument is fdiv in ruby, so it could share big_float_fdiv in a follow-up.
  • The description has the local results our merge policy asks for: corpus --both and MERE_RUBY_VM=1 --both 897/897 with hash indexing ON and OFF, the source gates, record_hygiene, and the related upstream selections with RSS on both engines.
  • git merge-tree against current main: clean. No mere-pr-review-watch inline comments.

Merging.

@284km
284km merged commit 164c75f into main Nov 25, 2026
9 of 19 checks passed
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