power Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/power/ Sat, 28 Mar 2020 10:23:34 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 power Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/power/ 32 32 159787465 Calculate x^y – Pow() in Go (Golang) https://vikasboss.github.io/power-golang/ https://vikasboss.github.io/power-golang/#respond Sat, 28 Mar 2020 10:23:24 +0000 https://vikasboss.github.io/?p=1870 Overview math package of GO provides a Pow method that can be used to calculate x to the power y. Below is the signature of the function. It takes input as two...

The post Calculate x^y – Pow() in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

math package of GO provides a Pow method that can be used to calculate x to the power y.

Below is the signature of the function. It takes input as two float arguments and returns a float

func Pow(x, y float64) float64

The same function can also 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

Code

package main

import (
    "fmt"
    "math"
)

func main() {
    //Power for integers
    res := math.Pow(2, 10)
    fmt.Println(res)

    //Power for float
    res = math.Pow(1.5, 2)
    fmt.Println(res)

    //Anything to power 0 is 1
    res = math.Pow(3, 0)
    fmt.Println(res)
}

Output:

1024
2.25
1

The post Calculate x^y – Pow() in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/power-golang/feed/ 0 1870