math

package module
v0.0.1-alpha.4 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2025 License: Unlicense Imports: 2 Imported by: 4

README

Go Generic Math Library

A drop‑in replacement for Go's standard math package with generic wrappers that eliminate manual type conversions. This library also includes additional user-defined functions and types to extend functionality.

Features

  • Generic Functions: Automatically wraps standard math functions to work with a variety of numeric types.
  • Automatic Code Generation: Use go:generate to produce core files (core_functions.go, core_consts.go, core_vars.go, and core_types.go) that mirror the standard library.
  • Extended Utilities: Extra functions such as Blend, Avg, Clamp, Delta, Wrap, FormatNumber, and more.
  • User Extensible: Easily fork, modify, and extend the library to suit your needs.

Installation

Clone the repository:

git clone https://github.com/toxyl/math.git
cd math

Code Generation

The library uses go:generate to automatically generate core files. To generate these files, run:

go generate generate.go

This will create:

  • core_functions.go
  • core_consts.go
  • core_vars.go
  • core_types.go

Usage Example

package main

import (
	"fmt"
	"github.com/toxyl/math"
)

func main() {
	// Use a generic math function without manual type casting.
	fmt.Println(math.Max(0.0, uint(1)))

	// Use an extended utility function.
	fmt.Println(math.Blend(10, 20, 0.25))
}

License

This project is released under the UNLICENSE. Feel free to fork, modify, and extend the library according to your needs.

Happy coding!

Documentation

Index

Constants

View Source
const (
	E                      = math.E
	Pi                     = math.Pi
	Phi                    = math.Phi
	Sqrt2                  = math.Sqrt2
	SqrtE                  = math.SqrtE
	SqrtPi                 = math.SqrtPi
	SqrtPhi                = math.SqrtPhi
	Ln2                    = math.Ln2
	Log2E                  = math.Log2E
	Ln10                   = math.Ln10
	Log10E                 = math.Log10E
	MaxFloat32             = math.MaxFloat32
	SmallestNonzeroFloat32 = math.SmallestNonzeroFloat32
	MaxFloat64             = math.MaxFloat64
	SmallestNonzeroFloat64 = math.SmallestNonzeroFloat64
	MaxInt                 = math.MaxInt
	MinInt                 = math.MinInt
	MaxInt8                = math.MaxInt8
	MinInt8                = math.MinInt8
	MaxInt16               = math.MaxInt16
	MinInt16               = math.MinInt16
	MaxInt32               = math.MaxInt32
	MinInt32               = math.MinInt32
	MaxInt64               = math.MaxInt64
	MinInt64               = math.MinInt64
	MaxUint                = math.MaxUint
	MaxUint8               = math.MaxUint8
	MaxUint16              = math.MaxUint16
	MaxUint32              = math.MaxUint32
	MaxUint64              = math.MaxUint64
)

Core constants: re-exported from the standard math package.

View Source
const (
	Tau = 2 * Pi
)

Tau is 2 * Pi.

Variables

View Source
var (
	InfMin = math.Inf(-1)
	InfMax = math.Inf(1)
)

User-defined variables.

View Source
var Float32bits = math.Float32bits

Direct alias.

View Source
var Float32frombits = math.Float32frombits

Direct alias.

View Source
var Float64bits = math.Float64bits

Direct alias.

View Source
var Float64frombits = math.Float64frombits

Direct alias.

View Source
var Frexp = math.Frexp

Direct alias.

View Source
var Ilogb = math.Ilogb

Direct alias.

View Source
var Inf = math.Inf

Direct alias.

View Source
var IsInf = math.IsInf

Direct alias.

View Source
var IsNaN = math.IsNaN

Direct alias.

View Source
var Jn = math.Jn

Direct alias.

View Source
var Ldexp = math.Ldexp

Direct alias.

View Source
var Lgamma = math.Lgamma

Direct alias.

View Source
var Modf = math.Modf

Direct alias.

View Source
var Nextafter32 = math.Nextafter32

Direct alias.

View Source
var Pow10 = math.Pow10

Direct alias.

View Source
var Signbit = math.Signbit

Direct alias.

View Source
var Sincos = math.Sincos

Direct alias.

View Source
var Yn = math.Yn

Direct alias.

Functions

func Abs

func Abs[N Number](x N) N

func Acos

func Acos[N Number](x N) N

func Acosh

func Acosh[N Number](x N) N

func Add

func Add[N Number](x, y N) N

Add returns the sum of x and y.

func ApproxEqual

