Documentation ¶
Overview ¶
Package mathutil implements some functions for math calculation.
Index ¶
- func Average[T constraints.Integer | constraints.Float](numbers ...T) T
- func Exponent(x, n int64) int64
- func Factorial(x uint) uint
- func Fibonacci(first, second, n int) int
- func Max[T constraints.Integer | constraints.Float](numbers ...T) T
- func MaxBy[T any](slice []T, comparator func(T, T) bool) T
- func Min[T constraints.Integer | constraints.Float](numbers ...T) T
- func MinBy[T any](slice []T, comparator func(T, T) bool) T
- func Percent(val, total float64, n int) float64
- func RoundToFloat(x float64, n int) float64
- func RoundToString(x float64, n int) string
- func TruncRound(x float64, n int) float64
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Average ¶
func Average[T constraints.Integer | constraints.Float](numbers ...T) T
Average return average value of numbers. Play: https://go.dev/play/p/Vv7LBwER-pz
Example ¶
result1 := Average(1, 2) result2 := RoundToFloat(Average(1.2, 1.4), 1) fmt.Println(result1) fmt.Println(result2)
Output: 1 1.3
func Exponent ¶
Exponent calculate x^n. Play: https://go.dev/play/p/uF3HGNPk8wr
Example ¶
result1 := Exponent(10, 0) result2 := Exponent(10, 1) result3 := Exponent(10, 2) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3)
Output: 1 10 100
func Factorial ¶
Factorial calculate x!. Play: https://go.dev/play/p/tt6LdOK67Nx
Example ¶
result1 := Factorial(1) result2 := Factorial(2) result3 := Factorial(3) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3)
Output: 1 2 6
func Fibonacci ¶
Fibonacci calculate fibonacci number before n. Play: https://go.dev/play/p/IscseUNMuUc
Example ¶
result1 := Fibonacci(1, 1, 1) result2 := Fibonacci(1, 1, 2) result3 := Fibonacci(1, 1, 5) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3)
Output: 1 1 5
func Max ¶
func Max[T constraints.Integer | constraints.Float](numbers ...T) T
Max return max value of numbers. Play: https://go.dev/play/p/cN8DHI0rTkH
Example ¶
result1 := Max(1, 2, 3) result2 := Max(1.2, 1.4, 1.1, 1.4) fmt.Println(result1) fmt.Println(result2)
Output: 3 1.4
func MaxBy ¶
MaxBy return the maximum value of a slice using the given comparator function. Play: https://go.dev/play/p/pbe2MT-7DV2
Example ¶
result1 := MaxBy([]string{"a", "ab", "abc"}, func(v1, v2 string) bool { return len(v1) > len(v2) }) result2 := MaxBy([]string{"abd", "abc", "ab"}, func(v1, v2 string) bool { return len(v1) > len(v2) }) result3 := MaxBy([]string{}, func(v1, v2 string) bool { return len(v1) > len(v2) }) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3)
Output: abc abd
func Min ¶
func Min[T constraints.Integer | constraints.Float](numbers ...T) T
Min return min value of numbers. Play: https://go.dev/play/p/21BER_mlGUj
Example ¶
result1 := Min(1, 2, 3) result2 := Min(1.2, 1.4, 1.1, 1.4) fmt.Println(result1) fmt.Println(result2)
Output: 1 1.1
func MinBy ¶
MinBy return the minimum value of a slice using the given comparator function. Play: https://go.dev/play/p/XuJDKrDdglW
Example ¶
result1 := MinBy([]string{"a", "ab", "abc"}, func(v1, v2 string) bool { return len(v1) < len(v2) }) result2 := MinBy([]string{"ab", "ac", "abc"}, func(v1, v2 string) bool { return len(v1) < len(v2) }) result3 := MinBy([]string{}, func(v1, v2 string) bool { return len(v1) < len(v2) }) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3)
Output: a ab
func Percent ¶
Percent calculate the percentage of value to total. Play: Todo
Example ¶
result1 := Percent(1, 2, 2) result2 := Percent(0.1, 0.3, 2) fmt.Println(result1) fmt.Println(result2)
Output: 0.5 0.33
func RoundToFloat ¶
RoundToFloat round up to n decimal places. Play: https://go.dev/play/p/ghyb528JRJL
Example ¶
result1 := RoundToFloat(0.124, 2) result2 := RoundToFloat(0.125, 2) result3 := RoundToFloat(0.125, 3) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3)
Output: 0.12 0.13 0.125
func RoundToString ¶
RoundToString round up to n decimal places. Play: https://go.dev/play/p/kZwpBRAcllO
Example ¶
result1 := RoundToString(0.124, 2) result2 := RoundToString(0.125, 2) result3 := RoundToString(0.125, 3) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3)
Output: 0.12 0.13 0.125
func TruncRound ¶
TruncRound round off n decimal places. Play: https://go.dev/play/p/aumarSHIGzP
Example ¶
result1 := TruncRound(0.124, 2) result2 := TruncRound(0.125, 2) result3 := TruncRound(0.125, 3) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3)
Output: 0.12 0.12 0.125
Types ¶
This section is empty.