-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
attempt to add with overflow #139
Comments
Unless I'm misunderstanding your suggestion, your fix + test combo results in an error in debug mode: |
Yes! |
What is the solution to this? |
I think there is none so far besides using a larger/signed data type. I suspect a fix would entail inline the computation of the envelope center into the closure used by Maybe we can introduce something like |
I don't think this will work but also don't think for the test case itself, anything but widening it to So I replaced the body of fn sort_envelopes_by_center<T: RTreeObject<Envelope = Self>>(&self, envelopes: &mut [T]) {
let one = <Self::Point as Point>::Scalar::one();
let two = one + one;
envelopes.sort_by(|l, r| {
let l = l.envelope();
let r = r.envelope();
let l = Self::Point::generate(|axis| {
let x1 = l.lower.nth(axis);
let y1 = l.upper.nth(axis);
let x2 = self.lower.nth(axis);
let y2 = self.upper.nth(axis);
// x2 <= x1 <= y1 <= y2
// (x1 + y1) / two - (x2 + y2) / two
// x1 + (y1 - x1) / two - x2 - (y2 - x2) / two
(x1 / two - x2 / two) + (y1 / two - y2 / two)
})
.length_2();
let r = Self::Point::generate(|axis| {
let x1 = r.lower.nth(axis);
let y1 = r.upper.nth(axis);
let x2 = self.lower.nth(axis);
let y2 = self.upper.nth(axis);
(x1 / two - x2 / two) + (y1 / two - y2 / two)
})
.length_2();
l.partial_cmp(&r).unwrap()
});
} and this does actually through computing the center distance coordinates, only to blow up on the accumulator later when computing That said, the given points use just too much of the range of let points = [
[-1231069110, 165759396],
[781046969, 1474650861],
[2044084841, -178727451],
[477217386, 1765868819],
[1257259285, -1226428015],
[-1802029933, -488481506],
[-1786107855, 2050884187],
];
for l in points {
for r in points {
l.sub(&r);
}
} will overflow which to me suggests there is nothing that can be done but widening the coordinates to @w14 I think you could still give the branch rstar = { git = "https://github.com/georust/rstar.git", branch = "sort-envelopes-by-center" } but I don't think we can commit this implementation as it requires more operations than required in the common floating point case. We might want to provide the trait method (but without a an implementation that is different from what we have now) though so that it can be overwritten in downstream crates which want to use unsigned integers though? |
To be honest, I think the only reasonable course of action for this particular issue to close this as requiring a larger coordinate type like |
Probably related to #138.
Min repro:
Integer overflow is on this line:
rstar/rstar/src/aabb.rs
Line 180 in 9f8c6b6
The fix is probably to write this as:
The text was updated successfully, but these errors were encountered: