1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 21:30:03 -04:00

Add emath::fast_midpoint (#7435)

This commit is contained in:
Emil Ernerfeldt
2025-08-08 12:04:51 +02:00
committed by GitHub
parent 3024c39eaf
commit 6fae65a3fa
5 changed files with 23 additions and 6 deletions

View File

@@ -112,6 +112,21 @@ where
(T::ONE - t) * *range.start() + t * *range.end()
}
/// This is a faster version of [`f32::midpoint`] which doesn't handle overflow.
///
/// ```
/// # use emath::fast_midpoint;
/// assert_eq!(fast_midpoint(1.0, 5.0), 3.0);
/// ```
#[inline(always)]
pub fn fast_midpoint<R>(a: R, b: R) -> R
where
R: Copy + Add<R, Output = R> + Div<R, Output = R> + One,
{
let two = R::ONE + R::ONE;
(a + b) / two
}
/// Where in the range is this value? Returns 0-1 if within the range.
///
/// Returns <0 if before and >1 if after.