Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

golang

What is the Exp2 function in Golang?

Faraz Karim

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The Go programming language uses the Exp2 function to find the value of 22 raised to the power equaling the input xx. This is the same as 2x2^x, where xx is the input.

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

Function definition

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

Parameters

The Exp2 function takes a single argument of type float64. This argument represents the number xx in the formula 2x2^x.

Return value

The Exp2 function returns a single value of type float64, which results from raising 22 to the power of xx (xx 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 a NAN argument is passed, the return value is also NAN.

Examples

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

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

The following example shows how the Exp2 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.Exp2(x)
fmt.Print(x, "'s exponential value is ", y)
fmt.Print( "\n")
a := math.Inf(1)
b := math.Exp2(a)
fmt.Print(a, "'s exponential value is ", b)
}

RELATED TAGS

golang

CONTRIBUTOR

Faraz Karim
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring