Skip to content

Modern Math Clamp

Paul Squires edited this page Oct 19, 2026 · 5 revisions

Home › Math Library › Math Library › Clamp

Clamp

Limits a value to a range: below lo gives lo, above hi gives hi.

Syntax

Clamp( v, lo, hi )

Examples

#import "std/numeric"
using numeric

dim as float64 lo = 0.0, hi = 10.0

print Clamp( 5.0, lo, hi )   '' 5 -- already inside
print Clamp( -3.0, lo, hi )  '' 0 -- pulled up to the floor
print Clamp( 99.0, lo, hi )  '' 10 -- pushed down to the ceiling

'' Reads better than Max( lo, Min( hi, x ) ) and says what it is for

Library: Math Library
Category: Math Library
Documented under: Nothing Becomes A Keyword

Clone this wiki locally