Skip to content
On this page

# Mathutil ​

Package mathutil implements some functions for math calculation.

## Source: ​

- https://github.com/duke-git/lancet/blob/main/mathutil/mathutil.go

## Example: ​

go

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

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

## Index ​

- Average
- Exponent
- Fibonacci
- Factorial
- Max
- MaxBy
- Min
- MinBy
- Percent
- RoundToFloat
- RoundToString
- TruncRound
- Range
- RangeWithStep
- AngleToRadian
- RadianToAngle
- PointDistance
- IsPrime
- GCD
- LCM
- Cos
- Sin
- Log
- Sum
- Abs

## Documentation ​

### Average ​

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

Signature:

go

    func Average[T constraints.Integer | constraints.Float](numbers ...T) T

    func Average[T constraints.Integer | constraints.Float](numbers ...T) T

Example:Run

go

    package main

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

    func main() {
        result1 := mathutil.Average(1, 2)

        avg := mathutil.Average(1.2, 1.4)
        result2 := mathutil.RoundToFloat(avg, 1)

        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // 1
        // 1.3
    }

    package main

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

    func main() {
        result1 := mathutil.Average(1, 2)

        avg := mathutil.Average(1.2, 1.4)
        result2 := mathutil.RoundToFloat(avg, 1)

        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // 1
        // 1.3
    }

### Exponent ​

Calculate x to the nth power.

Signature:

go

    func Exponent(x, n int64) int64

    func Exponent(x, n int64) int64

Example:Run

go

    package main

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

    func main() {
        result1 := mathutil.Exponent(10, 0)
        result2 := mathutil.Exponent(10, 1)
        result3 := mathutil.Exponent(10, 2)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // 1
        // 10
        // 100
    }

    package main

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

    func main() {
        result1 := mathutil.Exponent(10, 0)
        result2 := mathutil.Exponent(10, 1)
        result3 := mathutil.Exponent(10, 2)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // 1
        // 10
        // 100
    }

### Fibonacci ​

Calculate the nth number of fibonacci sequence.

Signature:

go

    func Fibonacci(first, second, n int) int

    func Fibonacci(first, second, n int) int

Example:Run

go

    package main

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

    func main() {
        result1 := mathutil.Fibonacci(1, 1, 1)
        result2 := mathutil.Fibonacci(1, 1, 2)
        result3 := mathutil.Fibonacci(1, 1, 5)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // 1
        // 1
        // 5
    }

    package main

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

    func main() {
        result1 := mathutil.Fibonacci(1, 1, 1)
        result2 := mathutil.Fibonacci(1, 1, 2)
        result3 := mathutil.Fibonacci(1, 1, 5)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // 1
        // 1
        // 5
    }

### Factorial ​

Calculate the factorial of x.

Signature:

go

    func Factorial(x uint) uint

    func Factorial(x uint) uint

Example:Run

go

    package main

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

    func main() {
        result1 := mathutil.Factorial(1)
        result2 := mathutil.Factorial(2)
        result3 := mathutil.Factorial(3)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // 1
        // 2
        // 6
    }

    package main

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

    func main() {
        result1 := mathutil.Factorial(1)
        result2 := mathutil.Factorial(2)
        result3 := mathutil.Factorial(3)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // 1
        // 2
        // 6
    }

### Max ​

Return max value of numbers.

Signature:

go

    func Max[T constraints.Integer | constraints.Float](numbers ...T) T

    func Max[T constraints.Integer | constraints.Float](numbers ...T) T

Example:Run

go

    package main

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

    func main() {
        result1 := mathutil.Max(1, 2, 3)
        result2 := mathutil.Max(1.2, 1.4, 1.1, 1.4)

        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // 3
        // 1.4
    }

    package main

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

    func main() {
        result1 := mathutil.Max(1, 2, 3)
        result2 := mathutil.Max(1.2, 1.4, 1.1, 1.4)

        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // 3
        // 1.4
    }

### MaxBy ​

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

Signature:

go

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

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

Example:Run

go

    package main

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

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

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

        result3 := mathutil.MaxBy([]string{}, func(v1, v2 string) bool {
            return len(v1) > len(v2)
        })

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // abc
        // abd
        //
    }

    package main

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

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

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

        result3 := mathutil.MaxBy([]string{}, func(v1, v2 string) bool {
            return len(v1) > len(v2)
        })

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // abc
        // abd
        //
    }

