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.
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 Signature | Returns | Side Effects | Error 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)
# 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
.
{
"schemaVersion": 2,
"name": "math-utils",
"publisher": "otter-official",
"version": "1.0.1",
"kind": "pack",
"description": "Essential mathematical helpers: clamp, lerp, mapRange, minVal, maxVal, calcAverage, calcSum, calcPercentage, roundVal, roundUpVal, and roundDownVal.",
"entry": "src/main.ot",
"license": "MIT",
"requiresOtter": ">=1.0.1",
"platforms": [
"console",
"desktop",
"web"
],
"variablePrefix": "_mu_",
"provides": {
"capabilities": [
"math.computation",
"math.interpolation",
"math.statistics"
],
"operations": [
{
"name": "clamp",
"description": "Restricts a numeric value to stay within minLimit and maxLimit.",
"parameters": [
{ "name": "value", "type": "number" },
{ "name": "minLimit", "type": "number" },
{ "name": "maxLimit", "type": "number" }
],
"returns": { "type": "number" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "lerp",
"description": "Linearly interpolates between startVal and endVal by fraction t.",
"parameters": [
{ "name": "startVal", "type": "number" },
{ "name": "endVal", "type": "number" },
{ "name": "t", "type": "number" }
],
"returns": { "type": "number" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "mapRange",
"description": "Maps a value from input range [inMin, inMax] to output range [outMin, outMax].",
"parameters": [
{ "name": "val", "type": "number" },
{ "name": "inMin", "type": "number" },
{ "name": "inMax", "type": "number" },
{ "name": "outMin", "type": "number" },
{ "name": "outMax", "type": "number" }
],
"returns": { "type": "number" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "minVal",
"description": "Returns the lesser of left and right.",
"parameters": [
{ "name": "left", "type": "number" },
{ "name": "right", "type": "number" }
],
"returns": { "type": "number" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "maxVal",
"description": "Returns the greater of left and right.",
"parameters": [
{ "name": "left", "type": "number" },
{ "name": "right", "type": "number" }
],
"returns": { "type": "number" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "calcAverage",
"description": "Calculates the arithmetic mean of a list of numbers.",
"parameters": [
{ "name": "items", "type": "list" }
],
"returns": { "type": "number" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "calcSum",
"description": "Calculates the sum of a list of numbers.",
"parameters": [
{ "name": "items", "type": "list" }
],
"returns": { "type": "number" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "calcPercentage",
"description": "Calculates percentage of part over whole (0 to 100).",
"parameters": [
{ "name": "part", "type": "number" },
{ "name": "whole", "type": "number" }
],
"returns": { "type": "number" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "roundVal",
"description": "Rounds a number to the nearest integer.",
"parameters": [
{ "name": "val", "type": "number" }
],
"returns": { "type": "number" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "roundUpVal",
"description": "Rounds a number up (ceiling).",
"parameters": [
{ "name": "val", "type": "number" }
],
"returns": { "type": "number" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "roundDownVal",
"description": "Rounds a number down (floor).",
"parameters": [
{ "name": "val", "type": "number" }
],
"returns": { "type": "number" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
}
]
},
"requires": {
"capabilities": [],
"packages": {}
},
"dependencies": {},
"tags": [
"math",
"numbers",
"clamp",
"lerp",
"range",
"interpolation",
"average",
"statistics"
],
"examples": [
"examples/demo.ot"
]
}
# Tests for math-utils (Strict Behavioral Assertions)
use "../src/main.ot"
say "Running math-utils behavioral tests..."
# 1. Clamp inside range
r1 is clamp 50 0 100
if r1 is not 50
fail with "clamp inside range failed: expected 50"
.
# 2. Clamp upper bound
r2 is clamp 150 0 100
if r2 is not 100
fail with "clamp upper bound failed: expected 100"
.
# 3. Clamp lower bound
negVal is 0 minus 20
r3 is clamp negVal 0 100
if r3 is not 0
fail with "clamp lower bound failed: expected 0"
.
# 4. Clamp with inverted limits (invalid range: min > max)
r4 is clamp 50 100 0
if r4 is not 50
fail with "clamp inverted range failed: expected 50"
.
# 5. Lerp quarter
r5 is lerp 0 100 0.25
if r5 is not 25
fail with "lerp quarter failed: expected 25"
.
# 6. Lerp zero and one
r6a is lerp 10 50 0
if r6a is not 10
fail with "lerp at 0 failed: expected 10"
.
r6b is lerp 10 50 1
if r6b is not 50
fail with "lerp at 1 failed: expected 50"
.
# 7. Map range normal
r7 is mapRange 5 0 10 0 100
if r7 is not 50
fail with "mapRange normal failed: expected 50"
.
# 8. Map range with 0 input span (division by zero safeguard)
r8 is mapRange 5 10 10 0 100
if r8 is not 0
fail with "mapRange zero span safeguard failed: expected outMin 0"
.
# 9. Min / Max
r9 is minVal 12 34
if r9 is not 12
fail with "minVal failed: expected 12"
.
r10 is maxVal 12 34
if r10 is not 34
fail with "maxVal failed: expected 34"
.
# 10. CalcSum and CalcAverage with values
nums are
10
20
30
.
sumVal is calcSum nums
if sumVal is not 60
fail with "calcSum failed: expected 60"
.
avgVal is calcAverage nums
if avgVal is not 20
fail with "calcAverage failed: expected 20"
.
# 11. CalcAverage with empty list
emptyList are empty
emptyAvg is calcAverage emptyList
if emptyAvg is not 0
fail with "calcAverage on empty list failed: expected 0"
.
# 12. Percentage normal & division by zero
p1 is calcPercentage 25 100
if p1 is not 25
fail with "calcPercentage normal failed: expected 25"
.
pZero is calcPercentage 25 0
if pZero is not 0
fail with "calcPercentage division by zero safeguard failed: expected 0"
.
# 13. Rounding helpers
roundNorm is roundVal 3.6
if roundNorm is not 4
fail with "roundVal failed: expected 4"
.
roundUp is roundUpVal 3.2
if roundUp is not 4
fail with "roundUpVal failed: expected 4"
.
roundDown is roundDownVal 3.8
if roundDown is not 3
fail with "roundDownVal failed: expected 3"
.
say "All math-utils behavioral tests passed successfully!"
# math-utils Demo
use "../src/main.ot"
say "=== math-utils Demo ==="
say "Clamp 150 to [0, 100]:" clamp 150 0 100
say "Lerp 0 to 200 at 0.5:" lerp 0 200 0.5
say "Map 75 from [0, 100] to [0, 1000]:" mapRange 75 0 100 0 1000
say "Min of 45 and 99:" minVal 45 99
say "Max of 45 and 99:" maxVal 45 99
scores are
85
92
78
95
.
say "Total sum:" calcSum scores
say "Average score:" calcAverage scores
say "Percentage (45 / 60):" calcPercentage 45 60 "%"
say "Round 7.4:" roundVal 7.4
say "Round up 7.2:" roundUpVal 7.2
say "Round down 7.8:" roundDownVal 7.8
Installation & Usage
otter pack install otter-official/math-utils
otter pack verify --contracts
use ".otter/packages/otter-official/math-utils/1.0.1/src/main.ot" # Inspect exported operation contracts: otter pack contract otter-official/math-utils