mathutil

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 17, 2026 License: MIT Imports: 3 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs(n int) int

Abs returns the absolute value of an integer.

func CeilTo

func CeilTo(x, to float64) float64

CeilTo rounds up to the nearest multiple of `to`.

func Clamp

func Clamp[T int | int64 | float32 | float64](value, min, max T) T

Clamp returns value clamped to [min, max]. Panics if min > max.

func Cube

func Cube(x float64) float64

Cube returns x^3.

func Factorial

func Factorial(n int) int

Factorial returns n! (n factorial). Panics if n < 0. Returns 1 for n = 0. Note: will overflow for n > 20 (int64).

func Fibonacci

func Fibonacci(n int) int

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 FloorTo

func FloorTo(x, to float64) float64

FloorTo rounds down to the nearest multiple of `to`.

func GCD

func GCD(a, b int) int

GCD returns the greatest common divisor of a and b. GCD(0, 0) returns 0.

func InRange

func InRange[T int | int64 | float32 | float64](value, min, max T) bool

InRange returns true if value is in [min, max] (inclusive).

func InvLerp

func InvLerp(a, b, value float64) float64

InvLerp returns the interpolation factor t such that Lerp(a, b, t) = value. Returns 0 if a == b.

func IsEven

func IsEven(n int) bool

IsEven returns true if n is even.

func IsInteger

func IsInteger(x float64) bool

IsInteger returns true if x is a whole number.

func IsOdd

func IsOdd(n int) bool

IsOdd returns true if n is odd.

func IsPowerOfTwo

func IsPowerOfTwo(n int) bool

IsPowerOfTwo returns true if n is a power of two. Returns false for n <= 0.

func IsPrime

func IsPrime(n int) bool

IsPrime returns true if n is a prime number. n must be non-negative. Returns false for n < 2.

func LCM

func LCM(a, b int) int

LCM returns the least common multiple of a and b. Returns 0 if either a or b is 0.

func Lerp

func Lerp(a, b, t float64) float64

Lerp performs linear interpolation between a and b. t=0 returns a, t=1 returns b, t=0.5 returns the midpoint.

func MapRange

func MapRange(value, inMin, inMax, outMin, outMax float64) float64

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 Max

func Max[T int | int64 | float32 | float64](a, b T) T

Max returns the larger of two values.

func MaxSlice

func MaxSlice[T int | int64 | float32 | float64](s []T) T

MaxSlice returns the maximum value in a slice. Panics if the slice is empty.

func Mean

func Mean(s []float64) float64

Mean returns the arithmetic mean (average) of a float64 slice. Returns NaN for an empty slice.

func Median

func Median(s []float64) float64

Median returns the median of a float64 slice. Returns NaN for an empty slice.

func Min

func Min[T int | int64 | float32 | float64](a, b T) T

Min returns the smaller of two values.

func MinMax

func MinMax[T int | int64 | float32 | float64](a, b T) (T, T)

MinMax returns (min, max) of two values.

func MinSlice

func MinSlice[T int | int64 | float32 | float64](s []T) T

MinSlice returns the minimum value in a slice. Panics if the slice is empty.

func Mode

func Mode(s []float64) []float64

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

func NextPowerOfTwo(n int) int

NextPowerOfTwo returns the smallest power of two >= n. Returns 1 for n <= 1. Panics if n is too large.

func Percentile

func Percentile(s []float64, p float64) float64

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 Quantile

func Quantile(s []float64, q float64) float64

Quantile returns the q-th quantile (0-1) of a float64 slice.

func Round

func Round(x float64, precision int) float64

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

func RoundTo(x, to float64) float64

RoundTo rounds to the nearest multiple of `to`. e.g. RoundTo(17, 5) = 15, RoundTo(18, 5) = 20.

func SampleStdDev

func SampleStdDev(s []float64) float64

SampleStdDev returns the sample standard deviation. Returns NaN for slices with fewer than 2 elements.

func SampleVariance

func SampleVariance(s []float64) float64

SampleVariance returns the sample variance (Bessel's correction). Returns NaN for slices with fewer than 2 elements.

func Sign

func Sign(x float64) float64

Sign returns -1, 0, or 1 based on the sign of x.

func Square

func Square(x float64) float64

Square returns x^2.

func StdDev

func StdDev(s []float64) float64

StdDev returns the population standard deviation. Returns NaN for an empty slice.

func Sum

func Sum(s []float64) float64

Sum returns the sum of a float64 slice.

func SumInt

func SumInt(s []int) int

SumInt returns the sum of an int slice.

func Truncate

func Truncate(x float64, precision int) float64

Truncate truncates to the specified number of decimal places. e.g. Truncate(3.9999, 2) = 3.99.

func Variance

func Variance(s []float64) float64

Variance returns the population variance of a float64 slice. Returns NaN for an empty slice.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL