metis.math
Extends the default math library with additional utility functions.
This module re-exports any function defined in math, and so may shadow the
math global.
local math = require "metis.math" print(math.min(0, 10))
| clamp(value, min, max) | Clamp a value within a range. |
|---|---|
| wrap(value, min, max) | Wrap a value around a range. |
- clamp(value, min, max)Source
Clamp a value within a range. This limits the given
valueto be betweenminandmax(inclusive).Parameters
- value
numberThe value to clamp. - min
numberThe lower bound of the permitted values. - max
numberThe upper bound of the permitted values.
Returns
numberThe clamped value.
Usage
Clamp a value between 3 and 7.
local clamp = require "metis.math".clamp for i = 1, 10 do io.write(clamp(i, 3, 7), " ") end -- 3 3 3 4 5 6 7 7 7 7
- value
- wrap(value, min, max)Source
Wrap a value around a range. This is equivalent to the modulo operator (
%), but for any lower and upper bound.Parameters
- value
numberThe value to wrap. - min
numberThe lower bound of the permitted values. - max
numberThe upper bound of the permitted values.
Usage
Wrap a value between 3 and 7.
local wrap = require "metis.math".wrap for i = 1, 10 do io.write(wrap(i, 3, 7), " ") end -- 6 7 3 4 5 6 7 3 4 5
- value