Golang 找到给定数字的Log1p()

Golang 找到给定数字的Log1p()

Go语言提供了基本常量和数学函数的内建支持,使用math包可以对数字进行操作。您可以使用math包中提供的 Log1p()函数 找到1加它的指定参数的自然对数。因此,您需要使用import关键字在程序中添加一个math包来访问Log1p()函数。

语法:

func Log1p(a float64) float64
  • 当a的值接近零时,它比log(a+1)更准确。
  • 如果在这个函数中传递+Inf,则此函数将返回+Inf。
  • 如果在这个函数中传递+0或-0,则此函数将返回+0或-0。
  • 如果在这个函数中传递-1,则此函数将返回-Inf。
  • 如果a的值<-1,则此函数将返回NaN。
  • 如果在这个函数中传递NaN,则此函数将返回NaN。

示例1:

// Golang程序,演示如何找到给定数字的自然对数
  
package main
  
import (
    "fmt"
    "math"
)
  
// 主函数
func main() {
  
    // 使用Logb()函数,找到给定数字的自然对数
    res_1 := math.Log1p(0)
    res_2 := math.Log1p(1)
    res_3 := math.Log1p(math.Inf(1))
    res_4 := math.Log1p(math.NaN())
    res_5 := math.Log1p(36)
  
    // 显示结果
    fmt.Printf("Result 1: %.1f", res_1)
    fmt.Printf("\nResult 2: %.1f", res_2)
    fmt.Printf("\nResult 3: %.1f", res_3)
    fmt.Printf("\nResult 4: %.1f", res_4)
    fmt.Printf("\nResult 5: %.1f", res_5)
  
} 

输出:

Result 1: 0.0
Result 2: 0.7
Result 3: +Inf
Result 4: NaN
Result 5: 3.6

示例2:

// Golang程序,演示如何找到给定数字的自然对数
  
package main
  
import (
    "fmt"
    "math"
)
  
// 主函数
func main() {
  
    // 使用Log1p()函数,找到给定数字的自然对数
    nvalue_1 := math.Log1p(100)
    nvalue_2 := math.Log1p(26)
    res := nvalue_1 + nvalue_2
    fmt.Printf("%.5f + %.5f = %.5f", 
            nvalue_1, nvalue_2, res)
  
} 

输出:

4.61512 + 3.29584 = 7.91096

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程