Golang 找到浮点数的互补误差函数

Golang 找到浮点数的互补误差函数

Go语言提供了基本常量和数学函数的内置支持,使用math包可对数字执行操作。您可以使用math包提供的Erfc()函数找到指定数字的互补误差函数。因此,您需要在程序中添加一个math包,将import关键字用于访问Erfc()函数。

语法:

func Erfc(a float64) float64
  • 如果在此函数中传递+Inf,则该函数将返回0。
  • 如果在此函数中传递-Inf,则该函数将返回2。
  • 如果在此函数中传递NaN,则该函数将返回NaN。

示例1:

// 本Go程序演示如何找到给定数字的互补误差函数
 
package main
 
import (
    "fmt"
    "math"
)
 
//主函数
func main() {
 
    // 使用Erfc()函数找到互补误差函数
    res_1 := math.Erfc(4.8)
    res_2 := math.Erfc(math.Inf(-3))
    res_3 := math.Erfc(math.Inf(3))
    res_4 := math.Erfc(math.NaN())
 
    // 显示结果
    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: 0.0
Result 2: 2.0
Result 3: 0.0
Result 4: NaN

示例2:

// 本Go程序演示如何找到给定数字的互补误差函数
 
package main
 
import (
    "fmt"
    "math"
)
 
//主函数
func main() {
 
    // 使用Erfc()函数找到互补误差函数
    nvalue_1 := math.Erfc(math.Inf(-3))
    nvalue_2 := math.Erfc(4.9)
    res := nvalue_1 + nvalue_2
    fmt.Printf("%.1f + %.1f = %.1f",
                nvalue_1, nvalue_2, res)
 
} 

输出:

2.0 + 0.0 = 2.0

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程