diff --git a/README.md b/README.md index 1cf0c81..749f5d2 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@
![Go version](https://img.shields.io/badge/go-%3E%3D1.16-9cf) -[![Release](https://img.shields.io/badge/release-1.2.5-green.svg)](https://github.com/duke-git/lancet/releases) +[![Release](https://img.shields.io/badge/release-1.2.6-green.svg)](https://github.com/duke-git/lancet/releases) [![GoDoc](https://godoc.org/github.com//duke-git/lancet?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet)](https://goreportcard.com/report/github.com/duke-git/lancet) [![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml) @@ -194,6 +194,23 @@ import "github.com/duke-git/lancet/function" - [Delay](https://github.com/duke-git/lancet/blob/main/docs/function.md#Delay) - [Watcher](https://github.com/duke-git/lancet/blob/main/docs/function.md#Watcher) + +### Mathutil package implements some functions for math calculation. + +```go +import "github.com/duke-git/lancet/mathutil" +``` + +#### Function list: +- [Exponent](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Exponent) +- [Fibonacci](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Fibonacci) +- [Factorial](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Factorial) +- [Percent](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Percent) +- [RoundToFloat](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RoundToFloat) +- [RoundToString](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RoundToString) +- [TruncRound](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#TruncRound) + + ### Netutil package contains functions to get net information and send http request. ```go diff --git a/README_zh-CN.md b/README_zh-CN.md index 6a2f131..5fe4c90 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -4,7 +4,7 @@
![Go version](https://img.shields.io/badge/go-%3E%3D1.16-9cf) -[![Release](https://img.shields.io/badge/release-1.2.5-green.svg)](https://github.com/duke-git/lancet/releases) +[![Release](https://img.shields.io/badge/release-1.2.6-green.svg)](https://github.com/duke-git/lancet/releases) [![GoDoc](https://godoc.org/github.com//duke-git/lancet?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet)](https://goreportcard.com/report/github.com/duke-git/lancet) [![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml) @@ -195,6 +195,20 @@ import "github.com/duke-git/lancet/function" - [Delay](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Delay) - [Watcher](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Watcher) +### mathutil包实现了一些数学计算的函数。 + +```go +import "github.com/duke-git/lancet/mathutil" +``` + +#### Function list: +- [Exponent](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Exponent) +- [Fibonacci](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Fibonacci) +- [Factorial](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Factorial) +- [Percent](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Percent) +- [RoundToFloat](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RoundToFloat) +- [RoundToString](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RoundToString) +- [TruncRound](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#TruncRound) ### netutil网络包支持获取ip地址,发送http请求。 diff --git a/docs/mathutil.md b/docs/mathutil.md new file mode 100644 index 0000000..b911f38 --- /dev/null +++ b/docs/mathutil.md @@ -0,0 +1,233 @@ +# Mathutil +Package mathutil implements some functions for math calculation. + +
+ +## Source: + +[https://github.com/duke-git/lancet/blob/main/mathutil/mathutil.go](https://github.com/duke-git/lancet/blob/main/mathutil/mathutil.go) + + +
+ +## Example: +```go +import ( + "github.com/duke-git/lancet/mathutil" +) +``` + +
+ +## Index +- [Exponent](#Exponent) +- [Fibonacci](#Fibonacci) +- [Factorial](#Factorial) +- [Percent](#Percent) +- [RoundToFloat](#RoundToFloat) +- [RoundToString](#RoundToString) +- [TruncRound](#TruncRound) + +
+ +## Documentation + + +### Exponent +

Calculate x to the nth power.

+ +Signature: + +```go +func Exponent(x, n int64) int64 +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/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: + +```go +func Fibonacci(first, second, n int) int +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/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: + +```go +func Factorial(x uint) uint +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/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 +} +``` + + + +### Percent +

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

+ +Signature: + +```go +func Percent(val, total float64, n int) float64 +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/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: + +```go +func RoundToFloat(x float64, n int) float64 +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/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: + +```go +func RoundToString(x float64, n int) string +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/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: + +```go +func TruncRound(x float64, n int) float64 +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/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 +} +``` + + + diff --git a/docs/mathutil_zh-CN.md b/docs/mathutil_zh-CN.md new file mode 100644 index 0000000..a0ceb6a --- /dev/null +++ b/docs/mathutil_zh-CN.md @@ -0,0 +1,234 @@ +# Mathutil +mathutil包实现了一些数学计算的函数. + +
+ +## 源码: + +[https://github.com/duke-git/lancet/blob/main/mathutil/mathutil.go](https://github.com/duke-git/lancet/blob/main/mathutil/mathutil.go) + + +
+ +## 用法: +```go +import ( + "github.com/duke-git/lancet/mathutil" +) +``` + +
+ +## 目录 +- [Exponent](#Exponent) +- [Fibonacci](#Fibonacci) +- [Factorial](#Factorial) + +- [Percent](#Percent) +- [RoundToFloat](#RoundToFloat) +- [RoundToString](#RoundToString) +- [TruncRound](#TruncRound) + +
+ +## Documentation + + +### Exponent +

指数计算(x的n次方)

+ +函数签名: + +```go +func Exponent(x, n int64) int64 +``` +例子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/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 +

计算斐波那契数列的第n个数

+ +函数签名: + +```go +func Fibonacci(first, second, n int) int +``` +例子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/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 +

计算阶乘

+ +函数签名: + +```go +func Factorial(x uint) uint +``` +例子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/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 +} +``` + + + +### Percent +

计算百分比,保留n位小数

+ +函数签名: + +```go +func Percent(val, total float64, n int) float64 +``` +例子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/mathutil" +) + +func main() { + fmt.Println(mathutil.Percent(1, 2, 2)) //1 + fmt.Println(mathutil.Percent(0.1, 0.3, 2)) //33.33 +} +``` + + + +### RoundToFloat +

四舍五入,保留n位小数

+ +函数签名: + +```go +func RoundToFloat(x float64, n int) float64 +``` +例子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/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 +

四舍五入,保留n位小数,返回字符串

+ +函数签名: + +```go +func RoundToString(x float64, n int) string +``` +例子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/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 +

截短n位小数(不进行四舍五入)

+ +函数签名: + +```go +func TruncRound(x float64, n int) float64 +``` +例子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/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 +} +``` + + + diff --git a/mathutil/mathutil.go b/mathutil/mathutil.go new file mode 100644 index 0000000..aada9d2 --- /dev/null +++ b/mathutil/mathutil.go @@ -0,0 +1,92 @@ +// Copyright 2021 dudaodong@gmail.com. All rights reserved. +// Use of this source code is governed by MIT license + +// Package mathutil implements some functions for math calculation. +package mathutil + +import ( + "fmt" + "math" + "strconv" + "strings" +) + +// Exponent calculate x^n +func Exponent(x, n int64) int64 { + if n == 0 { + return 1 + } + + t := Exponent(x, n/2) + + if n%2 == 1 { + return t * t * x + } + + return t * t +} + +// Fibonacci calculate fibonacci number before n +func Fibonacci(first, second, n int) int { + if n <= 0 { + return 0 + } + if n < 3 { + return 1 + } else if n == 3 { + return first + second + } else { + return Fibonacci(second, first+second, n-1) + } +} + +// Factorial calculate x! +func Factorial(x uint) uint { + var f uint = 1 + for ; x > 1; x-- { + f *= x + } + return f +} + +// Percent calculate the percentage of val to total +func Percent(val, total float64, n int) float64 { + if total == 0 { + return float64(0) + } + tmp := val / total * 100 + res := RoundToFloat(tmp, n) + + return res +} + +// RoundToString round up to n decimal places +func RoundToString(x float64, n int) string { + tmp := math.Pow(10.0, float64(n)) + x *= tmp + x = math.Round(x) + res := strconv.FormatFloat(x/tmp, 'f', n, 64) + return res +} + +// RoundToFloat round up to n decimal places +func RoundToFloat(x float64, n int) float64 { + tmp := math.Pow(10.0, float64(n)) + x *= tmp + x = math.Round(x) + return x / tmp +} + +// TruncRound round off n decimal places +func TruncRound(x float64, n int) float64 { + floatStr := fmt.Sprintf("%."+strconv.Itoa(n+1)+"f", x) + temp := strings.Split(floatStr, ".") + var newFloat string + if len(temp) < 2 || n >= len(temp[1]) { + newFloat = floatStr + } else { + newFloat = temp[0] + "." + temp[1][:n] + } + res, _ := strconv.ParseFloat(newFloat, 64) + return res +} diff --git a/mathutil/mathutil_test.go b/mathutil/mathutil_test.go new file mode 100644 index 0000000..e2fd1e1 --- /dev/null +++ b/mathutil/mathutil_test.go @@ -0,0 +1,72 @@ +package mathutil + +import ( + "testing" + + "github.com/duke-git/lancet/internal" +) + +func TestExponent(t *testing.T) { + assert := internal.NewAssert(t, "TestExponent") + + assert.Equal(int64(1), Exponent(10, 0)) + assert.Equal(int64(10), Exponent(10, 1)) + assert.Equal(int64(100), Exponent(10, 2)) +} + +func TestFibonacci(t *testing.T) { + assert := internal.NewAssert(t, "TestFibonacci") + + assert.Equal(0, Fibonacci(1, 1, 0)) + assert.Equal(1, Fibonacci(1, 1, 1)) + assert.Equal(1, Fibonacci(1, 1, 2)) + assert.Equal(5, Fibonacci(1, 1, 5)) +} + +func TestFactorial(t *testing.T) { + assert := internal.NewAssert(t, "TestFactorial") + + assert.Equal(uint(1), Factorial(0)) + assert.Equal(uint(1), Factorial(1)) + assert.Equal(uint(2), Factorial(2)) + assert.Equal(uint(6), Factorial(3)) +} + +func TestPercent(t *testing.T) { + assert := internal.NewAssert(t, "TestPercent") + + assert.Equal(float64(50), Percent(1, 2, 2)) + assert.Equal(float64(33.33), Percent(0.1, 0.3, 2)) +} + +func TestRoundToFloat(t *testing.T) { + assert := internal.NewAssert(t, "TestRoundToFloat") + + assert.Equal(RoundToFloat(0, 0), float64(0)) + assert.Equal(RoundToFloat(0, 1), float64(0)) + assert.Equal(RoundToFloat(0.124, 2), float64(0.12)) + assert.Equal(RoundToFloat(0.125, 2), float64(0.13)) + assert.Equal(RoundToFloat(0.125, 3), float64(0.125)) + assert.Equal(RoundToFloat(33.33333, 2), float64(33.33)) +} + +func TestRoundToString(t *testing.T) { + assert := internal.NewAssert(t, "TestRoundToString") + + assert.Equal(RoundToString(0, 0), "0") + assert.Equal(RoundToString(0, 1), "0.0") + assert.Equal(RoundToString(0.124, 2), "0.12") + assert.Equal(RoundToString(0.125, 2), "0.13") + assert.Equal(RoundToString(0.125, 3), "0.125") +} + +func TestTruncRound(t *testing.T) { + assert := internal.NewAssert(t, "TestTruncRound") + + assert.Equal(TruncRound(0, 0), float64(0)) + assert.Equal(TruncRound(0, 1), float64(0)) + assert.Equal(TruncRound(0.124, 2), float64(0.12)) + assert.Equal(TruncRound(0.125, 2), float64(0.12)) + assert.Equal(TruncRound(0.125, 3), float64(0.125)) + assert.Equal(TruncRound(33.33333, 2), float64(33.33)) +}