### Min ​

Return the minimum value of numbers.

Signature:

go

    func Min[T constraints.Integer | constraints.Float](numbers ...T) T

    func Min[T constraints.Integer | constraints.Float](numbers ...T) T

Example:Run

go

    package main

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

    func main() {
        result1 := mathutil.Min(1, 2, 3)
        result2 := mathutil.Min(1.2, 1.4, 1.1, 1.4)

        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // 1
        // 1.1
    }

    package main

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

    func main() {
        result1 := mathutil.Min(1, 2, 3)
        result2 := mathutil.Min(1.2, 1.4, 1.1, 1.4)

        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // 1
        // 1.1
    }

### MinBy ​

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

Signature:

go

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

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

Example:Run

go

    package main

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

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

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

        result3 := mathutil.MinBy([]string{}, func(v1, v2 string) bool {
            return len(v1) < len(v2)
        })

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // a
        // ab
        //
    }

    package main

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

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

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

        result3 := mathutil.MinBy([]string{}, func(v1, v2 string) bool {
            return len(v1) < len(v2)
        })

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // a
        // ab
        //
    }

### Percent ​

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

Signature:

go

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

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

Example:Run

go

    package main

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

    func main() {
        result1 := mathutil.Percent(1, 2, 2)
        result2 := mathutil.Percent(0.1, 0.3, 2)
        result3 := mathutil.Percent(-30305, 408420, 2)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // 50
        // 33.33
        // -7.42
    }

    package main

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

    func main() {
        result1 := mathutil.Percent(1, 2, 2)
        result2 := mathutil.Percent(0.1, 0.3, 2)
        result3 := mathutil.Percent(-30305, 408420, 2)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // 50
        // 33.33
        // -7.42
    }

### RoundToFloat ​

Round float up to n decimal places.

Signature:

go

    func RoundToFloat(x float64, n int) float64

    func RoundToFloat(x float64, n int) float64

Example:Run

go

    package main

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

    func main() {
        result1 := mathutil.RoundToFloat(0.124, 2)
        result2 := mathutil.RoundToFloat(0.125, 2)
        result3 := mathutil.RoundToFloat(0.125, 3)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // 0.12
        // 0.13
        // 0.125
    }

    package main

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

    func main() {
        result1 := mathutil.RoundToFloat(0.124, 2)
        result2 := mathutil.RoundToFloat(0.125, 2)
        result3 := mathutil.RoundToFloat(0.125, 3)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // 0.12
        // 0.13
        // 0.125
    }

### RoundToString ​

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

Signature:

go

    func RoundToString(x float64, n int) string

    func RoundToString(x float64, n int) string

Example:Run

go

    package main

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

    func main() {
        result1 := mathutil.RoundToString(0.124, 2)
        result2 := mathutil.RoundToString(0.125, 2)
        result3 := mathutil.RoundToString(0.125, 3)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // 0.12
        // 0.13
        // 0.125
    }

    package main

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

    func main() {
        result1 := mathutil.RoundToString(0.124, 2)
        result2 := mathutil.RoundToString(0.125, 2)
        result3 := mathutil.RoundToString(0.125, 3)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // 0.12
        // 0.13
        // 0.125
    }

### TruncRound ​

Round float off n decimal places.

Signature:

go

    func TruncRound(x float64, n int) float64

    func TruncRound(x float64, n int) float64

Example:Run

go

    package main

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

    func main() {
        result1 := mathutil.TruncRound(0.124, 2)
        result2 := mathutil.TruncRound(0.125, 2)
        result3 := mathutil.TruncRound(0.125, 3)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // 0.12
        // 0.12
        // 0.125
    }

    package main

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

    func main() {
        result1 := mathutil.TruncRound(0.124, 2)
        result2 := mathutil.TruncRound(0.125, 2)
        result3 := mathutil.TruncRound(0.125, 3)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // 0.12
        // 0.12
        // 0.125
    }

### Range ​

Creates a slice of numbers from start with specified count, element step is 1.

Signature:

go

    func Range[T constraints.Integer | constraints.Float](start T, count int) []T

    func Range[T constraints.Integer | constraints.Float](start T, count int) []T

Example:Run

go

    package main

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

    func main() {
        result1 := mathutil.Range(1, 4)
        result2 := mathutil.Range(1, -4)
        result3 := mathutil.Range(-4, 4)
        result4 := mathutil.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]
    }

    package main

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

    func main() {
        result1 := mathutil.Range(1, 4)
        result2 := mathutil.Range(1, -4)
        result3 := mathutil.Range(-4, 4)
        result4 := mathutil.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]
    }

### RangeWithStep ​

Creates a slice of numbers from start to end with specified step.

Signature:

go

    func RangeWithStep[T constraints.Integer | constraints.Float](start, end, step T) []T

    func RangeWithStep[T constraints.Integer | constraints.Float](start, end, step T) []T

Example:Run

go

    package main

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

    func main() {
        result1 := mathutil.RangeWithStep(1, 4, 1)
        result2 := mathutil.RangeWithStep(1, -1, 0)
        result3 := mathutil.RangeWithStep(-4, 1, 2)
        result4 := mathutil.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]
    }

    package main

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

    func main() {
        result1 := mathutil.RangeWithStep(1, 4, 1)
        result2 := mathutil.RangeWithStep(1, -1, 0)
        result3 := mathutil.RangeWithStep(-4, 1, 2)
        result4 := mathutil.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]
    }

### AngleToRadian ​

Converts angle value to radian value.

Signature:

go

    func AngleToRadian(angle float64) float64

    func AngleToRadian(angle float64) float64

Example:Run

go

    package main

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

    func main() {
        result1 := mathutil.AngleToRadian(45)
        result2 := mathutil.AngleToRadian(90)
        result3 := mathutil.AngleToRadian(180)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // 0.7853981633974483
        // 1.5707963267948966
        // 3.141592653589793
    }

    package main

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

    func main() {
        result1 := mathutil.AngleToRadian(45)
        result2 := mathutil.AngleToRadian(90)
        result3 := mathutil.AngleToRadian(180)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // 0.7853981633974483
        // 1.5707963267948966
        // 3.141592653589793
    }

### RadianToAngle ​

Converts radian value to angle value.

Signature:

go

    func RadianToAngle(radian float64) float64

    func RadianToAngle(radian float64) float64

Example:Run

go

    package main

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

    func main() {
        result1 := mathutil.RadianToAngle(math.Pi)
        result2 := mathutil.RadianToAngle(math.Pi / 2)
        result3 := mathutil.RadianToAngle(math.Pi / 4)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // 180
        // 90
        // 45
    }

    package main

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

    func main() {
        result1 := mathutil.RadianToAngle(math.Pi)
        result2 := mathutil.RadianToAngle(math.Pi / 2)
        result3 := mathutil.RadianToAngle(math.Pi / 4)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // 180
        // 90
        // 45
    }

### PointDistance ​

Caculates two points distance.

Signature:

go

    func PointDistance(x1, y1, x2, y2 float64) float64

    func PointDistance(x1, y1, x2, y2 float64) float64

Example:Run

go

    package main

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

    func main() {
        result1 := mathutil.PointDistance(1, 1, 4, 5)

        fmt.Println(result1)

        // Output:
        // 5
    }

    package main

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

    func main() {
        result1 := mathutil.PointDistance(1, 1, 4, 5)

        fmt.Println(result1)

        // Output:
        // 5
    }

### IsPrime ​

Checks if number is prime number.

Signature:

go

    func IsPrime(n int) bool

    func IsPrime(n int) bool

Example:Run

go

    package main

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

    func main() {
        result1 := mathutil.IsPrime(-1)
        result2 := mathutil.IsPrime(0)
        result3 := mathutil.IsPrime(1)
        result4 := mathutil.IsPrime(2)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)

        // Output:
        // false
        // false
        // false
        // true
    }

    package main

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

    func main() {
        result1 := mathutil.IsPrime(-1)
        result2 := mathutil.IsPrime(0)
        result3 := mathutil.IsPrime(1)
        result4 := mathutil.IsPrime(2)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)

        // Output:
        // false
        // false
        // false
        // true
    }

### GCD ​

Return greatest common divisor (GCD) of integers.

Signature:

go

    func GCD[T constraints.Integer](integers ...T) T

    func GCD[T constraints.Integer](integers ...T) T

Example:Run

