node-binarysearch icon indicating copy to clipboard operation
node-binarysearch copied to clipboard

binarysearch.closest with decimals

Open Lee182 opened this issue 3 years ago • 1 comments

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?

Lee182 avatar Oct 22 '20 23:10 Lee182

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

jakobo avatar Dec 07 '21 19:12 jakobo