Skip to main content

24_min_max/
24_min_max.rs

1//! `min` and `max` reductions with explicit NaN policy.
2//!
3//! Run: cargo run --example 24_min_max
4//!
5//! `matten` returns NaN from min/max if any element is NaN.
6//! This is deliberate: it is more predictable than silently ignoring NaN.
7
8use matten::Tensor;
9
10fn main() {
11    let v = Tensor::from_vec(vec![3.0, 1.0, 4.0, 1.0, 5.0, 9.0, 2.0]);
12    println!("min = {}", v.min()); // 1.0
13    println!("max = {}", v.max()); // 9.0
14
15    // NaN policy: any NaN -> result is NaN
16    let with_nan = Tensor::from_vec(vec![1.0, f64::NAN, 3.0]);
17    println!("min with NaN = {}", with_nan.min()); // NaN
18    println!("max with NaN = {}", with_nan.max()); // NaN
19    assert!(with_nan.min().is_nan());
20    assert!(with_nan.max().is_nan());
21
22    // Inf is handled normally
23    let with_inf = Tensor::from_vec(vec![1.0, f64::INFINITY, -1.0]);
24    println!("min with +inf = {}", with_inf.min()); // -1.0
25    println!("max with +inf = {}", with_inf.max()); // inf
26
27    println!("NaN policy verified: OK");
28}