×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang math.Pow() Function with Examples

Golang | math.Pow() Function: Here, we are going to learn about the Pow() function of the math package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 02, 2021

math.Pow()

The Pow() function is an inbuilt function of the math package which is used to get the x**y, the base-x exponential of y (we can say x to the power y).

It accepts two parameters (x, y) and returns the x**y, the base-x exponential of y.

Syntax

func Pow(x, y float64) float64

Parameters

  • x, y : The values to be used to find x**y (x to the power y).

Return Value

The return type of Pow() function is a float64, it returns the x**y, the base-x exponential of y.

Special Cases

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

// Golang program to demonstrate the
// example of math.Pow() Function

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Pow(2, 6))
	fmt.Println(math.Pow(2.5, 1.2))
	fmt.Println(math.Pow(-10.23, -20))
	fmt.Println(math.Pow(10, 5))
	fmt.Println(math.Pow(1.49, -2))

	fmt.Println(math.Pow(5.0, 0))
	fmt.Println(math.Pow(1, 10))
	fmt.Println(math.Pow(math.NaN(), 10))
	fmt.Println(math.Pow(math.Inf(1), 0))
}

Output:

64
3.002811084953578
6.3458138139042046e-21
100000
0.4504301608035674
1
1
NaN
1

Example 2

// Golang program to demonstrate the
// example of math.Pow() Function

package main

import (
	"fmt"
	"math"
)

func main() {
	var x float64
	var y float64
	var PowXY float64

	x = 2
	y = 8
	PowXY = math.Pow(x, y)
	fmt.Println(x, "to the power of", y, "is", PowXY)

	x = 5
	y = 3
	PowXY = math.Pow(x, y)
	fmt.Println(x, "to the power of", y, "is", PowXY)

	x = 0.5
	y = 15
	PowXY = math.Pow(x, y)
	fmt.Println(x, "to the power of", y, "is", PowXY)

	x = -5
	y = 0
	PowXY = math.Pow(x, y)
	fmt.Println(x, "to the power of", y, "is", PowXY)

	x = 1.23
	y = 10
	PowXY = math.Pow(x, y)
	fmt.Println(x, "to the power of", y, "is", PowXY)
}

Output:

2 to the power of 8 is 256
5 to the power of 3 is 125
0.5 to the power of 15 is 3.0517578125e-05
-5 to the power of 0 is 1
1.23 to the power of 10 is 7.92594609605189

Example 3:

// Golang program to demonstrate the
// example of math.Pow() Function
// Find the square and cube of the given number

package main

import (
	"fmt"
	"math"
)

func main() {
	var x float64
	var result float64

	// Assign the value
	x = 8.5
	// Calculate & print the square
	result = math.Pow(x, 2)
	fmt.Printf("Square of %.2f is %.2f\n", x, result)

	// Calculate & print the cube
	result = math.Pow(x, 3)
	fmt.Printf("Cube of %.2f is %.2f\n", x, result)
}

Output:

Square of 8.50 is 72.25
Cube of 8.50 is 614.12

Golang math Package Constants and Functions »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.