mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
feat: add Log function
This commit is contained in:
@@ -323,3 +323,9 @@ func Cos(radian float64, precision ...int) float64 {
|
|||||||
func Sin(radian float64, precision ...int) float64 {
|
func Sin(radian float64, precision ...int) float64 {
|
||||||
return Cos((math.Pi / 2) - radian)
|
return Cos((math.Pi / 2) - radian)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Log retur the logarithm of base n.
|
||||||
|
// Play: todo
|
||||||
|
func Log(n, base float64) float64 {
|
||||||
|
return math.Log(n) / math.Log(base)
|
||||||
|
}
|
||||||
|
|||||||
@@ -362,3 +362,18 @@ func ExampleSin() {
|
|||||||
// 0
|
// 0
|
||||||
// 1
|
// 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleLog() {
|
||||||
|
result1 := Log(8, 2)
|
||||||
|
result2 := TruncRound(Log(5, 2), 2)
|
||||||
|
result3 := TruncRound(Log(27, 3), 0)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 3
|
||||||
|
// 2.32
|
||||||
|
// 3
|
||||||
|
}
|
||||||
|
|||||||
@@ -269,3 +269,11 @@ func TestSin(t *testing.T) {
|
|||||||
assert.EqualValues(0, Sin(math.Pi))
|
assert.EqualValues(0, Sin(math.Pi))
|
||||||
assert.EqualValues(1, Sin(math.Pi/2))
|
assert.EqualValues(1, Sin(math.Pi/2))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLog(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestLog")
|
||||||
|
|
||||||
|
assert.EqualValues(3, Log(8, 2))
|
||||||
|
assert.EqualValues(3, TruncRound(Log(27, 3), 0))
|
||||||
|
assert.EqualValues(2.32, TruncRound(Log(5, 2), 2))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user