Skip to content

Commit

Permalink
ui: Use PositiveSign instead of NotNan in FpsCounter.
Browse files Browse the repository at this point in the history
Part of <#537>.
This doesn’t directly remove any negative-zero hazards, but it is a
more appropriately bounded data type regardless (frame time cannot be
negative).
  • Loading branch information
kpreid committed Nov 24, 2024
1 parent c23942d commit fc3f723
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
11 changes: 11 additions & 0 deletions all-is-cubes-base/src/math/restricted_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ impl<T: FloatCore> PositiveSign<T> {
unsafe { NotNan::new_unchecked(self.0) }
}

/// Returns whether the value is finite.
///
/// Since the value is statically guaranteed to be neither NaN nor negative,
/// the only case where this returns `false` is when the value is positive infinity.
#[inline]
pub fn is_finite(&self) -> bool {
// We could delegate to FloatCore::is_finite() but that would perform an unnecessary
// NaN check.
self.0 != T::infinity()
}

#[cfg(test)]
#[track_caller]
pub(crate) fn consistency_check(self)
Expand Down
10 changes: 5 additions & 5 deletions all-is-cubes-ui/src/apps/time.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use all_is_cubes::math::NotNan;
use all_is_cubes::math::{zo64, PositiveSign, ZeroOne};
use all_is_cubes::time::{Duration, Instant, TickSchedule};
#[cfg(doc)]
use all_is_cubes::universe::Universe;
Expand Down Expand Up @@ -142,7 +142,7 @@ impl<I: Instant> FrameClock<I> {
#[derive(Clone, Debug, Eq, PartialEq)]
#[doc(hidden)] // TODO: Decide whether we want FpsCounter in our public API
pub struct FpsCounter<I> {
average_frame_time_seconds: Option<NotNan<f64>>,
average_frame_time_seconds: Option<PositiveSign<f64>>,
last_frame: Option<I>,
}

Expand Down Expand Up @@ -179,12 +179,12 @@ impl<I: Instant> FpsCounter<I> {
None
}
})
.and_then(|duration| NotNan::new(duration.as_secs_f64()).ok());
.and_then(|duration| PositiveSign::try_from(duration.as_secs_f64()).ok());
if let Some(this_seconds) = this_seconds {
self.average_frame_time_seconds = Some(
if let Some(previous) = self.average_frame_time_seconds.filter(|v| v.is_finite()) {
let mix = 2.0f64.powi(-3);
this_seconds * mix + previous * (1. - mix)
const MIX: ZeroOne<f64> = zo64(1.0 / 8.0);
this_seconds * MIX + previous * MIX.complement()
} else {
// recover from any weirdness or initial state
this_seconds
Expand Down

0 comments on commit fc3f723

Please sign in to comment.