Ferrous API Reference

All Types / Ferrous.Results

class CheckedMath

public static class CheckedMath

Zero-allocation Rust-inspired checked and saturating integer arithmetic helpers. Checked operations return Option<T>.None on overflow or division by zero; saturating operations clamp to IMinMaxValue<TSelf>.MinValue or IMinMaxValue<TSelf>.MaxValue.

Methods

public static Option<T> CheckedAdd<T>(T left, T right) where T : IBinaryInteger<T>, IMinMaxValue<T>

Adds two integers, returning Option<T>.None on arithmetic overflow.

public static Option<T> CheckedSub<T>(T left, T right) where T : IBinaryInteger<T>, IMinMaxValue<T>

Subtracts right from left, returning Option<T>.None on overflow.

public static Option<T> CheckedMul<T>(T left, T right) where T : IBinaryInteger<T>, IMinMaxValue<T>

Multiplies two integers, returning Option<T>.None on arithmetic overflow.

public static Option<T> CheckedDiv<T>(T left, T right) where T : IBinaryInteger<T>, IMinMaxValue<T>

Divides left by right, returning Option<T>.None on division by zero or signed minimum-value overflow (MinValue / -1).

public static T SaturatingAdd<T>(T left, T right) where T : IBinaryInteger<T>, IMinMaxValue<T>

Adds two integers, saturating at IMinMaxValue<TSelf>.MinValue or IMinMaxValue<TSelf>.MaxValue instead of overflowing.

public static T SaturatingSub<T>(T left, T right) where T : IBinaryInteger<T>, IMinMaxValue<T>

Subtracts right from left, saturating at IMinMaxValue<TSelf>.MinValue or IMinMaxValue<TSelf>.MaxValue instead of overflowing.

public static T SaturatingMul<T>(T left, T right) where T : IBinaryInteger<T>, IMinMaxValue<T>

Multiplies two integers, saturating at IMinMaxValue<TSelf>.MinValue or IMinMaxValue<TSelf>.MaxValue instead of overflowing.