fix: return nearest element from binarySearchNearest instead of window edge - #843
Open
NemeZZiZZ wants to merge 1 commit into
Open
fix: return nearest element from binarySearchNearest instead of window edge#843NemeZZiZZ wants to merge 1 commit into
NemeZZiZZ wants to merge 1 commit into
Conversation
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.
Problem
binarySearchNearestnarrows the window to ≤ 2 (up to three candidates:left,left+1,right) and then returnsleftunconditionally — the window's left edge, not the nearest element:Verified by execution:
[10, 20, 30, 40, 50], target39→ returns index2(value 30) instead of3(value 40); target100→ index3instead of4.Consumers:
Chart.scrollToTimestamp(Chart.ts:1172),Chart.zoomAtTimestamp(Chart.ts:1202),Store.timestampToDataIndex(Store.ts:1061) — all land 1–2 bars off the requested timestamp.Fix
After the loop, compare the remaining candidates in
[left, right](at most three) and return the one with the smallest distance to the target. Ties resolve to the lower index.Verification
Fuzz against brute-force nearest, 5000 random strictly-increasing arrays (lengths 1–300, targets in/around/beyond range): 5000/5000 match, ties to the lower index. The audit examples:
39 → 3,100 → 4,21 → 1, exact hit30 → 2.Note: on arrays with duplicate keys the result is still an index at the minimal distance, but among ties spanning more than the final window any tied index may be returned (pre-existing behavior; irrelevant for bar timestamps, which are unique).