What is the Cbrt function in Golang?

The Go programming language uses the Cbrt function to find the cubic root of a number.

To use this function, you must import the math package in your file and access the Cbrt function within it using the . notation (math.Cbrt). Here, Cbrt is the actual function, while math is the Go package that stores the definition of this function.

Function definition

The definition of the Cbrt function inside the math package is:

Parameters

The Cbrt function takes a single argument of type float64. This is the number you will want to find the cube root of.

Return value

The Cbrt function returns a single value of type float64. This value represents the cube root of the argument.

Below are two types of return values that are only used by the function under certain circumstances:

  • NAN: Not a number, or NAN, is returned in all cases where the input argument is an undefined numeric value.

  • + Inf: This is returned if the input value is positive infinity or equivalent.

  • - Inf: This is returned if the input value is negative infinity or equivalent.

Examples

Below is a simple example where we find out the cube-root of a positive number:

package main
import "fmt"
import "math"
func main() {
var x float64 = math.Cbrt(125)
fmt.Println(x)
}

The following is a program that shows how the function will behave if it is given infinity as argument:

package main
import "fmt"
import "math"
func main() {
var x float64 = math.Cbrt(math.Inf(-1))
fmt.Println("Cuberoot of negative infinity is: ", x)
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved