What is the Exp function in Golang?
The Go programming language uses the Exp function to find the value of raised to the power equaling the input . This is the same as , where is the input.
To use this function, you must import the math package in your file and access the Exp function within it using the . notation (math.Exp). Here, Exp is the actual function, while math is the Go package that stores the definition of this function.
Function definition
The definition of the Exp function inside the math package is:
Parameters
The Exp function takes a single argument of type float64. This argument represents the number in the formula .
Return value
The Exp function returns a single value of type float64, which results from raising to the power ( being the input float64).
An exception to the above statements is when you pass something that is positive infinity or NAN as an argument:
-
+Inf: If the argument has a positive infinite value, the return value will be exactly the same as the argument, i.e.,+Inf. -
NAN: If aNANargument is passed, the return value is alsoNAN.
Examples
Following is a simple example where we find out the exponential value of 5:
package mainimport ("fmt""math")func main() {x := 5.0y := math.Exp(x)fmt.Print(x, "'s exponential value is ", y)}
The following example shows how the Exp function handles infinite valued arguments, for which we use the Inf function:
The
Inffunction returns an infinite value with a sign matching the sign of the argument that it is given.
package mainimport ("fmt""math")func main() {x := math.Inf(-1)y := math.Exp(x)fmt.Print(x, "'s exponential value is ", y)fmt.Print( "\n")a := math.Inf(1)b := math.Exp(a)fmt.Print(a, "'s exponential value is ", b)}
Free Resources