mathutil

package
v14.1.7 Latest Latest
Warning

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

Go to latest
Published: May 13, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package mathutil provides some additional math methods

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs[N NumericNeg](val N) N

Abs returns the absolute value of val

Example
fmt.Println(Abs(10))
fmt.Println(Abs(-10))
Output:
10
10

func B

func B[N Numeric](cond bool, positive, negative N) N

B returns positive if cond is true, otherwise returns negative

Example
isRoot := true

fmt.Println(B(isRoot, 0, 1000))
fmt.Println(B(!isRoot, 0, 1000))
Output:
0
1000

func Between

func Between[N Numeric](val, min, max N) N

Between returns val clamped to the range [min, max]

Example
fmt.Println(Between(10, 1, 5))
fmt.Println(Between(-3, 1, 5))
fmt.Println(Between(4, 1, 5))
Output:
5
1
4

func FromPerc

func FromPerc(perc float64, total float64) float64

FromPerc returns the value corresponding to perc percent of total. Returns 0 if perc is <= 0 or total is 0.

Example
fmt.Printf("%g\n", FromPerc(15.0, 2860))
Output:
429

func IsFloat

func IsFloat(s string) bool

IsFloat returns true if every character in s is a valid float symbol (digits, leading minus, and decimal point).

Note: this does not structurally validate the value; use strconv.ParseFloat for full parsing.

Example
fmt.Println(IsFloat("test"))
fmt.Println(IsFloat("74.6131"))
fmt.Println(IsFloat("-10.4"))
Output:
false
true
true

func IsInt

func IsInt(s string) bool

IsInt returns true if every character in s is a valid integer symbol (digits and leading minus).

Note: this does not structurally validate the value; use strconv.Atoi for full parsing.

Example
fmt.Println(IsInt("test"))
fmt.Println(IsInt("746131"))
fmt.Println(IsInt("-194"))
Output:
false
true
true

func IsNumber

func IsNumber(s string) bool

IsNumber returns true if s passes either IsInt or IsFloat.

Example
fmt.Println(IsNumber("test"))
fmt.Println(IsNumber("746131"))
fmt.Println(IsNumber("-10.431"))
Output:
false
true
true

func Perc

func Perc[N Numeric](current, total N) float64

Perc returns the percentage that current represents of total. Returns 0 if total is 0.

Example
fmt.Printf("%g%%\n", Perc(180, 600))
Output:
30%

func Round

func Round(v float64, p int) float64

Round returns v rounded to p decimal places using half-up rounding. p should be in range [0, 15] for float64 precision.

Example
fmt.Println(Round(3.14159, 2))
Output:
3.14

Types

type Float

type Float interface {
	~float32 | ~float64
}

Float is a type constraint that matches float32 and float64

type Integer

type Integer interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

Integer is a type constraint that matches all signed and unsigned integer types

type Numeric

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

Numeric is a type constraint that matches all integer, unsigned integer, uintptr, and float types

type NumericNeg

type NumericNeg interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~float32 | ~float64
}

NumericNeg is a type constraint that matches numeric types capable of holding negative values

Jump to

Keyboard shortcuts

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