Cube Root of a Number
To find cube root of a number in Go language, use Cbrt() function of math package. Cbrt(x) returns the cube root value of x.
In this tutorial, we will learn the syntax of Cbrt() function, and how to use this function to find the cube root of a given number.
Syntax
The syntax of Cbrt() function is
math.Cbrt(x)
where x
is a floating point value of type float64.
Please note that we have to import “math” package to use Cbrt() function.
Return Value
The function returns a floating point value of type float64.
Examples
Cube Root of Positive Number
In the following program, we take a positive value in x
, and find its cube root.
example.go
package main
import (
"fmt"
"math"
)
func main() {
var x float64 = 8
result := math.Cbrt(x)
fmt.Println("Cube root of 8 is :", result)
}
Output
Cube root of 8 is : 2
Cube Root of Negative Number
In the following program, we take a negative value in x
, and find its cube root.
example.go
package main
import (
"fmt"
"math"
)
func main() {
var x float64 = -8
result := math.Cbrt(x)
fmt.Println("Cube root of -8 is :", result)
}
Output
Cube root of -8 is : -2
Conclusion
In this Golang Tutorial, we learned how to find cube root of a given number in Go language, with example programs.