To calculate the Power or Exponent of a number x^y in Golang, follow these steps:
- Import the math package in the golang program
- Use the math.Pow() function to calculate the power of the number
- Golang math Pow function returns the x**y, the base-x exponential of y.
In this article, we will discuss how to use the math Pow function to calculate the power or exponent of a number.
How to Calculate the Power of a Number in Golang Pow()
Use the math.Pow() function to calculate the power of a number. The Golang Pow function takes two parameters and finds the base-x exponential of y for x^y.
Let’s understand the golang exponent for a number using the below program.
package main import ( "fmt" "math" ) func main() { // Find the 2 to the power of 3, cube of a number ans := math.Pow(2, 3) fmt.Println(ans) // Find the 3.5 exponential of 2 ans = math.Pow(3.5, 2) fmt.Println(ans) // Calculate the 1 to the power of 0 ans = math.Pow(1, 0) fmt.Println(ans) // Calculate 0 exponential of 1 ans = math.Pow(0, 1) fmt.Println(ans) }
Output
8
12.25
1
0
In the above Golang program, we imported the math package.
To find the exponent of a number, use the golang math Pow function and pass the input parameters to it.
For example, to find the cube of a number, use the Golang math Pow function that takes input parameters math.Pow(2, 3)
and finds the 2 raised to the 3 power.
Cool Tip: How to get quotient or remainder in Golang!
How to Calculate the Square of a Number in Golang
To find the square of a number in golang, use the Golang math.Pow() function. It takes two input parameters of type float64 and returns the x to the power of y.
Let’s understand, the square of a number using the below golang program.
package main import ( "fmt" "math" ) func main() { ans := math.Pow(2, 2) fmt.Printf("%.1f", ans) }
Output
4.0
In the above Golang program, we imported a math package to use the Pow
function.
The Pow () function in go, takes the two number and calculate the 2 to the power of 2 and results in the calculation of a square for a given number.
How to Calculate a Power of 10 in Golang
Golang math package has a Pow10 function that calculates the power of 10 and returns the base-10 exponential of a given number.
Let’s understand how to calculate the Power of 10 for the n exponentials in golang.
package main import ( "fmt" "math" ) func main() { // find power of 10 to the exponent of 3 ans := math.Pow10(3) fmt.Println(ans) ans = math.Pow10(2) fmt.Println(ans) }
Output
1000
100
Golang Pow() function Syntax and Special Cases
The Pow () function is available in the math package.
func Pow Syntax
func Pow(x,y float64) float64
Where,
Parameters: x and y are of type float64
Returns: Returns x to the power of y
Special Cases of math.Pow() function are:
Pow(x, ±0) = 1 for any x
Pow(1, y) = 1 for any y
Pow(x, 1) = x for any x
Pow(NaN, y) = NaN
Pow(x, NaN) = NaN
Pow(±0, y) = ±Inf for y an odd integer < 0
Pow(±0, -Inf) = +Inf
Pow(±0, +Inf) = +0
Pow(±0, y) = +Inf for finite y < 0 and not an odd integer
Pow(±0, y) = ±0 for y an odd integer > 0
Pow(±0, y) = +0 for finite y > 0 and not an odd integer
Pow(-1, ±Inf) = 1
Pow(x, +Inf) = +Inf for |x| > 1
Pow(x, -Inf) = +0 for |x| > 1
Pow(x, +Inf) = +0 for |x| < 1
Pow(x, -Inf) = +Inf for |x| < 1
Pow(+Inf, y) = +Inf for y > 0
Pow(+Inf, y) = +0 for y < 0
Pow(-Inf, y) = Pow(-0, -y)
Pow(x, y) = NaN for finite x < 0 and finite non-integer y
Conclusion
I hope the above article helped you to understand the Golang math Pow function to calculate the power of a number of the exponential of a number.
You can find more topics about the Golang tutorials on the GolangSpot Home page.
Recommended Content
Find the Square Root of a Number in Golang
Find the log of a number in Golang