Which standard do f32/f64::min/max
follow?
#87702
Labels
A-floating-point
Area: Floating point numbers and arithmetic
C-bug
Category: This is a bug.
T-libs-api
Relevant to the library API team, which will review and decide on the PR/issue.
I've been working on adding the new WebAssembly SIMD instructions to the
wide
crate, which is the most widely used crate for portable SIMD on stable Rust. As I finished up the implementation and ran the tests I noticed that various operations behave quite differently on the different architectures. In particular I ran across floating point min and max. Those have quite differing behavior when it comes to NaN handling and -0.0 / +0.0.When it comes to NaN there's three different strategies I've seen:
Ignoring all NaNs is defined in IEEE Std 754-2008 as
maxNum
:Then there's two different strategies when it comes to handling -0.0 and +0.0:
The IEEE Std 754-2008 allows the implementation to choose the behavior there. Not so much the IEEE Std 754-2019 which always requires -0.0 to be treated as smaller than +0.0. The 2019 standard defines the following two implementations:
Here's a list I created of various languages and processor instructions and how their max implementations behave: Survey of Floating Point Implementations for Maximum
Rust's min and max seem to not follow the latest standard and instead -0.0 and +0.0 are treated as equal. I'd say this behavior is likely incidental as Rust seems to just call into libm. Considering Rust intends to have a specification / standard at some point, we probably would need to cut some ties to libm and specify which IEEE 754 standard Rust intends to use (the reference points to 2008 for now at least). I'm mostly raising this to get some discussion going to see what the plan is moving forward, as it likely makes sense to adopt the new standard at some point.
The text was updated successfully, but these errors were encountered: