Header menu logo TDesu.FSharp

Numeric Module

Generic numeric helpers: clamping, linear interpolation, and inclusive-range checks. Built on F#'s built-in arithmetic/comparison constraints, so they work for float, decimal, custom numeric types — anything with the required operators — not just int.

Functions and values

Function or value Description

Numeric.clamp lo hi value

Full Usage: Numeric.clamp lo hi value

Parameters:
    lo : 'a - Inclusive lower bound.
    hi : 'a - Inclusive upper bound.
    value : 'a - Value to clamp.

Returns: 'a
Modifiers: inline
Type parameters: 'a

Restricts value to the inclusive range [lo, hi].

Total: never throws. If lo is greater than hi the two comparisons below still run in order, so the result is always exactly lo or hi (never a value strictly between them, and never the unclamped value): anything less than lo clamps to lo, everything else clamps to hi.

lo : 'a

Inclusive lower bound.

hi : 'a

Inclusive upper bound.

value : 'a

Value to clamp.

Returns: 'a
Example

 Numeric.clamp 0 10 15   // 10
 Numeric.clamp 0 10 -3   // 0
 Numeric.clamp 0 10 4    // 4
 Numeric.clamp 10 1 5    // 10 — degenerate lo > hi, see remarks

Numeric.inverseLerp a b value

Full Usage: Numeric.inverseLerp a b value

Parameters:
    a : ^a - Value at t = 0.
    b : ^a - Value at t = 1.
    value : ^a - Value to locate between a and b.

Returns: ^a
Modifiers: inline
Type parameters: ^a, ^a

Inverse of lerp: finds the fraction t at which value occurs between a and b.

Total for floating-point types: when a equals b the divisor is zero and the result is NaN or ±Infinity per IEEE 754, never an exception. For integral types, a zero divisor throws DivideByZeroException, matching that type's own division semantics — this function adds no special-casing on top of it.

a : ^a

Value at t = 0.

b : ^a

Value at t = 1.

value : ^a

Value to locate between a and b.

Returns: ^a
Example

 Numeric.inverseLerp 0.0 10.0 5.0    // 0.5
 Numeric.inverseLerp 0.0 10.0 15.0   // 1.5 — outside [0, 1], value is past b

Numeric.isBetween lo hi value

Full Usage: Numeric.isBetween lo hi value

Parameters:
    lo : 'a - Inclusive lower bound.
    hi : 'a - Inclusive upper bound.
    value : 'a - Value to test.

Returns: bool
Modifiers: inline
Type parameters: 'a

Returns true if value lies within [lo, hi], inclusive on both ends.

If lo is greater than hi the range is empty, so this always returns false.

lo : 'a

Inclusive lower bound.

hi : 'a

Inclusive upper bound.

value : 'a

Value to test.

Returns: bool
Example

 Numeric.isBetween 0 10 0     // true
 Numeric.isBetween 0 10 10    // true
 Numeric.isBetween 0 10 11    // false

Numeric.lerp a b t

Full Usage: Numeric.lerp a b t

Parameters:
    a : ^a - Value at t = 0.
    b : ^a - Value at t = 1.
    t : ^a - Interpolation fraction.

Returns: ^a
Modifiers: inline
Type parameters: ^a, ^a, ^b

Linearly interpolates between a and b by fraction t.

Unclamped: t is not restricted to [0, 1]. t = 0 returns a, t = 1 returns b, and values outside [0, 1] extrapolate linearly past a or b instead of clamping. Pipe t through clamp first if clamped interpolation is what you want.

a : ^a

Value at t = 0.

b : ^a

Value at t = 1.

t : ^a

Interpolation fraction.

Returns: ^a
Example

 Numeric.lerp 0.0 10.0 0.0    // 0.0
 Numeric.lerp 0.0 10.0 1.0    // 10.0
 Numeric.lerp 0.0 10.0 0.5    // 5.0
 Numeric.lerp 0.0 10.0 2.0    // 20.0 — extrapolates past b

Numeric.one

Full Usage: Numeric.one

Returns: ^a
Modifiers: inline
Type parameters: ^a

The multiplicative identity for numeric type 'a, via GenericOne.

Returns: ^a
Example

 Numeric.one<int>      // 1
 Numeric.one<float>    // 1.0
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
Multiple items
val float: value: 'T -> float (requires member op_Explicit)

--------------------
type float = System.Double

--------------------
type float<'Measure> = float

Numeric.zero

Full Usage: Numeric.zero

Returns: ^a
Modifiers: inline
Type parameters: ^a

The additive identity for numeric type 'a, via GenericZero.

Returns: ^a
Example

 Numeric.zero<int>      // 0
 Numeric.zero<float>    // 0.0
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
Multiple items
val float: value: 'T -> float (requires member op_Explicit)

--------------------
type float = System.Double

--------------------
type float<'Measure> = float

Type something to start searching.