Documentation
¶
Overview ¶
Package mathutil provides some additional math methods
Index ¶
- func Abs[N NumericNeg](val N) N
- func B[N Numeric](cond bool, positive, negative N) N
- func Between[N Numeric](val, min, max N) N
- func FromPerc(perc float64, total float64) float64
- func IsFloat(s string) bool
- func IsInt(s string) bool
- func IsNumber(s string) bool
- func Perc[N Numeric](current, total N) float64
- func Round(v float64, p int) float64
- type Float
- type Integer
- type Numeric
- type NumericNeg
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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
Types ¶
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