Skip to content

Incorrect results for binary search on large arrays due to miscomputation of midpoint #261

@yurivish

Description

@yurivish

I think I've spotted a bug in d3's binary search implementations that arises due to the use of unsigned bit shifts for the midpoint computation, which is a good approach when the numbers are actually unsigned integers, but not when they're floats:

https://observablehq.com/d/229d8bcf448308b9

If I've understood the problem correctly, the fix is simple, replacing

const mid = (lo + hi) >>> 1;

in bisector.left and bisector.right with

const mid = Math.floor(lo + (hi - lo) / 2)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions