Skip to content
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

Implement NaN propagating minmax with TotalOrder and FloatCore #323

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2297,6 +2297,38 @@ pub trait TotalOrder {
/// check_lt(-0.0_f64, 0.0_f64);
/// ```
fn total_cmp(&self, other: &Self) -> Ordering;

/// Get the maximum of two numbers, propagating NaN
///
/// For this operation, -0.0 is considered to be less than +0.0 as
/// specified in IEEE 754-2019.
#[must_use]
fn maximum(self, other: Self) -> Self
where
Self: FloatCore,
{
match (self.is_nan(), other.is_nan()) {
(true, _) => self,
(_, true) => other,
_ => core::cmp::max_by(self, other, Self::total_cmp),
}
}

/// Get the minimum of two numbers, propagating NaN
///
/// For this operation, -0.0 is considered to be less than +0.0 as
/// specified in IEEE 754-2019.
#[must_use]
fn minimum(self, other: Self) -> Self
where
Self: FloatCore,
{
match (self.is_nan(), other.is_nan()) {
(true, _) => self,
(_, true) => other,
_ => core::cmp::min_by(self, other, Self::total_cmp),
}
}
}
macro_rules! totalorder_impl {
($T:ident, $I:ident, $U:ident, $bits:expr) => {
Expand Down