PICO-8 Wiki
mid( first, second, third )
Returns the middle of three numbers. Also useful for clamping.
The first number.

The second number.

The third number.

This function is designed simply to give you the middle value out of three.

However, one of its best uses is frequently overlooked: it can clamp a value to a range.

If you call mid(min_val,val_to_clamp,max_val), it acts exactly like the similarly-formatted clamp() function in other languages. The result will be min_val if val is too low, max_val if val is too high, and val itself if it is in range.


print(mid(8, 2, 4))           -- 4

print(mid(-3.5, -3.4, -3.6))  -- -3.5

print(mid(6, 6, 8))           -- 6

-- clamp to 0..1
print(mid(0, v, 1))

-- sort three values
v0 = min(min(a,b),c)
v1 = mid(a,b,c)
v2 = max(max(a,b),c)

See also[]