Documentation ¶
Overview ¶
Package mathutil implements some functions for math calculation.
Index ¶
- func Abs[T constraints.Integer | constraints.Float](x T) T
- func AngleToRadian(angle float64) float64
- func Average[T constraints.Integer | constraints.Float](numbers ...T) T
- func CeilToFloat[T constraints.Float | constraints.Integer](x T, n int) float64
- func CeilToString[T constraints.Float | constraints.Integer](x T, n int) string
- func Cos(radian float64, precision ...int) float64
- func Div[T constraints.Float | constraints.Integer](x T, y T) float64
- func Exponent(x, n int64) int64
- func Factorial(x uint) uint
- func Fibonacci(first, second, n int) int
- func FloorToFloat[T constraints.Float | constraints.Integer](x T, n int) float64
- func FloorToString[T constraints.Float | constraints.Integer](x T, n int) string
- func GCD[T constraints.Integer](integers ...T) T
- func IsPrime(n int) bool
- func LCM[T constraints.Integer](integers ...T) T
- func Log(n, base float64) float64
- 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 PointDistance(x1, y1, x2, y2 float64) float64
- func RadianToAngle(radian float64) float64
- func Range[T constraints.Integer | constraints.Float](start T, count int) []T
- func RangeWithStep[T constraints.Integer | constraints.Float](start, end, step T) []T
- func RoundToFloat[T constraints.Float | constraints.Integer](x T, n int) float64
- func RoundToString[T constraints.Float | constraints.Integer](x T, n int) string
- func Sin(radian float64, precision ...int) float64
- func Sum[T constraints.Integer | constraints.Float](numbers ...T) T
- func TruncRound[T constraints.Float | constraints.Integer](x T, n int) T
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Abs ¶
func Abs[T constraints.Integer | constraints.Float](x T) T
Abs returns the absolute value of x. Play: https://go.dev/play/p/fsyBh1Os-1d
Example ¶
result1 := Abs(-1) result2 := Abs(-0.1) result3 := Abs(float32(0.2)) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3)
Output: 1 0.1 0.2
func AngleToRadian ¶
AngleToRadian converts angle value to radian value. Play: https://go.dev/play/p/CIvlICqrHql
Example ¶
result1 := AngleToRadian(45) result2 := AngleToRadian(90) result3 := AngleToRadian(180) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3)
Output: 0.7853981633974483 1.5707963267948966 3.141592653589793
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 CeilToFloat ¶
func CeilToFloat[T constraints.Float | constraints.Integer](x T, n int) float64
CeilToFloat round up to n decimal places. Play: https://go.dev/play/p/8hOeSADZPCo
Example ¶
result1 := CeilToFloat(3.14159, 1) result2 := CeilToFloat(3.14159, 2) result3 := CeilToFloat(5, 4) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3)
Output: 3.2 3.15 5
func CeilToString ¶
func CeilToString[T constraints.Float | constraints.Integer](x T, n int) string
CeilToString round up to n decimal places. Play: https://go.dev/play/p/wy5bYEyUKKG
Example ¶
result1 := CeilToString(3.14159, 1) result2 := CeilToString(3.14159, 2) result3 := CeilToString(5, 4) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3)
Output: 3.2 3.15 5.0000
func Cos ¶
Cos returns the cosine of the radian argument. Play: https://go.dev/play/p/Sm89LoIfvFq
Example ¶
result1 := Cos(0) result2 := Cos(90) result3 := Cos(180) result4 := Cos(math.Pi) result5 := Cos(math.Pi / 2) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3) fmt.Println(result4) fmt.Println(result5)
Output: 1 -0.447 -0.598 -1 0
func Div ¶
func Div[T constraints.Float | constraints.Integer](x T, y T) float64
Div returns the result of x divided by y. Play: https://go.dev/play/p/WLxDdGXXYat
Example ¶
result1 := Div(9, 4) result2 := Div(1, 2) result3 := Div(0, 666) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3)
Output: 2.25 0.5 0
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 FloorToFloat ¶
func FloorToFloat[T constraints.Float | constraints.Integer](x T, n int) float64
FloorToFloat round down to n decimal places. Play: https://go.dev/play/p/vbCBrQHZEED
Example ¶
result1 := FloorToFloat(3.14159, 1) result2 := FloorToFloat(3.14159, 2) result3 := FloorToFloat(5, 4) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3)
Output: 3.1 3.14 5
func FloorToString ¶
func FloorToString[T constraints.Float | constraints.Integer](x T, n int) string
FloorToString round down to n decimal places. Play: https://go.dev/play/p/Qk9KPd2IdDb
Example ¶
result1 := FloorToString(3.14159, 1) result2 := FloorToString(3.14159, 2) result3 := FloorToString(5, 4) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3)
Output: 3.1 3.14 5.0000
func GCD ¶
func GCD[T constraints.Integer](integers ...T) T
GCD return greatest common divisor (GCD) of integers. Play: https://go.dev/play/p/CiEceLSoAKB
Example ¶
result1 := GCD(1, 1) result2 := GCD(1, -1) result3 := GCD(-1, 1) result4 := GCD(-1, -1) result5 := GCD(3, 6, 9) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3) fmt.Println(result4) fmt.Println(result5)
Output: 1 1 -1 -1 3
func IsPrime ¶
IsPrime checks if number is prime number. Play: https://go.dev/play/p/Rdd8UTHZJ7u
Example ¶
result1 := IsPrime(-1) result2 := IsPrime(0) result3 := IsPrime(1) result4 := IsPrime(2) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3) fmt.Println(result4)
Output: false false false true
func LCM ¶
func LCM[T constraints.Integer](integers ...T) T
LCM return Least Common Multiple (LCM) of integers. Play: https://go.dev/play/p/EjcZxfY7G_g
Example ¶
result1 := LCM(1, 1) result2 := LCM(1, 2) result3 := LCM(3, 6, 9) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3)
Output: 1 2 18
func Log ¶
Log returns the logarithm of base n. Play: https://go.dev/play/p/_d4bi8oyhat
Example ¶
result1 := Log(8, 2) result2 := TruncRound(Log(5, 2), 2) result3 := TruncRound(Log(27, 3), 0) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3)
Output: 3 2.32 3
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: https://go.dev/play/p/s0NdFCtwuyd
Example ¶
result1 := Percent(1, 2, 2) result2 := Percent(0.1, 0.3, 2) result3 := Percent(-30305, 408420, 2) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3)
Output: 50 33.33 -7.42
func PointDistance ¶
PointDistance get two points distance. Play: https://go.dev/play/p/RrG4JIaziM8
Example ¶
result1 := PointDistance(1, 1, 4, 5) fmt.Println(result1)
Output: 5
func RadianToAngle ¶
RadianToAngle converts radian value to angle value. Play: https://go.dev/play/p/dQtmOTUOMgi
Example ¶
result1 := RadianToAngle(math.Pi) result2 := RadianToAngle(math.Pi / 2) result3 := RadianToAngle(math.Pi / 4) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3)
Output: 180 90 45
func Range ¶
func Range[T constraints.Integer | constraints.Float](start T, count int) []T
Range creates a slice of numbers from start with specified count, element step is 1. Play: https://go.dev/play/p/9ke2opxa8ZP
Example ¶
result1 := Range(1, 4) result2 := Range(1, -4) result3 := Range(-4, 4) result4 := Range(1.0, 4) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3) fmt.Println(result4)
Output: [1 2 3 4] [1 2 3 4] [-4 -3 -2 -1] [1 2 3 4]
func RangeWithStep ¶
func RangeWithStep[T constraints.Integer | constraints.Float](start, end, step T) []T
RangeWithStep creates a slice of numbers from start to end with specified step. Play: https://go.dev/play/p/akLWz0EqOSM
Example ¶
result1 := RangeWithStep(1, 4, 1) result2 := RangeWithStep(1, -1, 0) result3 := RangeWithStep(-4, 1, 2) result4 := RangeWithStep(1.0, 4.0, 1.1) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3) fmt.Println(result4)
Output: [1 2 3] [] [-4 -2 0] [1 2.1 3.2]
func RoundToFloat ¶
func RoundToFloat[T constraints.Float | constraints.Integer](x T, n int) float64
RoundToFloat round off 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 ¶
func RoundToString[T constraints.Float | constraints.Integer](x T, n int) string
RoundToString round off 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 Sin ¶
Sin returns the sine of the radian argument. Play: https://go.dev/play/p/TWMQlMywDsP
Example ¶
result1 := Sin(0) result2 := Sin(90) result3 := Sin(180) result4 := Sin(math.Pi) result5 := Sin(math.Pi / 2) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3) fmt.Println(result4) fmt.Println(result5)
Output: 0 0.894 -0.801 0 1
func Sum ¶
func Sum[T constraints.Integer | constraints.Float](numbers ...T) T
Sum return sum of passed numbers. Play: https://go.dev/play/p/1To2ImAMJA7
Example ¶
result1 := Sum(1, 2) result2 := Sum(0.1, float64(1)) fmt.Println(result1) fmt.Println(result2)
Output: 3 1.1
func TruncRound ¶
func TruncRound[T constraints.Float | constraints.Integer](x T, n int) T
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.