What is the Expm1 function in Golang?

The Go programming language uses the Expm1 function to find the value of ee raised to the power equaling the input xx, minus 11. This is the same as exe^x -1, where xx is the input.

To use this function, you must import the math package in your file and access the Expm1 function within it using the . notation (math.Expm1). Here, Expm1 is the actual function, while math is the Go package that stores the definition of this function.

Function definition

The definition of the Expm1 function inside the math package is:

Parameters

The Expm1 function takes a single argument of type float64. This argument represents the number xx in the formula exe^x -1.

Return value

The Expm1 function returns a single value of type float64, which results from raising ee to the power of xx, minus 11 (xx being the input float64).

An exception to the above statements is when you pass something that is positive infinity, negative 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.

  • -Inf: If the argument has a negative infinite value, the return value will be -1.

  • NAN: If a NAN argument is passed, the return value is also NAN.

Examples

Following is a simple example where we find out the Expm1 value of 5:

package main
import (
"fmt"
"math"
)
func main() {
x := 5.0
y := math.Expm1(x)
fmt.Print(x, "'s exponential value minus 1 is ", y)
}

The following example shows how the Expm1 function handles infinite valued arguments, for which we use the Inf function:

The Inf function returns an infinite value with a sign matching the sign of the argument that it is given.

package main
import (
"fmt"
"math"
)
func main() {
x := math.Inf(-1)
y := math.Expm1(x)
fmt.Print(x, "'s exponential value minus 1 is ", y)
fmt.Print( "\n")
a := math.Inf(1)
b := math.Expm1(a)
fmt.Print(a, "'s exponential value minus 1 is ", b)
}

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved