Documentation
¶
Overview ¶
Package math provides numeric helpers.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Clamp ¶
Clamp restricts `v` to the [`lo`, `hi`] range. NaN clamps to `lo`; infinities clamp to the nearest bound. Unlike `min(max(v, lo), hi)`, NaN does not propagate.
Example ¶
package main
import (
"fmt"
xmath "github.com/gechr/x/math"
)
func main() {
fmt.Println(xmath.Clamp(5, 0, 10))
fmt.Println(xmath.Clamp(-3, 0, 10))
fmt.Println(xmath.Clamp(42, 0, 10))
}
Output: 5 0 10
Example (Nan) ¶
Unlike min(max(v, lo), hi), NaN does not propagate - it clamps to `lo`.
package main
import (
"fmt"
"math"
xmath "github.com/gechr/x/math"
)
func main() {
fmt.Println(xmath.Clamp(math.NaN(), 1.0, 2.0))
fmt.Println(min(max(math.NaN(), 1.0), 2.0))
}
Output: 1 NaN
Example (Strings) ¶
Clamp works with any ordered type, including strings.
package main
import (
"fmt"
xmath "github.com/gechr/x/math"
)
func main() {
fmt.Println(xmath.Clamp("a", "b", "d"))
fmt.Println(xmath.Clamp("c", "b", "d"))
fmt.Println(xmath.Clamp("z", "b", "d"))
}
Output: b c d
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.