mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
feat: add Abs
This commit is contained in:
@@ -46,6 +46,8 @@ import (
|
|||||||
- [Sin](#Sin)
|
- [Sin](#Sin)
|
||||||
- [Log](#Log)
|
- [Log](#Log)
|
||||||
- [Sum](#Sum)
|
- [Sum](#Sum)
|
||||||
|
- [Abs](#Abs)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -942,4 +944,40 @@ func main() {
|
|||||||
// 3
|
// 3
|
||||||
// 1.1
|
// 1.1
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="Abs">Abs</span>
|
||||||
|
|
||||||
|
<p>Returns the absolute value of x.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Abs[T constraints.Integer | constraints.Float](x T) T
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
}
|
||||||
```
|
```
|
||||||
@@ -46,6 +46,7 @@ import (
|
|||||||
- [Sin](#Sin)
|
- [Sin](#Sin)
|
||||||
- [Log](#Log)
|
- [Log](#Log)
|
||||||
- [Sum](#Sum)
|
- [Sum](#Sum)
|
||||||
|
- [Abs](#Abs)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -941,3 +942,39 @@ func main() {
|
|||||||
// 1.1
|
// 1.1
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="Abs">Abs</span>
|
||||||
|
|
||||||
|
<p>求绝对值。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Abs[T constraints.Integer | constraints.Float](x T) T
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -341,3 +341,13 @@ func Sin(radian float64, precision ...int) float64 {
|
|||||||
func Log(n, base float64) float64 {
|
func Log(n, base float64) float64 {
|
||||||
return math.Log(n) / math.Log(base)
|
return math.Log(n) / math.Log(base)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Abs returns the absolute value of x.
|
||||||
|
// Play: todo
|
||||||
|
func Abs[T constraints.Integer | constraints.Float](x T) T {
|
||||||
|
if x < 0 {
|
||||||
|
return (-x)
|
||||||
|
}
|
||||||
|
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
|||||||
@@ -389,3 +389,18 @@ func ExampleLog() {
|
|||||||
// 2.32
|
// 2.32
|
||||||
// 3
|
// 3
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleAbs() {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -285,3 +285,16 @@ func TestLog(t *testing.T) {
|
|||||||
assert.EqualValues(3, TruncRound(Log(27, 3), 0))
|
assert.EqualValues(3, TruncRound(Log(27, 3), 0))
|
||||||
assert.EqualValues(2.32, TruncRound(Log(5, 2), 2))
|
assert.EqualValues(2.32, TruncRound(Log(5, 2), 2))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAbs(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestAbs")
|
||||||
|
|
||||||
|
assert.Equal(0, Abs(0))
|
||||||
|
assert.Equal(1, Abs(-1))
|
||||||
|
|
||||||
|
assert.Equal(0.1, Abs(-0.1))
|
||||||
|
|
||||||
|
assert.Equal(int64(1), Abs(int64(-1)))
|
||||||
|
assert.Equal(float32(1), Abs(float32(-1)))
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user