Golang : Math pow(the power of x^y) example
No programming language will be complete without the power of
math function. This tutorial is a simple example on how to use math.Pow()
function.
In Math
2^5 = 2x2x2x2x2 =32
In Golang
Math.pow(2, 5)
In code :
package main
import (
"fmt"
"math"
)
func main() {
result := math.Pow(2, 5)
fmt.Println(" 2 power of 5 is : ", result)
x := 10.5
y := 5
floatResult := math.Pow(x, float64(y)) //type cast int to float64
fmt.Printf(" %.02f power of %d is : %.02f \n", x, y, floatResult)
}
2 power of 5 is : 32
10.50 power of 5 is : 127628.16
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+9.2k Golang : cannot assign type int to value (type uint8) in range error
+5.5k Android Studio : Hello World example
+4.7k Golang : Markov chains to predict probability of next state example
+37.9k Golang : How to iterate over a []string(array)
+7.2k Golang : Metaprogramming example of wrapping a function
+4.4k Unix/Linux/MacOSx : How to remove an environment variable ?
+17.7k Golang : Delete duplicate items from a slice/array
+16.5k Golang : How to log each HTTP request to your web server?
+12.8k Golang : convert(cast) string to float value
+15.4k Golang : Set up source IP address before making HTTP request
+6.8k Golang : Sort words with first uppercase letter
+2k Golang : Fix go-cron set time not working issue