Skip to content
Related Articles

Related Articles

Improve Article

math.Pow() Function in Golang With Examples

  • Last Updated : 13 Apr, 2020

Go language provides inbuilt support for basic constants and mathematical functions to perform operations on the numbers with the help of the math package. You can find the base-a exponential of b(a**b)with the help of Pow() function provided by the math package. So, you need to add a math package in your program with the help of the import keyword to access Pow() function.

Syntax:

func Pow(a, b float64) float64
  • If Pow(a, ±0), then this method will return 1 for any a.
  • If Pow(1, b), then this method will return 1 for any b.
  • If Pow(a, 1), then this method will return a for any a.
  • If Pow(NaN, b), then this method will return NaN.
  • If Pow(a, NaN), then this method will return NaN.
  • If Pow(±0, b), then this method will return ± Inf for b an odd integer < 0.
  • If Pow(±0, -Inf), then this method will return +Inf.
  • If Pow(±0, +Inf), then this method will return +0.
  • If Pow(±0, b), then this method will return +Inf for finite b < 0 and not an odd integer.
  • If Pow(±0, b), then this method will return ± 0 for b an odd integer > 0.
  • If Pow(±0, b), then this method will return +0 for finite b > 0 and not an odd integer.
  • If Pow(+1, ±Inf), then this method will return 1.
  • If Pow(a, +Inf), then this method will return +Inf for |a| > 1.
  • If Pow(a, -Inf), then this method will return +0 for |a| > 1.
  • If Pow(a, +Inf), then this method will return +0 for |a| < 1.
  • If Pow(a, -Inf), then this method will return +Inf for |a| < 1.
  • If Pow(+Inf, b), then this method will return +Inf for b > 0.
  • If Pow(+Inf, b), then this method will return +0 for b < 0.
  • If Pow(-Inf, b), then this method will return Pow(-0, -b).
  • If Pow(a, b), then this method will return NaN for finite a < 0 and finite non-integer b.

Example 1:




// Golang program to illustrate
// the use of math.Pow() function
  
package main
  
import (
    "fmt"
    "math"
)
  
// Main function
func main() {
  
    // Finding the base-a exponential of b
    // Using Pow() function
    res_1 := math.Pow(3, 5)
    res_2 := math.Pow(math.Inf(1), 3)
    res_3 := math.Pow(2, 0)
    res_4 := math.Pow(1, math.NaN())
    res_5 := math.Pow(-0, math.Inf(-1))
  
    // Displaying the result
    fmt.Printf("Result 1: %.1f", res_1)
    fmt.Printf("\nResult 2: %.1f", res_2)
    fmt.Printf("\nResult 3: %.1f", res_3)
    fmt.Printf("\nResult 4: %.1f", res_4)
    fmt.Printf("\nResult 5: %.1f", res_5)
}

Output:

Result 1: 243.0
Result 2: +Inf
Result 3: 1.0
Result 4: 1.0
Result 5: +Inf

Example 2:




// Golang program to illustrate
// the use of math.Pow() function
  
package main
  
import (
    "fmt"
    "math"
)
  
// Main function
func main() {
   
    // Finding the base-a exponential of b
    // Using Pow() function
    nvalue_1 := math.Pow(3, 4)
    nvalue_2 := math.Pow(5, 6)
  
    // Sum of the given numbers
    res := nvalue_1 + nvalue_2
    fmt.Printf("%.3f + %.3f = %.3f",
            nvalue_1, nvalue_2, res)
  
}

Output:

81.000 + 15625.000 = 15706.000



My Personal Notes arrow_drop_up
Recommended Articles
Page :