maths

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: GPL-3.0 Imports: 5 Imported by: 0

README

stdx/maths

Import path: github.com/InsideGallery/core/stdx/maths

Overview

stdx/maths provides Cantor pairing, rounding, clamping, weighted random selection, and 128-bit integer split helpers.

Main APIs

  • CantorPair and CantorUnpair map two uint64 values to and from one uint64 value.
  • RoundWithPrecision, Round, Clamp, and ApproximatelyEqual provide small float helpers.
  • IntStringToBigInt, BigIntToHighAndLow, and HighAndLowToBigInt convert decimal strings and high/low uint64 pairs.
  • WeightIndex selects a key from a map[interface{}]uint64 using the weights as probabilities.
  • RandomDigitString returns a random string of the requested length.
  • DefaultPrecision is used by RoundWithPrecision when the provided precision is approximately zero.

Usage

pair := maths.CantorPair(7, 11)
left, right := maths.CantorUnpair(pair)
clamped := maths.Clamp(120, 0, 100)

_ = left
_ = right
_ = clamped

Notes

WeightIndex returns nil for an empty map. Despite its name, RandomDigitString fills the string with random byte values from math/rand, not only ASCII digits and not cryptographic randomness.

Documentation

Overview

Package maths provides maths and probability helpers.

Index

Constants

View Source
const (
	DefaultPrecision float64 = 0.0001
)

mathematics constants

Variables

This section is empty.

Functions

func ApproximatelyEqual

func ApproximatelyEqual(a, b float64) bool

ApproximatelyEqual function to test if two real numbers are (almost) equal

func BigIntToHighAndLow

func BigIntToHighAndLow(x *big.Int) (uint64, uint64)

func CantorPair

func CantorPair(k1, k2 uint64) uint64

CantorPair two uint64 numbers by cantor pairing function.

func CantorUnpair

func CantorUnpair(pair uint64) (uint64, uint64)

CantorUnpair one uint64 pair to two uint64 numbers by cantor pairing function.

func Clamp

func Clamp(value, lowerLimit, upperLimit float64) float64

Clamp return the result of the "value" clamped by

func HighAndLowToBigInt

func HighAndLowToBigInt(h, l uint64) *big.Int

func IntStringToBigInt

func IntStringToBigInt(str string) *big.Int

func RandomDigitString

func RandomDigitString(length int) (result string)

RandomDigitString return random digit strings

func Round

func Round(v float64, p float64) float64

Round float to precision

func RoundWithPrecision

func RoundWithPrecision(value float64, precision float64) float64

RoundWithPrecision round precision

func WeightIndex

func WeightIndex(prob map[interface{}]uint64) interface{}

WeightIndex get key by probability chances

Types

This section is empty.

Jump to

Keyboard shortcuts

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