I am trying to take the maximum value from an array of f32
:
let v: [f32; 5] = [1.0, 2.0, 3.0, 4.0, 5.0];
let res = v.iter().max().unwrap();
But the trait Ord
is not implemented for f32
and I can't implement it because
only traits defined in the current crate can be implemented for arbitrary types [E0117]
I know about PartialOrd
, but I cannot figure out how to use it in this situation.
I am sure that my collection is not empty and it does not contain NaN
or infinity.
Is there any way to take max value from a f32 collection except by writing own wrappers or an imperative function like this?
fn maxf32(arr: &[f32; 5]) -> f32 {
let mut res = arr[0];
for &x in arr {
if x > res {
res = x;
}
}
res
}