`SimdPartialOrd::simd_min()` and `simd_max()` aren't commutative when values aren't comparable
- Dominant language
- Rust
- Stars
- 347
- Forks
- 39
- PR merge metrics
- No merged PRs in 30d
Description
The base assumption is that `min(a, b) == min(b, a)`, and same for `max`.
This is not true when values are not comparable (= when `PartialOrd::partial_cmp` would return `None`) because, in that case, these two methods always return the right hand side.
In other words, `a.simd_min(b) == b` but `b.simd_min(a) == a` when `a.partial_cmp(b) == None`.
This happens, for instance, when comparing a regular `f32` and `f32::NAN`:
```rust
let number = 1.0f32;
let nan = f32::NAN;
println!("{}", number.simd_min(nan)); // Prints "NaN"
println!("{}", nan.simd_min(number)); // Prints "1"
```
This happens because the current implementation of `simd_min` is the following:
```rust
fn simd_min(self, other: Self) -> Self {
if self <= other {
self
} else {
other
}
}
```
and a `<=` (or any syntax-sugared comparison in general) between two instances `PartialOrd` where `a.partial_ord(&b)` returns `None` is evaluated to `false`.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.