Documentation
¶
Overview ¶
Package mathutil provides mathematical utilities:
- Range: Clamp, Min, Max, MinMax, InRange
- Precision: Round, RoundTo, FloorTo, CeilTo, Truncate
- Statistics: Sum, Mean, Median, Mode, Variance, StdDev, Percentile
- Number theory: GCD, LCM, IsPrime, Factorial, Fibonacci
- Mapping: MapRange, Lerp
Quick start ¶
mathutil.Clamp(15, 0, 10) // 10
mathutil.Round(3.14159, 2) // 3.14
mathutil.Sum([]float64{1, 2, 3}) // 6
mathutil.Mean([]float64{1, 2, 3}) // 2
mathutil.GCD(12, 18) // 6
Index ¶
- func Abs(n int) int
- func CeilTo(x, to float64) float64
- func Clamp[T int | int64 | float32 | float64](value, min, max T) T
- func Cube(x float64) float64
- func Factorial(n int) int
- func Fibonacci(n int) int
- func FloorTo(x, to float64) float64
- func GCD(a, b int) int
- func InRange[T int | int64 | float32 | float64](value, min, max T) bool
- func InvLerp(a, b, value float64) float64
- func IsEven(n int) bool
- func IsInteger(x float64) bool
- func IsOdd(n int) bool
- func IsPowerOfTwo(n int) bool
- func IsPrime(n int) bool
- func LCM(a, b int) int
- func Lerp(a, b, t float64) float64
- func MapRange(value, inMin, inMax, outMin, outMax float64) float64
- func Max[T int | int64 | float32 | float64](a, b T) T
- func MaxSlice[T int | int64 | float32 | float64](s []T) T
- func Mean(s []float64) float64
- func Median(s []float64) float64
- func Min[T int | int64 | float32 | float64](a, b T) T
- func MinMax[T int | int64 | float32 | float64](a, b T) (T, T)
- func MinSlice[T int | int64 | float32 | float64](s []T) T
- func Mode(s []float64) []float64
- func NextPowerOfTwo(n int) int
- func Percentile(s []float64, p float64) float64
- func Quantile(s []float64, q float64) float64
- func Round(x float64, precision int) float64
- func RoundTo(x, to float64) float64
- func SampleStdDev(s []float64) float64
- func SampleVariance(s []float64) float64
- func Sign(x float64) float64
- func Square(x float64) float64
- func StdDev(s []float64) float64
- func Sum(s []float64) float64
- func SumInt(s []int) int
- func Truncate(x float64, precision int) float64
- func Variance(s []float64) float64
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Factorial ¶
Factorial returns n! (n factorial). Panics if n < 0. Returns 1 for n = 0. Note: will overflow for n > 20 (int64).
func Fibonacci ¶
Fibonacci returns the n-th Fibonacci number (0-indexed). F(0)=0, F(1)=1, F(2)=1, F(3)=2, ... Panics if n < 0.
func InvLerp ¶
InvLerp returns the interpolation factor t such that Lerp(a, b, t) = value. Returns 0 if a == b.
func IsPowerOfTwo ¶
IsPowerOfTwo returns true if n is a power of two. Returns false for n <= 0.
func IsPrime ¶
IsPrime returns true if n is a prime number. n must be non-negative. Returns false for n < 2.
func Lerp ¶
Lerp performs linear interpolation between a and b. t=0 returns a, t=1 returns b, t=0.5 returns the midpoint.
func MapRange ¶
MapRange maps a value from one range to another. e.g. MapRange(5, 0, 10, 0, 100) = 50. If input range is zero, returns the output start.
func Mean ¶
Mean returns the arithmetic mean (average) of a float64 slice. Returns NaN for an empty slice.
func Mode ¶
Mode returns the most frequent value(s) in a float64 slice. Returns all values with the highest frequency. Returns an empty slice for an empty input.
func NextPowerOfTwo ¶
NextPowerOfTwo returns the smallest power of two >= n. Returns 1 for n <= 1. Panics if n is too large.
func Percentile ¶
Percentile returns the p-th percentile (0-100) of a float64 slice. Uses linear interpolation between closest ranks. Returns NaN for an empty slice. Panics if p is not in [0, 100].
func Round ¶
Round rounds a float64 to the specified number of decimal places. Uses "round half away from zero" (banker's rounding not used). e.g. Round(3.14159, 2) = 3.14, Round(2.5, 0) = 3.
func RoundTo ¶
RoundTo rounds to the nearest multiple of `to`. e.g. RoundTo(17, 5) = 15, RoundTo(18, 5) = 20.
func SampleStdDev ¶
SampleStdDev returns the sample standard deviation. Returns NaN for slices with fewer than 2 elements.
func SampleVariance ¶
SampleVariance returns the sample variance (Bessel's correction). Returns NaN for slices with fewer than 2 elements.
Types ¶
This section is empty.