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 Max[N Numeric](val1, val2 N) N
- func Min[N Numeric](val1, val2 N) N
- func Perc[N Numeric](current, total N) float64
- func Round(v float64, p int) float64
- 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 absolute value
Example ¶
fmt.Println(Abs(10)) fmt.Println(Abs(-10))
Output: 10 10
func B ¶ added in v13.10.1
B is shorthand for choosing value by condition
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 value between min and max values
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 ¶ added in v13.5.1
FromPerc calculates value from percentage
Example ¶
fmt.Printf("%g\n", FromPerc(15.0, 2860))
Output: 429
func IsFloat ¶ added in v13.5.0
IsFloat returns true if given string contains float symbols.
Note that this method does not validate the given value.
Example ¶
fmt.Println(IsFloat("test")) fmt.Println(IsFloat("74.6131")) fmt.Println(IsFloat("-10.4"))
Output: false true true
func IsInt ¶ added in v13.5.0
IsInt returns true if given string contains int symbols.
Note that this method does not validate the given value.
Example ¶
fmt.Println(IsInt("test")) fmt.Println(IsInt("746131")) fmt.Println(IsInt("-194"))
Output: false true true
func IsNumber ¶ added in v13.5.0
IsNumber returns true if given string contains number symbols (int or float).
Note that this method does not validate the given value.
Example ¶
fmt.Println(IsNumber("test")) fmt.Println(IsNumber("746131")) fmt.Println(IsNumber("-10.431"))
Output: false true true
func Max ¶
func Max[N Numeric](val1, val2 N) N
Max returns a greater value
Example ¶
fmt.Println(Max(1, 10)) fmt.Println(Max(3, -3))
Output: 10 3
func Min ¶
func Min[N Numeric](val1, val2 N) N
Min returns a smaller value
Example ¶
fmt.Println(Min(1, 10)) fmt.Println(Min(3, -3))
Output: 1 -3
Types ¶
Click to show internal directories.
Click to hide internal directories.