-
Notifications
You must be signed in to change notification settings - Fork 77
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
overflow panic in debug estimating cost #598
Comments
Saturating arithmetic could give incorrect results. The best way would probably be to use checked arithmetic instead in the few places where this might happen (such as adding costs), and document in a "# Panics" section in which case this method might panic. |
If the estimated cost is exceeding the bound of the numeric datatype neither wrapping nor saturating will be "correct", but as a user my preference would be saturating > wrapping > panic. For a game I prioritise not crashing over total correctness, and accept that if the costs I've fed to astar are this high it's a problem on my end I should fix it. But it's still vastly more preferable that some routes be suboptimal or not resolvable over a straight panic. The majority of the time this code will be run is on end-user systems who cannot/will not be able to report or debug a panic like this. If checked add is the chosen option, either returning |
Thanks Trent for discussing this further. First of all, I'm not ready to have this crate silently report incorrect results ("suboptimal" is incorrect as far as A* is concerned), but we can try to devise solutions to the problem and the needs you are exposing (those are different). Right now, I cannot use any other operator than Concerning the point you raise: you can already get the behavior you want by using a thin wrapper around your cost type which defines addition as a saturating one. This would get you the result you expect, even in the case of panics on overflow since it will never overflow. Could that work for you? |
Oh, I'd forgotten C was a generic, so yes I could indeed do that :) thank you for the suggestion.
Understandable :) |
Hey, while running in debug mode I got a panic in the astar implmentation due to overflow.
pathfinding/src/directed/astar.rs
Line 140 in 2aa2326
I probably have some pathological setup, but for correctness the estimated cost should probably saturate instead of wrapping. Thoughts on using
new_cost.saturating_add(h)
?The text was updated successfully, but these errors were encountered: