Skip to content

Commit

Permalink
💚 ci: math - fix run ci tests error on go1.19
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 10, 2023
1 parent 69326dd commit a6a8d0e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
14 changes: 10 additions & 4 deletions mathutil/mathutil.go
Expand Up @@ -2,8 +2,9 @@
package mathutil

import (
"github.com/gookit/goutil/comdef"
"math"

"github.com/gookit/goutil/comdef"
)

// OrElse return default value on val is zero, else return val
Expand Down Expand Up @@ -73,13 +74,18 @@ func GteOr[T comdef.XintOrFloat](val, min, defVal T) T {
return defVal
}

// Mul computes a*b value, rounding the result.
func Mul[T comdef.XintOrFloat](a, b T) float64 {
// Mul computes the a*b value, rounding the result.
func Mul[T1, T2 comdef.XintOrFloat](a T1, b T2) float64 {
return math.Round(SafeFloat(a) * SafeFloat(b))
}

// MulF2i computes the float64 type a * b value, rounding the result to an integer.
func MulF2i(a, b float64) int {
return int(math.Round(a * b))
}

// Div computes the a/b value, result use round handle.
func Div[T comdef.XintOrFloat](a, b T) float64 {
func Div[T1, T2 comdef.XintOrFloat](a T1, b T2) float64 {
return math.Round(SafeFloat(a) / SafeFloat(b))
}

Expand Down
1 change: 1 addition & 0 deletions mathutil/mathutil_test.go
Expand Up @@ -42,4 +42,5 @@ func TestDiv(t *testing.T) {

func TestMul(t *testing.T) {
assert.Eq(t, float64(5), mathutil.Mul(2, 2.35))
assert.Eq(t, 36, mathutil.MulF2i(27, 1.35))
}

0 comments on commit a6a8d0e

Please sign in to comment.