-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
std::cmp::max vs std::num::Float vs NaN #21816
Comments
I personally think |
This issue should be moved to https://github.com/rust-lang/rfcs? Regarding your second suggestion, how would you implement Alternative: we could introduce an additional trait, implemented for Ord and f32, f64: trait Lattice : PartialOrd {
fn max(&self, other: &Self) -> Self;
fn min(&self, other: &Self) -> Self;
}
impl<T: Ord> Lattice for T { ... }
impl Lattice for f32 { ... }
impl Lattice for f64 { ... }
fn max<T: Lattice>(x: T, y: T) -> T { x.max(&y) }
fn min<T: Lattice>(x: T, y: T) -> T { x.min(&y) } The drawback is that, we need to introduce yet another comparison trait. @bombless Note that IEEE 754 defines |
We have Float::max anyway, so I don't think IEEE 754 could be a real problem. |
I'm pulling a massive triage effort to get us ready for 1.0. As part of this, I'm moving stuff that's wishlist-like to the RFCs repo, as that's where major new things should get discussed/prioritized. As these functions are stable, changing them would require going through the RFC process. This issue has been moved to the RFCs repo: rust-lang/rfcs#852 |
It's werid that
std::cmp::max
can't comparef64
numbers. I know that strictly speaking IEEE floats don't have total order this function expects, but still it's surprising (andpartial_max
is awkward to use).And there's
std::num::Float::max
which works withf64
just fine (the docs don't even say howNaN
is handled).It bugs me that the two versions of
max
are not consistent in their strictness, and that the first-and-most-obviousmax
function in the stdlib "doesn't work" with a basic type in the language.My suggestion:
max
version that only allowsOrd
to something else, liketotal_max
orstrict_max
.std::cmp::max
for floating point numbers, so thata.max(b)
is consistent withmax(a,b)
.The text was updated successfully, but these errors were encountered: