當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Golang math.Expm1用法及代碼示例

Go 語言為基本常量和數學函數提供內置支持,以在 math 包的幫助下對數字執行運算。您可以借助 math 包提供的 Expm1() 函數找到指定數字減去 1 的 base-e index ,即 e**a -1。因此,您需要借助 import 關鍵字在程序中添加一個數學包來訪問 Expm1() 函數。

用法:

func Expm1(a float64) float64
  • 如果在此函數中傳遞 +Inf,則此函數將返回 +Inf。
  • 如果在此函數中傳遞 -Inf,則此函數將返回 -1。
  • 在這個函數中,非常大的值溢出到 -1 或 +Inf。
  • 如果在此函數中傳遞 NaN,則此函數將返回 NaN。

範例1:


// Golang program to illustrate the Exmp1 function
  
package main
  
import (
    "fmt"
    "math"
)
  
// Main function
func main() {
  
    // Using Expm1() function
    res_1:= math.Expm1(4)
    res_2:= math.Expm1(-1)
    res_3:= math.Expm1(math.Inf(1))
    res_4:= math.Expm1(math.NaN())
  
    // Displaying the result
    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)
  
}

輸出:

Result 1:53.6
Result 2:-0.6
Result 3:+Inf
Result 4:NaN

範例2:


// Golang program to illustrate the Exmp1 function
  
package main
  
import (
    "fmt"
    "math"
)
  
// Main function
func main() {
  
    // Using Expm1() function
    nvalue_1:= math.Expm1(7.3)
    nvalue_2:= math.Expm1(-3)
    res:= nvalue_1 + nvalue_2
    fmt.Printf("%.1f + %.1f = %.1f",
            nvalue_1, nvalue_2, res)
  
}

輸出:

1479.3 + -1.0 = 1478.3 



相關用法


注:本文由純淨天空篩選整理自Kirti_Mangal大神的英文原創作品 math.Expm1 Function() in Golang With Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。