mathutils

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package mathutils - because apparently I needed MORE math in my life Contains basic math functions and some black magic optimization algorithms that I probably shouldn't understand but somehow work.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs[T ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~float32 | ~float64](x T) T

Abs returns absolute value because negative feelings aren't welcome here

func Average

func Average[T ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64](slice []T) (float64, bool)

Average calculates the mean because life isn't always extreme

func Clamp

func Clamp[T ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64](value, min, max T) T

Clamp constrains a value because some things need boundaries (unlike my caffeine intake)

func DegToRad

func DegToRad(degrees float64) float64

DegToRad converts degrees to radians (because mathematicians hate convenience)

func Factorial

func Factorial(n int) int

Factorial calculates n! (the mathematical equivalent of exponential anxiety)

func FastCos

func FastCos(x float64) float64

FastCos - Like FastSin but shifted because trigonometry is weird

func FastInvSqrt

func FastInvSqrt(x float32) float32

FastInvSqrt - The legendary Quake inverse square root This is basically black magic from the 90s that still makes me question reality

func FastPow

func FastPow(base float64, exp int) float64

FastPow - Binary exponentiation because math.Pow is for weaklings

func FastSin

func FastSin(x float64) float64

FastSin - Taylor series approximation because precision is overrated

func GCD

func GCD(a, b int) int

GCD - Greatest Common Divisor using Euclidean algorithm (Euclid was clearly showing off but hey, it works)

func IsInf

func IsInf(x float64, sign int) bool

IsInf checks if a number went to infinity and beyond

func IsNaN

func IsNaN(x float64) bool

IsNaN checks if a float is "Not a Number" (like my work-life balance)

func IsPowerOfTwo

func IsPowerOfTwo(x int) bool

IsPowerOfTwo checks if a number is a power of 2 using bit wizardry

func IsPrime

func IsPrime(n int) bool

IsPrime checks if a number is prime using trial division (Because even numbers need to feel special sometimes)

func LCM

func LCM(a, b int) int

LCM - Least Common Multiple (for when you need the smallest shared nightmare)

func Lerp

func Lerp(a, b, t float64) float64

Lerp - Linear interpolation (or "how to get from A to B without drama")

func Max

func Max[T ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64](a, b T) T

Max because everyone wants to be the winner

func MaxSlice

func MaxSlice[T ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64](slice []T) (T, bool)

MaxSlice finds the biggest number (unlike my bank account)

func Min

func Min[T ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64](a, b T) T

Min because someone has to lose

func MinSlice

func MinSlice[T ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64](slice []T) (T, bool)

MinSlice finds the smallest number in a slice (like my self-esteem in code reviews)

func NextPowerOfTwo

func NextPowerOfTwo(x int) int

NextPowerOfTwo finds the next power of 2 (useful for memory allocation and existential dread)

func PopCount

func PopCount(x uint64) int

PopCount returns the number of set bits (because counting 1s is apparently important)

func RadToDeg

func RadToDeg(radians float64) float64

RadToDeg converts radians to degrees (for us normal humans)

func Round

func Round(x float64) float64

Round rounds to nearest integer (because close enough is good enough)

func RoundToDecimal

func RoundToDecimal(x float64, decimals int) float64

RoundToDecimal rounds to n decimal places (for when you need to be precise-ish)

func SmoothStep

func SmoothStep(edge0, edge1, x float64) float64

SmoothStep - Smooth Hermite interpolation for when linear is too boring

func Sum

func Sum[T ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64](slice []T) T

Sum adds up all the numbers (and my regrets)

Types

This section is empty.

Jump to

Keyboard shortcuts

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