blmath

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2025 License: MIT Imports: 2 Imported by: 3

Documentation

Overview

Package blmath contains numeric and math related functions.

Index

Constants

View Source
const HalfPi = math.Pi / 2

HalfPi pi / 2

View Source
const Tau = math.Pi * 2

Tau 2 pi

View Source
const TwoPi = math.Pi * 2

TwoPi 2 pi

Variables

This section is empty.

Functions

func Abs

func Abs[T Number](num T) T

Abs is a generic absolute value function.

func Clamp

func Clamp(value float64, min float64, max float64) float64

Clamp enforces a value does not go beyond a min/max range.

func ComplexImagAbs

func ComplexImagAbs(z complex128) complex128

ComplexImagAbs returns a complex number with the real component and the abolute value of the imaginary component. Useful for certain types of fractals, such as "duck fractals"

func ComplexMagnitude

func ComplexMagnitude(z complex128) float64

ComplexMagnitude returns the magnitude of a complex number

func CosRange

func CosRange(angle float64, min float64, max float64) float64

CosRange returns the cos of an angle mapped to a min/max range.

func DegToRad

func DegToRad(d float64) float64

DegToRad converts a degree value to radians.

func Difference

func Difference(a, b float64) float64

Difference returns the absolute value of the difference between two numbers.

func DigRoot

func DigRoot(value int) int

DigRoot returns the digital root of a number, which is the sum of the numbers digits, repeated until it yields a single digit.

func Equalish

func Equalish(a float64, b float64, delta float64) bool

Equalish returns whether the two values are approximately equal.

func Fract

func Fract(n float64) float64

Fract returns the fractional part of a floating point number.

func GCD

func GCD(a, b int) int

GCD returns the greatest common denominator of two integers.

func Gamma

func Gamma(val, gamma float64) float64

Gamma increases/decreases the given value by an amount specified in gamma. Usually val is a pixel brightness value from 0.0 - 1.0. gamma of 1.0 makes no change. Higher is brighter, lower is darker.

func LCM

func LCM(x, y int) int

LCM returns the least common multiple of two integers.

func Lerp

func Lerp(t float64, min float64, max float64) float64

Lerp is linear interpolation within a min/max range.

func LoopCos

func LoopCos(t, min, max float64) float64

LoopCos is the same as LoopSin but out of phase. In this version the values will go from the mid point, to max to min and back to mid as t goes from 0 to 1. Yes, yes, LoopSin uses cos, and LoopCos uses sin. Deal with it.

func LoopSin

func LoopSin(t, min, max float64) float64

LoopSin maps a normal value to min and max values with a sine wave. In this version the values will go from min, to max and back as t goes from 0 to 1.

func LoopSquare

func LoopSquare(t, min, max float64) float64

LoopSquare maps a normal value to min and max values. It alternates between the min and max, max for the first part of the cycle, then min.

func LoopTri

func LoopTri(t, min, max float64) float64

LoopTri maps a normal value to min and max values linearly. In this version the values will go from min, to max and back as t goes from 0 to 1. Similar to LoopSin, but moves at a constant speed.

func Map

func Map(srcValue float64, srcMin float64, srcMax float64, dstMin float64, dstMax float64) float64

Map maps a value within one min/max range to a value within another range.

func MapExpLin

func MapExpLin(srcValue, srcMin, srcMax, dstMin, dstMax float64) float64

MapExpLin maps a value within an exponential min/max range to another linear range. min and max values will be swapped if min is greater than max. The resulting srcMin value cannot be 0.

func MapFloats

func MapFloats(list []float64, minVal, maxVal float64) []float64

MapFloats maps a list of floats to fall within a min/max range It returns the normalized list and does not change the original.

func MapLinExp

func MapLinExp(srcValue, srcMin, srcMax, dstMin, dstMax float64) float64

MapLinExp maps a value within a linear min/max range to another exponential range. min and max values will be swapped if min is greater than max. The resulting dstMin value cannot be 0.

func Max

func Max[T Number](a, b T) T

Max is a generic max value function

func Min

func Min[T Number](a, b T) T

Min is a generic min value function

func MinMaxFloats

func MinMaxFloats(list []float64) (float64, float64)

MinMaxFloats returns the minimum and maximum values in a list of floats.

func ModPos

func ModPos(a, b float64) float64

ModPos computes a % b for float64, and ensures the result is positive

func ModPosInt

func ModPosInt(a, b int) int

ModPosInt computes a % b for int, and ensures the result is positive

func Norm

func Norm(value float64, min float64, max float64) float64

Norm returns a normalized value in a min/max range.

func NormalizeFloats

func NormalizeFloats(list []float64) []float64

NormalizeFloats normalizes a list of floats to fall within a 0.0 to 1.0 range. It returns the normalized list and does not change the original.

func Quantize

func Quantize(val, min, max float64, steps int) float64

Quantize quantizes a value in a range into a number of discrete steps in that range.

func RadToDeg

func RadToDeg(r float64) float64

RadToDeg converts a radian value to degrees.

func RoundTo

func RoundTo(value float64, decimal int) float64

RoundTo rounds a number to the nearest decimal value.

func RoundToNearest

func RoundToNearest(value float64, mult float64) float64

RoundToNearest rounds a number to the nearest multiple of a value.

func Simplify

func Simplify(x, y int) (int, int)

Simplify reduces an int/int fraction to its simplest form.

func SinRange

func SinRange(angle float64, min float64, max float64) float64

SinRange returns the sin of an angle mapped to a min/max range.

func Wrap

func Wrap(value float64, min float64, max float64) float64

Wrap wraps a value around so it remains between min (inclusive) and max (exclusive).

func WrapPi

func WrapPi(value float64) float64

WrapPi wraps a number to be within -Pi (inclusive) and +Pi (exclusive).

func WrapTau

func WrapTau(value float64) float64

WrapTau wraps a number to be within 0 (inclusive) to 2 * Pi (exclusive).

Types

type Number

type Number interface {
	int | int64 | float32 | float64
}

Number can be an int or float

Jump to

Keyboard shortcuts

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