Golang math.Lgamma()函数的使用及示例说明

Golang math.Lgamma()函数的使用及示例说明

Go语言提供了基本常数和数学函数的内置支持,使用math包可以对数字进行操作。该包提供了 Lgamma()函数, 用于查找Gamma(a)的自然对数和符号(-1或+1)。因此,您需要在您的程序中使用import关键字添加math包以访问Lgamma()函数。

语法:

func Lgamma(a float64) (lgamma float64, sign int)
  • 如果Lgamma(+Inf),则该函数将返回+Inf。
  • 如果Lgamma(0),则该函数将返回+Inf。
  • 如果Lgamma(-整数),则该函数将返回+Inf。
  • 如果Lgamma(-Inf),则该函数将返回-Inf。
  • 如果Lgamma(NaN),则该函数将返回NaN。

示例1:

// Golang程序演示
// math.Lgamma()函数
package main
  
import (
    "fmt"
    "math"
)
  
// 主函数
func main() {
  
    // 使用Lgamma()函数查找Gamma()的自然对数
    res_1, s1 := math.Lgamma(0)
    res_2, s2 := math.Lgamma(-2.3)
    res_3, s3 := math.Lgamma(math.Inf(-2))
    res_4, s4 := math.Lgamma(-2)
    res_5, s5 := math.Lgamma(math.NaN())
  
    // 显示结果
    fmt.Printf("\nResult 1: %f and sign: %d", res_1, s1)
    fmt.Printf("\nResult 2: %f and sign: %d", res_2, s2)
    fmt.Printf("\nResult 3: %f and sign: %d", res_3, s3)
    fmt.Printf("\nResult 4: %f and sign: %d", res_4, s4)
    fmt.Printf("\nResult 5: %f and sign: %d", res_5, s5)
} 

输出:

Result 1: +Inf and sign: 1
Result 2: 0.369567 and sign: -1
Result 3: -Inf and sign: 1
Result 4: +Inf and sign: 1
Result 5: NaN and sign: 1

示例2:

// Golang程序演示
// math.Lgamma()函数
package main
  
import (
    "fmt"
    "math"
)
  
// 主函数
func main() {
  
    // 使用Lgamma()函数查找Gamma()的自然对数
    nvalue_1, sign_1 := math.Lgamma(1.5)
    nvalue_2, sign_2 := math.Lgamma(3.45)
  
    // 查找自然对数和的总和 
    res := nvalue_1 + nvalue_2
    fmt.Printf("Result 1: %f and sign: %d", nvalue_1, sign_1)
    fmt.Printf("\nResult 2: %f and sign: %d \n", nvalue_2, sign_2)
    fmt.Println("Sum of Result 1 and Result 2: ", res)
  
} 

输出:

Result 1: -0.120782 and sign: 1
Result 2: 1.146231 and sign: 1 
Sum of Result 1 and Result 2:  1.0254487526135667

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程