Numeric
Proves exact static selection, magnitude and limit operations for a numeric type. Built-in integers, byte and IEEE representations use compiler-bound existing source implementations. No runtime witness, allocation or implicit conversion is introduced. User numeric owners may satisfy the same exact static members. Lifecycle capabilities remain separate; this concept grants no copy or move.
Module std.math
import std.math;Public declaration contracts and authored examples. A page can document several related symbols. Implementation bodies are excluded.
Numeric
pub concept Numeric<Self>Proves exact static selection, magnitude and limit operations for a numeric type. Built-in integers, byte and IEEE representations use compiler-bound existing source implementations. No runtime witness, allocation or implicit conversion is introduced. User numeric owners may satisfy the same exact static members. Lifecycle capabilities remain separate; this concept grants no copy or move. Self: Exact representation; operations preserve its type and policies.
Example
module example.numeric_concept;
from std.math import Numeric;
fn Lowest<T>() -> T where T: Numeric, Defaultable {
return T.MinimumNumber(default(T));
}
fn Highest<T>() -> T where T: Numeric, Defaultable {
return T.MaximumNumber(default(T));
}
fn BoundMagnitude<T>(Left: T, Right: T, Low: T, High: T) -> T where T: Numeric, Copyable {
let Smaller = T.SelectMinimum(Left, Right);
let Larger = T.SelectMaximum(Smaller, Right);
return T.AbsoluteNumber(T.ClampNumber(Larger, Low, High));
}
fn Main() -> i32 {
if Lowest<i8>() != -128_i8 || Highest<i8>() != 127_i8 { return 1; }
if BoundMagnitude(-10_i32, -5_i32, -7_i32, 0_i32) != 5 { return 2; }
return 0;
}Numeric.SelectMinimum
requires static fn SelectMinimum(Left: Self, Right: Self) -> Self;Selects the smaller value, retaining the first equal value and propagating NaN. Left: First value. Right: Second value. Return: Exact selected value.
Numeric.SelectMaximum
requires static fn SelectMaximum(Left: Self, Right: Self) -> Self;Selects the larger value with the same tie and NaN policy. Left: First value. Right: Second value. Return: Exact selected value.
Numeric.ClampNumber
requires static fn ClampNumber(Value: Self, Minimum: Self, Maximum: Self) -> Self;Restricts Value to ordered inclusive bounds without changing its representation. Value: Input value. Minimum: Lower bound. Maximum: Upper bound. Return: Selected value; reversed/NaN bounds trap.
Numeric.AbsoluteNumber
requires static fn AbsoluteNumber(Value: Self) -> Self;Produces a nonnegative magnitude, normalizing floating negative zero. Value: Input value. Return: Magnitude; unrepresentable signed minimum traps.
Numeric.MinimumNumber
requires static fn MinimumNumber(Witness: Self) -> Self;Returns the exact lowest finite representation value. Witness: Type witness only, never retained. Return: Lowest finite value.
Numeric.MaximumNumber
requires static fn MaximumNumber(Witness: Self) -> Self;Returns the exact highest finite representation value. Witness: Type witness only, never retained. Return: Highest finite value.