Otter Packs Otter Upstream Contract & Behavioral Tests

math-utils Pack

Essential mathematical helpers: clamp, lerp, mapRange, minVal, maxVal, calcAverage, calcSum, calcPercentage, roundVal, roundUpVal, and roundDownVal.

ID: otter-official/math-utils Version: 1.0.1 License: MIT Framework: Otter >=1.0.1

Schema v2 Operation Contract (11 operations)

Scoping Prefix: _mu_

Schema v2 Pack exposing 11 structurally AST-verified Otter 1.0.1 operations with isolated prefix '_mu_'.

Operation SignatureReturnsSide EffectsError Mode
clamp(value: number, minLimit: number, maxLimit: number) number none returns-fallback
lerp(startVal: number, endVal: number, t: number) number none returns-fallback
mapRange(val: number, inMin: number, inMax: number, outMin: number, outMax: number) number none returns-fallback
minVal(left: number, right: number) number none returns-fallback
maxVal(left: number, right: number) number none returns-fallback
calcAverage(items: list) number none returns-fallback
calcSum(items: list) number none returns-fallback
calcPercentage(part: number, whole: number) number none returns-fallback
roundVal(val: number) number none returns-fallback
roundUpVal(val: number) number none returns-fallback
roundDownVal(val: number) number none returns-fallback

Source Code (4 files)

src/main.ot · otter
# math-utils - Otter Reusable Math Library
# Provides clamp, lerp, mapRange, minVal, maxVal, calcAverage, calcSum, calcPercentage, roundVal, roundUpVal, roundDownVal
# Internal variables use '_mu_' prefix to prevent collisions with caller/outer variables.

to clamp value minLimit maxLimit
    _mu_low is minLimit
    _mu_high is maxLimit
    if _mu_low is greater than _mu_high
        _mu_temp is _mu_low
        _mu_low is _mu_high
        _mu_high is _mu_temp
    .
    if value is less than _mu_low
        return _mu_low
    .
    if value is greater than _mu_high
        return _mu_high
    .
    return value
.

to lerp startVal endVal t
    _mu_diff is endVal minus startVal
    _mu_offset is _mu_diff times t
    return startVal plus _mu_offset
.

to mapRange val inMin inMax outMin outMax
    _mu_inSpan is inMax minus inMin
    if _mu_inSpan is 0
        return outMin
    .
    _mu_outSpan is outMax minus outMin
    _mu_valDelta is val minus inMin
    _mu_fraction is _mu_valDelta divided by _mu_inSpan
    _mu_scaled is _mu_fraction times _mu_outSpan
    return outMin plus _mu_scaled
.

to minVal left right
    if left is less than right
        return left
    .
    return right
.

to maxVal left right
    if left is greater than right
        return left
    .
    return right
.

to calcAverage items
    _mu_total is 0
    _mu_count is 0
    for each _mu_it in items
        _mu_total is _mu_total plus _mu_it
        _mu_count is _mu_count plus 1
    .
    if _mu_count is 0
        return 0
    .
    return _mu_total divided by _mu_count
.

to calcSum items
    _mu_total is 0
    for each _mu_it in items
        _mu_total is _mu_total plus _mu_it
    .
    return _mu_total
.

to calcPercentage part whole
    if whole is 0
        return 0
    .
    _mu_fraction is part divided by whole
    return _mu_fraction times 100
.

to roundVal val
    return round of val
.

to roundUpVal val
    return round up of val
.

to roundDownVal val
    return round down of val
.