Welcome To Golang By Example

Cube Root of a number in Go (Golang)

Table of Contents

Overview

math package of GO provides a Cbrt method that can be used to get the cube root of that number.

Below is the signature of the function. It takes input a float and also returns a float.

func Cbrt(x float64) float64

Some special cases of Cbrt function are

Code:

package main

import (
    "fmt"
    "math"
)

func main() {
    res := math.Cbrt(8)
    fmt.Println(res)

    res = math.Cbrt(27)
    fmt.Println(res)

    res = math.Cbrt(30.33)
    fmt.Println(res)
}

Output:

2
3
3.118584170228812