From bd636471774fda847e2a6eaa92a72600428a16cc Mon Sep 17 00:00:00 2001 From: Carter Schmidt <35572862+Fyrecean@users.noreply.github.com> Date: Sun, 1 Mar 2026 23:54:47 -0800 Subject: [PATCH] Fix crash when dragging a DragValue through small floats. (#7939) Increased smart_aim `NUM_DECIMALS` from 15 to 16 to fix crash in `best_in_range_f64` The f64 value 0.09999999999999995, when multiplied by `scale_factor` and rounded, becomes 16 digits (999999999999999.5 -> 1000000000000000) and the leading 1 is clipped off by `to_decimal_string` resulting in all 0s and triggering the debug_assert! message "Bug in smart aim code" * Closes * [x] I have followed the instructions in the PR template --- crates/emath/src/smart_aim.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/emath/src/smart_aim.rs b/crates/emath/src/smart_aim.rs index c1b96ec7b..5d6a3e0fc 100644 --- a/crates/emath/src/smart_aim.rs +++ b/crates/emath/src/smart_aim.rs @@ -2,7 +2,7 @@ use crate::fast_midpoint; -const NUM_DECIMALS: usize = 15; +const NUM_DECIMALS: usize = 16; /// Find the "simplest" number in a closed range [min, max], i.e. the one with the fewest decimal digits. /// @@ -143,6 +143,10 @@ fn from_decimal_string(s: [u8; NUM_DECIMALS]) -> u64 { #[expect(clippy::approx_constant)] #[test] fn test_aim() { + assert_eq!( + best_in_range_f64(0.0799999999999996, 0.09999999999999995), + 0.08, + ); assert_eq!(best_in_range_f64(-0.2, 0.0), 0.0, "Prefer zero"); assert_eq!(best_in_range_f64(-10_004.23, 3.14), 0.0, "Prefer zero"); assert_eq!(best_in_range_f64(-0.2, 100.0), 0.0, "Prefer zero");