func ApproxEqual[N Number](x, y N, tolerance float64) bool

ApproxEqual checks if two numbers are approximately equal within a given tolerance. The tolerance is the maximum allowed difference between the numbers. For example, if tolerance is 0.001, the numbers are considered equal if their difference is less than 0.001.

func Asin

func Asin[N Number](x N) N

func Asinh

func Asinh[N Number](x N) N

func Atan

func Atan[N Number](x N) N

func Atan2

func Atan2[N Number](y, x N) N

func Atanh

func Atanh[N Number](x N) N

func Avg

func Avg[N Number](x ...N) N

Avg calculates the average of a variadic number of values.

func Blend

func Blend[N Number](x, y N, p float64) N

Blend blends two numbers based on the proportion p.

func Cbrt

func Cbrt[N Number](x N) N

func Ceil

func Ceil[N Number](x N) N

func Clamp

func Clamp[N Number](x, min, max N) N

Clamp restricts x to be within the range [min, max].

func Copysign

func Copysign[N Number](f, sign N) N

func Cos

func Cos[N Number](x N) N

func Cosh

func Cosh[N Number](x N) N

func Delta

func Delta[N Number](x, y N) N

Delta calculates the difference between x and y.

func Denormalize

func Denormalize[N Number](norm, min, max N) N

Denormalize converts a normalized value in [0,1] to the target range [min, max]. The normalized value is clamped between 0 and 1.

func Dim

func Dim[N Number](x, y N) N

func Erf

func Erf[N Number](x N) N

func Erfc

func Erfc[N Number](x N) N

func Erfcinv

func Erfcinv[N Number](x N) N

func Erfinv

func Erfinv[N Number](x N) N

func Exp

func Exp[N Number](x N) N

func Exp2

func Exp2[N Number](x N) N

func Expm1

func Expm1[N Number](x N) N

func FMA

func FMA[N Number](x, y, z N) N

func Floor

func Floor[N Number](x N) N

func FormatNumber

func FormatNumber[N Number](number N, decimals int) string

FormatNumber formats a number with appropriate suffixes for large values.

func Gamma

func Gamma[N Number](x N) N

func Hypot

func Hypot[N Number](p, q N) N

func J0

func J0[N Number](x N) N

func J1

func J1[N Number](x N) N

func Lerp

func Lerp[N Number](a, b N, t float64) N

Lerp performs linear interpolation between a and b with weight t. The parameter t is typically in range [0,1].

func Log

func Log[N Number](x N) N

func Log1p

func Log1p[N Number](x N) N

func Log2

func Log2[N Number](x N) N

func Log10

func Log10[N Number](x N) N

func Logb

func Logb[N Number](x N) N

func Max

func Max[N Number](x, y N) N

func MaxLenStr

func MaxLenStr(strs ...string) int

MaxLenStr returns the length of the longest string from the provided arguments.

func Min

func Min[N Number](x, y N) N

func Mod

func Mod[N Number](x, y N) N

func NaN

func NaN[N Number]() N

func Nextafter

func Nextafter[N Number](x, y N) N

func Normalize

func Normalize[N Number](val, min, max N) N

Normalize converts a value from a given range [min, max] to a normalized value in [0,1]. If min equals max, it returns 0.

func Pow

func Pow[N Number](x, y N) N

func Remainder

func Remainder[N Number](x, y N) N

func Round

func Round[N Number](x N) N

func RoundToEven

func RoundToEven[N Number](x N) N

func Sin

func Sin[N Number](x N) N

func Sinh

func Sinh[N Number](x N) N

func Sqrt

func Sqrt[N Number](x N) N

func Sub

func Sub[N Number](x, y N) N

Sub returns the difference between x and y.

func Tan

func Tan[N Number](x N) N

func Tanh

func Tanh[N Number](x N) N

func Trunc

func Trunc[N Number](x N) N

func Wrap

func Wrap[N Number](val, min, max N) N

Wrap wraps the value within the interval [min, max).

func Y0

func Y0[N Number](x N) N

func Y1

func Y1[N Number](x N) N

Types

type Float

type Float interface {
	float32 | float64
}

Float is a constraint for floating-point types.

type Matrix

type Matrix [][]float64

Matrix represents a two-dimensional slice of float64 values.

type Number

type Number interface {
	int | int8 | int16 | int32 | int64 |
		uint | uint8 | uint16 | uint32 | uint64 |
		float32 | float64
}

Number is a constraint that covers most common numeric types.

Jump to

Keyboard shortcuts

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