From 0215f652ef7f4f7cfa2c37f9f093218b6cae60bd Mon Sep 17 00:00:00 2001 From: cya <2081215119@qq.com> Date: Mon, 4 Mar 2024 17:05:12 +0800 Subject: [PATCH] feat(mathutil): add DIv, FloorToFloat, FloorToString, CeilToFloat, CeilToString --- docs/api/packages/mathutil.md | 149 +++++++++++++++++++++++ docs/en/api/packages/mathutil.md | 190 +++++++++++++++++++++++++++++- mathutil/mathutil.go | 45 ++++++- mathutil/mathutil_exmaple_test.go | 74 ++++++++++++ mathutil/mathutil_test.go | 64 +++++++++- 5 files changed, 514 insertions(+), 8 deletions(-) diff --git a/docs/api/packages/mathutil.md b/docs/api/packages/mathutil.md index f4e67c01..da5326ab 100644 --- a/docs/api/packages/mathutil.md +++ b/docs/api/packages/mathutil.md @@ -34,6 +34,10 @@ import ( - [RoundToFloat](#RoundToFloat) - [RoundToString](#RoundToString) - [TruncRound](#TruncRound) +- [CeilToFloat](#CeilToFloat) +- [CeilToString](#CeilToString) +- [FloorToFloat](#FloorToFloat) +- [FloorToString](#FloorToString) - [Range](#Range) - [RangeWithStep](#RangeWithStep) - [AngleToRadian](#AngleToRadian) @@ -47,6 +51,7 @@ import ( - [Log](#Log) - [Sum](#Sum) - [Abs](#Abs) +- [Div](#Div)
@@ -493,6 +498,150 @@ func main() { } ``` +### CeilToFloat + +

向上舍入(进一法),保留 n 位小数

+ +函数签名: + +```go +func CeilToFloat[T constraints.Float | constraints.Integer](x T, n int) float64 +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/mathutil" +) + +func main() { + result1 := mathutil.CeilToFloat(3.14159, 1) + result2 := mathutil.CeilToFloat(3.14159, 2) + result3 := mathutil.CeilToFloat(5, 4) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // 3.2 + // 3.15 + // 5 +} +``` + +### CeilToString + +

向上舍入(进一法),保留 n 位小数,返回字符串

+ +函数签名: + +```go +func CeilToString[T constraints.Float | constraints.Integer](x T, n int) string +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/mathutil" +) + +func main() { + result1 := mathutil.CeilToString(3.14159, 1) + result2 := mathutil.CeilToString(3.14159, 2) + result3 := mathutil.CeilToString(5, 4) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // 3.2 + // 3.15 + // 5.0000 +} +``` + +### FloorToFloat + +

向下舍入(去尾法),保留 n 位小数

+ +函数签名: + +```go +func CeilToString[T constraints.Float | constraints.Integer](x T, n int) string +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/mathutil" +) + +func main() { + result1 := mathutil.FloorToFloat(3.14159, 1) + result2 := mathutil.FloorToFloat(3.14159, 2) + result3 := mathutil.FloorToFloat(5, 4) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // 3.1 + // 3.14 + // 5 +} +``` + +### FloorToString + +

向下舍入(去尾法),保留 n 位小数,返回字符串

+ +函数签名: + +```go +func CeilToString[T constraints.Float | constraints.Integer](x T, n int) string +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/mathutil" +) + +func main() { + result1 := mathutil.FloorToString(3.14159, 1) + result2 := mathutil.FloorToString(3.14159, 2) + result3 := mathutil.FloorToString(5, 4) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // 3.1 + // 3.14 + // 5.0000 +} +``` + ### Range

根据指定的起始值和数量,创建一个数字切片。

diff --git a/docs/en/api/packages/mathutil.md b/docs/en/api/packages/mathutil.md index af61ba37..6d9fba10 100644 --- a/docs/en/api/packages/mathutil.md +++ b/docs/en/api/packages/mathutil.md @@ -34,6 +34,10 @@ import ( - [RoundToFloat](#RoundToFloat) - [RoundToString](#RoundToString) - [TruncRound](#TruncRound) +- [CeilToFloat](#CeilToFloat) +- [CeilToString](#CeilToString) +- [FloorToFloat](#FloorToFloat) +- [FloorToString](#FloorToString) - [Range](#Range) - [RangeWithStep](#RangeWithStep) - [AngleToRadian](#AngleToRadian) @@ -47,6 +51,7 @@ import ( - [Log](#Log) - [Sum](#Sum) - [Abs](#Abs) +- [Div](#Div)
@@ -493,6 +498,150 @@ func main() { } ``` +### CeilToFloat + +

Round float up n decimal places.

+ +Signature: + +```go +func CeilToFloat[T constraints.Float | constraints.Integer](x T, n int) float64 +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/mathutil" +) + +func main() { + result1 := mathutil.CeilToFloat(3.14159, 1) + result2 := mathutil.CeilToFloat(3.14159, 2) + result3 := mathutil.CeilToFloat(5, 4) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // 3.2 + // 3.15 + // 5 +} +``` + +### CeilToString + +

Round float up n decimal places.

+ +Signature: + +```go +func CeilToString[T constraints.Float | constraints.Integer](x T, n int) string +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/mathutil" +) + +func main() { + result1 := mathutil.CeilToString(3.14159, 1) + result2 := mathutil.CeilToString(3.14159, 2) + result3 := mathutil.CeilToString(5, 4) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // 3.2 + // 3.15 + // 5.0000 +} +``` + +### FloorToFloat + +

Round float down n decimal places.

+ +Signature: + +```go +func CeilToString[T constraints.Float | constraints.Integer](x T, n int) string +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/mathutil" +) + +func main() { + result1 := mathutil.FloorToFloat(3.14159, 1) + result2 := mathutil.FloorToFloat(3.14159, 2) + result3 := mathutil.FloorToFloat(5, 4) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // 3.1 + // 3.14 + // 5 +} +``` + +### FloorToString + +

Round float down n decimal places.

+ +Signature: + +```go +func CeilToString[T constraints.Float | constraints.Integer](x T, n int) string +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/mathutil" +) + +func main() { + result1 := mathutil.FloorToString(3.14159, 1) + result2 := mathutil.FloorToString(3.14159, 2) + result3 := mathutil.FloorToString(5, 4) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // 3.1 + // 3.14 + // 5.0000 +} +``` + ### Range

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

@@ -964,9 +1113,9 @@ import ( ) func main() { - result1 := Abs(-1) - result2 := Abs(-0.1) - result3 := Abs(float32(0.2)) + result1 := mathutil.Abs(-1) + result2 := mathutil.Abs(-0.1) + result3 := mathutil.Abs(float32(0.2)) fmt.Println(result1) fmt.Println(result2) @@ -978,3 +1127,38 @@ func main() { // 0.2 } ``` + +### Div + +

returns the result of x divided by y.

+ +Signature: + +```go +func Div[T constraints.Float | constraints.Integer](x T, y T) float64 +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/mathutil" +) + +func main() { + result1 := mathutil.Div(9, 4) + result2 := mathutil.Div(1, 2) + result3 := mathutil.Div(0, 666) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + // Output: + // 2.25 + // 0.5 + // 0 +} +``` \ No newline at end of file diff --git a/mathutil/mathutil.go b/mathutil/mathutil.go index 4248d99c..7f5d20cf 100644 --- a/mathutil/mathutil.go +++ b/mathutil/mathutil.go @@ -66,7 +66,7 @@ func Percent(val, total float64, n int) float64 { return result } -// RoundToString round up to n decimal places. +// RoundToString round off to n decimal places. // Play: https://go.dev/play/p/kZwpBRAcllO func RoundToString[T constraints.Float | constraints.Integer](x T, n int) string { tmp := math.Pow(10.0, float64(n)) @@ -76,7 +76,7 @@ func RoundToString[T constraints.Float | constraints.Integer](x T, n int) string return result } -// RoundToFloat round up to n decimal places. +// RoundToFloat round off to n decimal places. // Play: https://go.dev/play/p/ghyb528JRJL func RoundToFloat[T constraints.Float | constraints.Integer](x T, n int) float64 { tmp := math.Pow(10.0, float64(n)) @@ -100,6 +100,40 @@ func TruncRound[T constraints.Float | constraints.Integer](x T, n int) T { return T(result) } +// FloorToFloat round down to n decimal places. +func FloorToFloat[T constraints.Float | constraints.Integer](x T, n int) float64 { + tmp := math.Pow(10.0, float64(n)) + x *= T(tmp) + r := math.Floor(float64(x)) + return r / tmp +} + +// FloorToString round down to n decimal places. +func FloorToString[T constraints.Float | constraints.Integer](x T, n int) string { + tmp := math.Pow(10.0, float64(n)) + x *= T(tmp) + r := math.Floor(float64(x)) + result := strconv.FormatFloat(r/tmp, 'f', n, 64) + return result +} + +// CeilToFloat round up to n decimal places. +func CeilToFloat[T constraints.Float | constraints.Integer](x T, n int) float64 { + tmp := math.Pow(10.0, float64(n)) + x *= T(tmp) + r := math.Ceil(float64(x)) + return r / tmp +} + +// CeilToString round up to n decimal places. +func CeilToString[T constraints.Float | constraints.Integer](x T, n int) string { + tmp := math.Pow(10.0, float64(n)) + x *= T(tmp) + r := math.Ceil(float64(x)) + result := strconv.FormatFloat(r/tmp, 'f', n, 64) + return result +} + // Max return max value of numbers. // Play: https://go.dev/play/p/cN8DHI0rTkH func Max[T constraints.Integer | constraints.Float](numbers ...T) T { @@ -253,7 +287,7 @@ func PointDistance(x1, y1, x2, y2 float64) float64 { return math.Sqrt(c) } -// IsPrimes checks if number is prime number. +// IsPrime checks if number is prime number. // Play: https://go.dev/play/p/Rdd8UTHZJ7u func IsPrime(n int) bool { if n < 2 { @@ -351,3 +385,8 @@ func Abs[T constraints.Integer | constraints.Float](x T) T { return x } + +// Div returns the result of x divided by y. +func Div[T constraints.Float | constraints.Integer](x T, y T) float64 { + return float64(x) / float64(y) +} diff --git a/mathutil/mathutil_exmaple_test.go b/mathutil/mathutil_exmaple_test.go index 6b7992c4..4f77be4a 100644 --- a/mathutil/mathutil_exmaple_test.go +++ b/mathutil/mathutil_exmaple_test.go @@ -110,6 +110,66 @@ func ExampleTruncRound() { // 0.125 } +func ExampleCeilToFloat() { + 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 ExampleCeilToString() { + 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 ExampleFloorToFloat() { + 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 ExampleFloorToString() { + 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 ExampleAverage() { result1 := Average(1, 2) result2 := RoundToFloat(Average(1.2, 1.4), 1) @@ -404,3 +464,17 @@ func ExampleAbs() { // 0.1 // 0.2 } + +func ExampleDiv() { + 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 +} diff --git a/mathutil/mathutil_test.go b/mathutil/mathutil_test.go index fb7cf99e..3051a411 100644 --- a/mathutil/mathutil_test.go +++ b/mathutil/mathutil_test.go @@ -72,8 +72,8 @@ func TestRoundToString(t *testing.T) { assert.Equal("0.12", RoundToString(0.124, 2)) assert.Equal("0.13", RoundToString(0.125, 2)) assert.Equal("0.125", RoundToString(0.125, 3)) - //assert.Equal("54.321", RoundToString(54.321, 3)) - //assert.Equal("17.000", RoundToString(17, 3)) + assert.Equal("54.321", RoundToString(54.321, 3)) + assert.Equal("17.000", RoundToString(17, 3)) } func TestTruncRound(t *testing.T) { @@ -93,6 +93,51 @@ func TestTruncRound(t *testing.T) { assert.Equal(33.33, TruncRound(33.33333, 2)) } +func TestFloorToFloat(t *testing.T) { + t.Parallel() + + assert := internal.NewAssert(t, "TestFloorToFloat") + + assert.Equal(3.14, FloorToFloat(3.14159, 2)) + assert.Equal(3.141, FloorToFloat(3.14159, 3)) + assert.Equal(5.0, FloorToFloat(5, 4)) + assert.Equal(2.0, FloorToFloat(9/4, 2)) +} + +func TestFloorToString(t *testing.T) { + t.Parallel() + + assert := internal.NewAssert(t, "TestFloorToString") + + assert.Equal("3.14", FloorToString(3.14159, 2)) + assert.Equal("3.141", FloorToString(3.14159, 3)) + assert.Equal("5.0000", FloorToString(5, 4)) +} + +func TestCeilToFloat(t *testing.T) { + t.Parallel() + + assert := internal.NewAssert(t, "TestCeilToFloat") + + assert.Equal(3.15, CeilToFloat(3.14159, 2)) + assert.Equal(3.142, CeilToFloat(3.14159, 3)) + assert.Equal(5.0, CeilToFloat(5, 4)) + assert.Equal(2.25, CeilToFloat(float32(9)/float32(4), 2)) + assert.Equal(0.15, CeilToFloat(float64(1)/float64(7), 2)) +} + +func TestCeilToString(t *testing.T) { + t.Parallel() + + assert := internal.NewAssert(t, "TestCeilToFloat") + + assert.Equal("3.15", CeilToString(3.14159, 2)) + assert.Equal("3.142", CeilToString(3.14159, 3)) + assert.Equal("5.0000", CeilToString(5, 4)) + assert.Equal("2.25", CeilToString(float32(9)/float32(4), 2)) + assert.Equal("0.15", CeilToString(float64(1)/float64(7), 2)) +} + func TestAverage(t *testing.T) { t.Parallel() @@ -352,4 +397,19 @@ func TestAbs(t *testing.T) { assert.Equal(int64(1), Abs(int64(-1))) assert.Equal(float32(1), Abs(float32(-1))) + assert.Equal(math.Inf(1), Abs(math.Inf(-1))) +} + +func TestDiv(t *testing.T) { + t.Parallel() + + assert := internal.NewAssert(t, "TestDiv") + + assert.Equal(float64(2), Div(4, 2)) + assert.Equal(2.5, Div(5, 2)) + assert.Equal(0.5, Div(1, 2)) + assert.Equal(0.5, Div(1, 2)) + assert.Equal(math.Inf(1), Div(8, 0)) + assert.Equal(math.Inf(-1), Div(-8, 0)) + assert.Equal(true, math.IsNaN(Div(0, 0))) }