Repository navigation
Implement Range#bsearch - #434
Merged
Merged
Conversation
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>
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.
Range#bsearchwas missing (onlyArray#bsearchexisted), socore/range/bsearch_spec.rbwas0/6/71— every example raisedNoMethodError.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
<=> 0hit):(Integer or Bignum bounds), honouring an exclusive end;
mapping doubles to a monotonic
int64(MRI'sdouble_as_int64) sothe search crosses zero and reaches
Float::MAXand±Infinity;TypeError, andthe no-block Enumerator reports a
nil#size.The block-result decision is factored into a shared
bsearchProbe, andan overflow-safe midpoint (
bsearchMid) keeps the float bit-domainsearch correct when the interval spans more than half the
int64range.Verified against ruby 4.0.6;
bsearch_specnow75/0/2(the tworemaining 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