📜  Golang 中的 math.Inf()函数示例

📅  最后修改于: 2021-10-24 13:34:15             🧑  作者: Mango

Go 语言为基本常量和数学函数提供内置支持,以在 math 包的帮助下对数字执行运算。借助 math 包提供的Inf()函数,您可以找到正无穷大(如果符号 >= 0)或负无穷大(如果符号 < 0)。因此,您需要在 import 关键字的帮助下在程序中添加一个数学包来访问 Inf()函数。

句法:

func Inf(sign int) float64

示例 1:

// Golang program to illustrate the
// math.Inf() Function 
package main
  
import (
    "fmt"
    "math"
)
  
// Main function
func main() {
  
    // Finding postive infinity
    // and negative infinity
    // Using Inf() function
    res_1 := math.Inf(-1)
    res_2 := math.Inf(1)
  
    // Displaying the result
    fmt.Println("Result 1: ", res_1)
    fmt.Println("Result 2: ", res_2)
  
}

输出:

Result 1: -Inf
Result 2: +Inf

示例 2:

// Golang program to illustrate the
// math.Inf() Function 
package main
  
import (
    "fmt"
    "math"
)
  
// Main function
func main() {
  
    // Finding postive infinity
    // and negative infinity
    // Using Inf() function
    nvalue := math.Inf(2)
    mvalue := math.Inf(-3)
  
    fmt.Println("Positive infinity: ", nvalue)
    fmt.Println("Negative infinity: ", mvalue)
  
}

输出:

Positive infinity:  +Inf
Negative infinity:  -Inf