Table of Contents
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
Single Numbers
Six functions work on single numbers, the speeds, times and amounts a game interpolates and limits. They take reals of any kind and return what Lisp's arithmetic makes of them: single floats of single floats, doubles of doubles, and exact rationals of rationals. They are inline, so that a double they return need not be boxed, and allocate nothing for single floats.
| Function | Returns |
|---|---|
(clamp value low high) |
value kept from low to high: low itself if it is less, high itself if it is more. |
(lerp a b s) |
The fraction s of the way from a to b: a at 0, b at 1, and beyond them for s outside 0 to 1. |
(inverse-lerp a b value) |
The fraction of the way from a to b that value is, as lerp takes it. |
(remap value from-low from-high to-low to-high) |
value carried from one range to another, keeping its place in it. |
(smoothstep edge0 edge1 x) |
0 at or before edge0, 1 at or after edge1, and between them a curve that leaves 0 and reaches 1 flat, 3t² − 2t³, as GLSL's smoothstep. |
(move-toward from to delta) |
from moved delta toward to, but no further than to, as Godot's move_toward. A negative delta moves away. |
(gm:clamp 15 0 10) ; => 10
(gm:lerp 0.0 10.0 0.25) ; => 2.5
(gm:lerp 0 10 1/4) ; => 5/2
(gm:inverse-lerp 0.0 10.0 2.5) ; => 0.25
(gm:remap 0.5 0.0 1.0 -100.0 100.0) ; => 0.0
(gm:smoothstep 0.0 1.0 0.25) ; => 0.15625
(gm:move-toward 4.8 5.0 0.5) ; => 5.0
edge0 may be more than edge1: the curve then rises as x falls.
clamp signals gmath-error if low is more than high, and
inverse-lerp, remap and smoothstep if the two ends they measure from
are equal, as no fraction then says where a value is between them. See
Errors.
For vectors, vec3-lerp, vec3-clamp-length and vec3-move-toward do
the same; see Vectors.