×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang math.Erf() Function with Examples

Golang | math.Erf() Function: Here, we are going to learn about the Erf() function of the math package with its usages, syntax, and examples.
Submitted by IncludeHelp, on August 30, 2021

math.Erf()

The Erf() function is an inbuilt function of the math package which is used to get the error function of the given parameter.

It accepts a parameter (x) and returns the error function of x.

Syntax

func Erf(x float64) float64

Parameters

  • x : The value of float64 type whose error function is to be found.

Return Value

The return type of Erf() function is a float64, it returns the error function of the given parameter.

Special cases are:

  • Erf(+Inf) = 1
    If the parameter is the positive infinity, the function returns 1.
  • Erf(-Inf) = -1
    If the parameter is the negative infinity, the function returns -1.
  • Erf(NaN) = NaN
    If the parameter is NaN (Not-A-Number), the function returns NaN.

Example 1

// Golang program to demonstrate the
// example of math.Erf() Function

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Erf(-10))
	fmt.Println(math.Erf(10))
	fmt.Println(math.Erf(-1))
	fmt.Println(math.Erf(1))
	fmt.Println(math.Erf(-0.89))
	fmt.Println(math.Erf(0.89))
	
	fmt.Println(math.Erf(math.Inf(-1)))
	fmt.Println(math.Erf(math.Inf(1)))
	fmt.Println(math.Erf(math.NaN()))
}

Output:

-1
1
-0.8427007929497149
0.8427007929497149
-0.7918432468144954
0.7918432468144954
-1
1
NaN

Example 2

// Golang program to demonstrate the
// example of math.Erf() Function

package main

import (
	"fmt"
	"math"
)

func main() {
	var x float64
	var ErfX float64

	x = -10
	ErfX = math.Erf(x)
	fmt.Println("Erf(", x, ") = ", ErfX)

	x = 0.89
	ErfX = math.Erf(x)
	fmt.Println("Erf(", x, ") = ", ErfX)

	x = -1
	ErfX = math.Erf(x)
	fmt.Println("Erf(", x, ") = ", ErfX)

	x = math.Inf(-1)
	ErfX = math.Erf(x)
	fmt.Println("Erf(", x, ") = ", ErfX)

	x = math.NaN()
	ErfX = math.Erf(x)
	fmt.Println("Erf(", x, ") = ", ErfX)
}

Output:

Erf( -10 ) =  -1
Erf( 0.89 ) =  0.7918432468144954
Erf( -1 ) =  -0.8427007929497149
Erf( -Inf ) =  -1
Erf( NaN ) =  NaN

Golang math Package Constants and Functions »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.