# Golang power calculation using math.Pow() function

Written By - Tuan Nguyen

## Getting started with math.Pow() function

Basic mathematical functions and constants are integrated into the Go language, allowing users to perform operations on numbers with the help of the `math` package. In this turorial we will concentrate on golang power calculation using `math.Pow()` function and other possible methods.

You can find the base `x` exponential of `y` `(x**y)` with the help of `Pow()` function provided by the math package. Below is the signature of the function. It takes input as two float arguments and returns a float:

`func Pow(x, y float64) float64:` Pow returns x**n, the base-x exponential of y.

This same function can be used to calculate the square or cube of a number. Just pass the second argument y as 2 in case of square and 3 in case of cube. Special cases are (in order):

``````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
``````

## Example 1: Use Pow() function to calculate power of integer, float

In this example, we will perform some simple exponential functions with integers and float with `Pow()`.

``````package main

import (
"fmt"
// import math package
"math"
)

func main() {
// calculate 2^9
result := math.Pow(2, 9)
fmt.Println(result)

// float inputs
res = math.Pow(4.5, 2)
fmt.Println(result)

// special case
res = math.Pow(1520, 0)
fmt.Println(result)
}
``````

Output:

```\$ go run main.go
512
20.25
1```

## Example 2: Implement own function to calculate Pow(x,y)

### 1. Implement Pow(x,y) based on Math.Pow()

In this example, we will discuss one problem when using `Pow()` with integers in Go and how to implement `Pow(x,y)` function yourself based on `Math.Pow()`. If we change the code in example 1 to:

``````	// calculate pow of integers inputs
a := 4
b := 10
result := math.Pow(a, b)
fmt.Println(result)``````

Output:

```.main.go:13:18: cannot use a (variable of type int) as type float64 in argument to math.Pow
.main.go:13:21: cannot use b (variable of type int) as type float64 in argument to math.Pow```

The problem is our inputs are integers while the `math.Pow()` takes float64 as inputs. You can read more about how to cast float64 to int64. We can write our own function to solve this problem:

``````package main

import (
"fmt"
"math"
)

func main() {
//calculate pow of integers inputs
a := 4
b := 10
result := calPowInt(a, b)
fmt.Println(result)
}

func calPowInt(x, y int) int {
return int(math.Pow(float64(x), float64(y)))
}``````

Output:

```\$ go run main.go
1048576```

### 2. Another way to do Pow(x,y)

We have several ways to write `Pow(x, n)` function for integers x and n. We will use `recursion` in this example:

``````package main

import (
"fmt"
)

func main() {
//calculate pow of integers inputs
a := 4
b := 10
result := calPowIntRecur(a, b)
fmt.Println(result)
}

// Assumption that n >= 0
func calPowIntRecur(x, n int) int {
if n == 0 {
return 1
}
if n == 1 {
return x
}
// recursion here
y := calPowIntRecur(x, n/2)
if n%2 == 0 {
return y * y
}
return x * y * y
}
``````

Output:

```\$ go run main.go
1048576```

## Summary

`math` package of GO provides a `Pow()` function that can be used to calculate x to the power y. It is powerful but in the case of integer inputs, we can write our own functions to avoid errors.

## References

Categories GO

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation. For any other feedbacks or questions you can either use the comments section or contact me form.