mrhooray/kdtree-rs

Stack overflow in add_to_bucket

TYPEmber opened this issue · 2 comments

    I get the same error when I use this crate.

And I figured out the reason.

The avg will be equal to a, under the limit of f64's accuracy. As a result, the fn split() can't split this node successfully. In this situation, fn add_to_bucket() and fn split() will call each other infinitely.

    let a = 0.47945351705599926f64;
    let b = 0.47945351705599931f64;
    
    let avg = (a + b) / 2.0;

    println!("{:.20E}", a);
    println!("{:.20E}", b);
    println!("{:.20E}", avg);

And the resolution is extremely easy. Just modify fn belongs_in_left() like this

fn belongs_in_left(&self, point: &[A]) -> bool {
        // before
        // point[self.split_dimension.unwrap()] < self.split_value.unwrap()
        // after
        point[self.split_dimension.unwrap()] <= self.split_value.unwrap()
}

Originally posted by @TYPEmber in #20 (comment)

#43 I have fixed it in this pr.

Thanks for the fix. Going to publish new version after getting CI running again through github actions.