MinMax
Selects the smaller value, propagating NaN and preserving the first equal value.
Module std.math
import std.math;Public declaration contracts and authored examples. A page can document several related symbols. Implementation bodies are excluded.
fn
pub const fn Min<T>(Left: T, Right: T) -> T where T: Numeric, CopyableSelects the smaller value, propagating NaN and preserving the first equal value. T: Copyable supported numeric representation. Left: First value; selected on ties. Right: Second value; no ownership or borrow is retained. Return: Numeric minimum; a NaN operand propagates without allocation.
Example
module example.numeric_selection;
from std.math import Min, Max, Clamp, Abs, NumericLimits, IsNaN;
fn Main() -> i32 {
if Min(7_i32, 3_i32) != 3_i32 || Max(7_i32, 3_i32) != 7_i32 { return 1; }
if Clamp(12_i32, 0_i32, 10_i32) != 10_i32 { return 2; }
if Abs(-42_i64) != 42_i64 { return 3; }
if NumericLimits<i8>.MinValue() != -128_i8 { return 4; }
if NumericLimits<u128>.MaxValue() != 340282366920938463463374607431768211455_u128 { return 5; }
if bit_cast<u64>(NumericLimits<f64>.MaxValue()) != 0x7FEFFFFFFFFFFFFF_u64 { return 8; }
if bit_cast<u64>(NumericLimits<f64>.MinValue()) != 0xFFEFFFFFFFFFFFFF_u64 { return 9; }
if NumericLimits<usize>.MaxValue() != truncate<usize>(340282366920938463463374607431768211455_u128) { return 10; }
let Nan = bit_cast<f32>(0x7FC00001_u32);
let Propagated = Min(1_f32, Nan);
if !IsNaN(Propagated) { return 6; }
if bit_cast<u32>(Abs(bit_cast<f32>(0x80000000_u32))) != 0_u32 { return 7; }
return 0;
}fn
pub const fn Max<T>(Left: T, Right: T) -> T where T: Numeric, CopyableSelects the larger value, propagating NaN and preserving the first equal value. T: Copyable supported numeric representation. Left: First value, selected on ties. Right: Second value, borrowed by neither the result nor any stored state. Return: Numeric maximum or a NaN operand; allocates nothing.