Finding Error Function of Given Number in Golang


The error function is a mathematical function used in statistics and other fields. It is defined as the integral of the Gaussian function from 0 to x. In Golang, the math package provides the Erf() function to compute the error function of a given number. In this article, we will discuss how to find the error function of a given number in Golang.

Using the Erf() Function

The Erf() function is defined in the math package of Golang. It takes a float64 value as input and returns the error function of the input value as a float64 value. Here is the syntax of the Erf() function −

func Erf(x float64) float64

To use the Erf() function, we need to import the math package in our code. Here is an example program that finds the error function of a given number −

Example

package main

import (
   "fmt"
   "math"
)

func main() {
   x := 1.0
   result := math.Erf(x)
   fmt.Println(result)
}

Output

0.8427007929497149

In the above program, we have imported the "fmt" and "math" packages. We have defined a float64 variable "x" and assigned the value 1.0 to it. Then, we have used the math.Erf() function to compute the error function of x and stored the result in a variable named "result". Finally, we have printed the result using the fmt.Println() function.

Using the Erfc() Function

The Erfc() function is also provided by the math package in Golang. It is defined as the complementary error function of x, which is equal to 1 - Erf(x). Here is the syntax of the Erfc() function −

func Erfc(x float64) float64

To use the Erfc() function, we need to import the math package in our code. Here is an example program that finds the complementary error function of a given number −

Example

package main

import (
   "fmt"
   "math"
)

func main() {
   x := 1.0
   result := math.Erfc(x)
   fmt.Println(result)
}

Output

0.15729920705028513

In the above program, we have imported the "fmt" and "math" packages. We have defined a float64 variable "x" and assigned the value 1.0 to it. Then, we have used the math.Erfc() function to compute the complementary error function of x and stored the result in a variable named "result". Finally, we have printed the result using the fmt.Println() function.

Conclusion

In this article, we have discussed how to find the error function and complementary error function of a given number in Golang using the math package. The Erf() function is used to find the error function, while the Erfc() function is used to find the complementary error function. These functions are very useful in statistics and other fields where the Gaussian function is used.

Updated on: 12-Apr-2023

66 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements