Skip to content

Implement Range#bsearch - #434

Merged
tannevaled merged 1 commit into
mainfrom
feat/cluster9
Aug 23, 2026
Merged

tannevaled merged 1 commit into
mainfrom
feat/cluster9

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Range#bsearch was missing (only Array#bsearch existed), so
core/range/bsearch_spec.rb was 0/6/71 — every example raised
NoMethodError.

It binary-searches the range's numeric domain in both bsearch modes (a
true/false block finds the minimum satisfying element; a numeric block
finds an exact <=> 0 hit):

  • an all-integer range searches the integer values directly
    (Integer or Bignum bounds), honouring an exclusive end;
  • a range with any Float bound searches the IEEE-754 bit domain,
    mapping doubles to a monotonic int64 (MRI's double_as_int64) so
    the search crosses zero and reaches Float::MAX and ±Infinity;
  • endless / beginless ranges grow the missing bound outward first;
  • a non-numeric bound or block result raises the MRI TypeError, and
    the no-block Enumerator reports a nil #size.

The block-result decision is factored into a shared bsearchProbe, and
an overflow-safe midpoint (bsearchMid) keeps the float bit-domain
search correct when the interval spans more than half the int64 range.

Verified against ruby 4.0.6; bsearch_spec now 75/0/2 (the two
remaining errors are a separate Kernel#eval-caller-binding limitation,
not bsearch). A full language+core sweep shows +75 with no
regressions and the whole-suite coverage gate stays at 100.0%.

🤖 Generated with Claude Code

Range#bsearch was missing (only Array#bsearch existed), so
core/range/bsearch_spec was 0/6/71 — every example raised NoMethodError.

It binary-searches the range's numeric domain in both bsearch modes (a
true/false block finds the minimum satisfying element; a numeric block
finds an exact <=> 0 hit). An all-integer range searches the integer
values directly (with #to_int-free Integer/Bignum bounds), honouring an
exclusive end; a range with any Float bound searches the IEEE-754 bit
domain, mapping doubles to a monotonic int64 (MRI's double_as_int64) so
the search crosses zero and reaches Float::MAX and ±Infinity correctly.
Endless and beginless ranges grow the missing bound outward first. A
non-numeric bound (or block result) raises the MRI TypeError, and the
no-block Enumerator reports a nil #size.

The block-result decision is factored into a shared bsearchProbe, and an
overflow-safe midpoint (bsearchMid) keeps the float bit-domain search
correct when the interval spans more than half the int64 range.

Verified against ruby 4.0.6; bsearch_spec now 75/0/2 (the two remaining
errors are a separate Kernel#eval-caller-binding limitation, not
bsearch).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 37e984e into main Aug 23, 2026
7 checks passed
@tannevaled
tannevaled deleted the feat/cluster9 branch August 23, 2026 21:01
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