Home »
Golang »
Golang Reference
Golang math.Exp2() Function with Examples
Golang | math.Exp2() Function: Here, we are going to learn about the Exp2() function of the math package with its usages, syntax, and examples.
Submitted by IncludeHelp, on August 30, 2021
math.Exp2()
The Exp2() function is an inbuilt function of the math package which is used to get the 2**x, the base-2 exponential of x, where x is the given parameter.
It accepts a parameter (x) and returns the 2**x, the base-2 exponential of x.
Syntax
func Exp2(x float64) float64
Parameters
- x : The value of float64 type whose base-2 is exponential to be found.
Return Value
The return type of Exp2() function is a float64, it returns the 2**x, the base-2 exponential of x.
Special cases are:
- Exp2(+Inf) = +Inf
If the parameter is the positive infinity, the function returns the same (+Inf).
- Exp2(NaN) = NaN
If the parameter is NaN (Not-A-Number), the function returns the same (NaN).
Example 1
// Golang program to demonstrate the
// example of math.Exp2() Function
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(math.Exp2(0))
fmt.Println(math.Exp2(5))
fmt.Println(math.Exp2(-5))
fmt.Println(math.Exp2(10.5))
fmt.Println(math.Exp2(-10.5))
fmt.Println(math.Exp2(math.Inf(1)))
fmt.Println(math.Exp2(math.NaN()))
}
Output:
1
32
0.03125
1448.1546878700492
0.0006905339660024878
+Inf
NaN
Example 2
// Golang program to demonstrate the
// example of math.Exp2() Function
package main
import (
"fmt"
"math"
)
func main() {
var x float64
var Exp2X float64
x = 0
Exp2X = math.Exp2(x)
fmt.Println("Exp2(", x, ") = ", Exp2X)
x = 0.5
Exp2X = math.Exp2(x)
fmt.Println("Exp2(", x, ") = ", Exp2X)
x = 6
Exp2X = math.Exp2(x)
fmt.Println("Exp2(", x, ") = ", Exp2X)
x = -6
Exp2X = math.Exp2(x)
fmt.Println("Exp2(", x, ") = ", Exp2X)
x = -0.15
Exp2X = math.Exp2(x)
fmt.Println("Exp2(", x, ") = ", Exp2X)
x = math.NaN()
Exp2X = math.Exp2(x)
fmt.Println("Exp2(", x, ") = ", Exp2X)
x = math.Inf(1)
Exp2X = math.Exp2(x)
fmt.Println("Exp2(", x, ") = ", Exp2X)
}
Output:
Exp2( 0 ) = 1
Exp2( 0.5 ) = 1.414213562373095
Exp2( 6 ) = 64
Exp2( -6 ) = 0.015625
Exp2( -0.15 ) = 0.9012504626108302
Exp2( NaN ) = NaN
Exp2( +Inf ) = +Inf
Golang math Package Constants and Functions »