# Mathutil

Package mathutil implements some functions for math calculation.

# Source:

# Example:

import (
    "github.com/duke-git/lancet/v2/mathutil"
)

# Documentation

# Average

Return average value of numbers. Maybe call RoundToFloat to round result.

Signature:

func Average[T lancetconstraints.Number](numbers ...T) T

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/mathutil"
)

func main() {
	fmt.Println(mathutil.Average(0, 0)) //0
	fmt.Println(mathutil.Average(1, 1)) //1
	avg := mathutil.Average(1.2, 1.4) //1.2999999998
	roundAvg := mmathutil.RoundToFloat(avg, 1) // 1.3
}

# Exponent

Calculate x to the nth power.

Signature:

func Exponent(x, n int64) int64

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/mathutil"
)

func main() {
	fmt.Println(mathutil.Exponent(10, 0)) //1
	fmt.Println(mathutil.Exponent(10, 1)) //10
	fmt.Println(mathutil.Exponent(10, 2)) //100
}

# Fibonacci

Calculate the nth number of fibonacci sequence.

Signature:

func Fibonacci(first, second, n int) int

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/mathutil"
)

func main() {
	fmt.Println(mathutil.Fibonacci(1, 1, 1)) //1
	fmt.Println(mathutil.Fibonacci(1, 1, 2)) //1
	fmt.Println(mathutil.Fibonacci(1, 1, 3)) //2
	fmt.Println(mathutil.Fibonacci(1, 1, 4)) //3
	fmt.Println(mathutil.Fibonacci(1, 1, 5)) //5
}

# Factorial

Calculate the factorial of x.

Signature:

func Factorial(x uint) uint

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/mathutil"
)

func main() {
	fmt.Println(mathutil.Factorial(0)) //1
	fmt.Println(mathutil.Factorial(1)) //1
	fmt.Println(mathutil.Factorial(2)) //2
	fmt.Println(mathutil.Factorial(3)) //6
}

# Max

Return max value of numbers.

Signature:

func Max[T lancetconstraints.Number](numbers ...T) T

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/mathutil"
)

func main() {
	fmt.Println(mathutil.Max(0, 0)) //0
	fmt.Println(mathutil.Max(1, 2, 3)) //3
	fmt.Println(mathutil.Max(1.2, 1.4, 1.1, 1.4)) //1.4
}

# MaxBy

Return the maximum value of a slice using the given comparator function.

Signature:

func MaxBy[T any](slice []T, comparator func(T, T) bool) T

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/mathutil"
)

func main() {
	res1 := mathutil.MaxBy([]string{"a", "ab", "abc"}, func(v1, v2 string) bool {
		return len(v1) > len(v2)
	})
	fmt.Println(res1) //abc

	res2 := mathutil.MaxBy([]string{"abd", "abc", "ab"}, func(v1, v2 string) bool {
		return len(v1) > len(v2)
	})
	fmt.Println(res2) //abd

	res3 := mathutil.MaxBy([]string{}, func(v1, v2 string) bool {
		return len(v1) > len(v2)
	})
	fmt.Println(res3) //“”
}

# Min

Return min value of numbers.

Signature:

func Min[T lancetconstraints.Number](numbers ...T) T

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/mathutil"
)

func main() {
	fmt.Println(mathutil.Min(0, 0)) //0
	fmt.Println(mathutil.Min(1, 2, 3)) //1
	fmt.Println(mathutil.Min(1.2, 1.4, 1.1, 1.4)) //1.1
}

# MinBy

Return the minimum value of a slice using the given comparator function.

Signature:

func MinBy[T any](slice []T, comparator func(T, T) bool) T

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/mathutil"
)

func main() {
	res1 := mathutil.MinBy([]string{"a", "ab", "abc"}, func(v1, v2 string) bool {
		return len(v1) < len(v2)
	})
	fmt.Println(res1) //a

	res2 := mathutil.MinBy([]string{"ab", "ac", "abc"}, func(v1, v2 string) bool {
		return len(v1) < len(v2)
	})
	fmt.Println(res2) //ab

	res3 := mathutil.MinBy([]string{}, func(v1, v2 string) bool {
		return len(v1) < len(v2)
	})
	fmt.Println(res3) //“”
}

# Percent

calculate the percentage of val to total, retain n decimal places.

Signature:

func Percent(val, total float64, n int) float64

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/mathutil"
)

func main() {
	fmt.Println(mathutil.Percent(1, 2, 2)) //1
	fmt.Println(mathutil.Percent(0.1, 0.3, 2)) //33.33
}

# RoundToFloat

Round float up to n decimal places.

Signature:

func RoundToFloat(x float64, n int) float64

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/mathutil"
)

func main() {
	fmt.Println(mathutil.RoundToFloat(0, 0)) //0
	fmt.Println(mathutil.RoundToFloat(0, 1)) //0
	fmt.Println(mathutil.RoundToFloat(0.124, 2)) //0.12
	fmt.Println(mathutil.RoundToFloat(0.125, 2)) //0.13
	fmt.Println(mathutil.RoundToFloat(0.125, 3)) //0.125
}

# RoundToString

Round float up to n decimal places. will return string.

Signature:

func RoundToString(x float64, n int) string

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/mathutil"
)

func main() {
	fmt.Println(mathutil.RoundToString(0, 0)) //"0"
	fmt.Println(mathutil.RoundToString(0, 1)) //"0.0:
	fmt.Println(mathutil.RoundToString(0.124, 2)) //"0.12"
	fmt.Println(mathutil.RoundToString(0.125, 2)) //"0.13"
	fmt.Println(mathutil.RoundToString(0.125, 3)) //"0.125"
}

# TruncRound

Round float off n decimal places.

Signature:

func TruncRound(x float64, n int) float64

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/mathutil"
)

func main() {
	fmt.Println(mathutil.TruncRound(0, 0)) //0
	fmt.Println(mathutil.TruncRound(0, 1)) //0
	fmt.Println(mathutil.TruncRound(0.124, 2)) //0.12
	fmt.Println(mathutil.TruncRound(0.125, 2)) //0.12
	fmt.Println(mathutil.TruncRound(0.125, 3)) //0.125
}
Last Updated: 9/29/2022, 1:53:14 PM