go

    package main

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

    func main() {
        result1 := mathutil.GCD(1, 1)
        result2 := mathutil.GCD(1, -1)
        result3 := mathutil.GCD(-1, 1)
        result4 := mathutil.GCD(-1, -1)
        result5 := mathutil.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
    }

    package main

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

    func main() {
        result1 := mathutil.GCD(1, 1)
        result2 := mathutil.GCD(1, -1)
        result3 := mathutil.GCD(-1, 1)
        result4 := mathutil.GCD(-1, -1)
        result5 := mathutil.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
    }

### LCM ​

Return Least Common Multiple (LCM) of integers.

Signature:

go

    func LCM[T constraints.Integer](integers ...T) T

    func LCM[T constraints.Integer](integers ...T) T

Example:Run

go

    package main

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

    func main() {
        result1 := mathutil.LCM(1, 1)
        result2 := mathutil.LCM(1, 2)
        result3 := mathutil.LCM(3, 6, 9)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // 1
        // 2
        // 18
    }

    package main

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

    func main() {
        result1 := mathutil.LCM(1, 1)
        result2 := mathutil.LCM(1, 2)
        result3 := mathutil.LCM(3, 6, 9)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // 1
        // 2
        // 18
    }

### Cos ​

Returns the cosine of the radian argument.

Signature:

go

    func Cos(radian float64, precision ...int) float64

    func Cos(radian float64, precision ...int) float64

Example:Run

go

    package main

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

    func main() {
        result1 := mathutil.Cos(0)
        result2 := mathutil.Cos(90)
        result3 := mathutil.Cos(180)
        result4 := mathutil.Cos(math.Pi)
        result5 := mathutil.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
    }

    package main

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

    func main() {
        result1 := mathutil.Cos(0)
        result2 := mathutil.Cos(90)
        result3 := mathutil.Cos(180)
        result4 := mathutil.Cos(math.Pi)
        result5 := mathutil.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
    }

### Sin ​

Returns the sine of the radian argument.

Signature:

go

    func Sin(radian float64, precision ...int) float64

    func Sin(radian float64, precision ...int) float64

Example:Run

go

    package main

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

    func main() {
        result1 := mathutil.Sin(0)
        result2 := mathutil.Sin(90)
        result3 := mathutil.Sin(180)
        result4 := mathutil.Sin(math.Pi)
        result5 := mathutil.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
    }

    package main

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

    func main() {
        result1 := mathutil.Sin(0)
        result2 := mathutil.Sin(90)
        result3 := mathutil.Sin(180)
        result4 := mathutil.Sin(math.Pi)
        result5 := mathutil.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
    }

### Log ​

Returns the logarithm of base n.

Signature:

go

    func Log(n, base float64) float64

    func Log(n, base float64) float64

Example:Run

go

    package main

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

    func main() {
        result1 := mathutil.Log(8, 2)
        result2 := mathutil.TruncRound(mathutil.Log(5, 2), 2)
        result3 := mathutil.TruncRound(mathutil.Log(27, 3), 0)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // 3
        // 2.32
        // 3
    }

    package main

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

    func main() {
        result1 := mathutil.Log(8, 2)
        result2 := mathutil.TruncRound(mathutil.Log(5, 2), 2)
        result3 := mathutil.TruncRound(mathutil.Log(27, 3), 0)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // 3
        // 2.32
        // 3
    }

### Sum ​

Returns sum of passed numbers.

Signature:

go

    func Sum[T constraints.Integer | constraints.Float](numbers ...T) T

    func Sum[T constraints.Integer | constraints.Float](numbers ...T) T

Example:Run

go

    package main

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

    func main() {
        result1 := mathutil.Sum(1, 2)
        result2 := mathutil.Sum(0.1, float64(1))

        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // 3
        // 1.1
    }

    package main

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

    func main() {
        result1 := mathutil.Sum(1, 2)
        result2 := mathutil.Sum(0.1, float64(1))

        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // 3
        // 1.1
    }

### Abs ​

Returns the absolute value of x.

Signature:

go

    func Abs[T constraints.Integer | constraints.Float](x T) T

    func Abs[T constraints.Integer | constraints.Float](x T) T

Example:Run

go

    package main

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

    func main() {
        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
    }

    package main

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

    func main() {
        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
    }