Go語言為基本常數和數學函數提供了內置支持,以借助數學包對數字進行運算。該軟件包提供了Lgamma()函數,該函數用於查找Gamma(a)的自然對數和符號(-1或+1)。因此,您需要借助import關鍵字在程序中添加數學包才能訪問Lgamma()函數。
用法:
func Lgamma(a float64) (lgamma float64, sign int)
- 如果為Lgamma(+ Inf),則此函數將返回+ Inf。
- 如果Lgamma(0),則此函數將返回+ Inf。
- 如果為Lgamma(-integer),則此函數將返回+ Inf。
- 如果為Lgamma(-Inf),則此函數將返回-Inf。
- 如果為Lgamma(NaN),則此函數將返回NaN。
範例1:
// Golang program to illustrate
// math.Lgamma() Function
package main
import (
"fmt"
"math"
)
// Main function
func main() {
// Finding the natural logarithm of Gamma()
// Using Lgamma() function
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())
// Displaying the result
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 program to illustrate
// math.Lgamma() Function
package main
import (
"fmt"
"math"
)
// Main function
func main() {
// Finding the natural logarithm of Gamma()
// Using Lgamma() function
nvalue_1, sign_1:= math.Lgamma(1.5)
nvalue_2, sign_2:= math.Lgamma(3.45)
// Finding the sum of the
// natural logarithm of Gamma()
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
相關用法
- Golang math.Float64bits()用法及代碼示例
- Golang atomic.AddInt64()用法及代碼示例
- Golang atomic.StoreInt64()用法及代碼示例
- Golang reflect.FieldByIndex()用法及代碼示例
- Golang string.Contains用法及代碼示例
- Golang bits.Sub()用法及代碼示例
- Golang io.PipeWriter.CloseWithError()用法及代碼示例
- Golang time.Round()用法及代碼示例
- Golang reflect.AppendSlice()用法及代碼示例
- Golang reflect.ChanOf()用法及代碼示例
- Golang flag.Bool()用法及代碼示例
- Golang time.Sleep()用法及代碼示例
- Golang time.Time.Year()用法及代碼示例
- Golang reflect.DeepEqual()用法及代碼示例
- Golang reflect.Indirect()用法及代碼示例
- Golang reflect.CanAddr()用法及代碼示例
- Golang reflect.CanInterface()用法及代碼示例
- Golang reflect.CanSet()用法及代碼示例
- Golang reflect.Cap()用法及代碼示例
- Golang strings.ContainsRune()用法及代碼示例
注:本文由純淨天空篩選整理自Kirti_Mangal大神的英文原創作品 math.Lgamma() Function in Golang with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。