Skip to content

binarysearch.closest with decimals #12

Open

Description

@Lee182
const index = binary.closest([25, 33, 50], 32.99)
expect(index).to.eq(1)

// this throws and the index is 0. I would expect it to be 1
// does the compare closest function work with decimals?

Activity

  1. theCodeDrift commented on Dec 7, 2021

    @theCodeDrift

    closest returns the leading edge. If you want the trailing edge, pass {end: true} to the function.

    var bs = require("binarysearch")
    const needle = 32.99;
    const haystack = [25, 33, 50];
    
    console.log(bs.closest(needle, haystack)); // 0
    console.log(bs.closest(needle, haystack, {end: true})); // 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions