Golang math.Cbrt() Function with Examples

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

math.Cbrt()

The Cbrt() function is an inbuilt function of the math package which is used to get the cube root of the given value.

It accepts one parameter and returns the cube root.

Syntax:

func Cbrt(x float64) float64

Parameter(s):

  • x : The value whose cube root value is to be found.

Return Value:

The return type of Cbrt() function is a float64, it returns the cube root of the given value.

Special cases are:

  • Cbrt(±0) = ±0
    If the parameter is ±0, it returns the same (±0).
  • Cbrt(±Inf) = ±Inf
    If the parameter is ±Inf, it returns the same (±Inf).
  • Cbrt(NaN) = NaN
    If the parameter is NaN (Not-A-Number), it returns the same (NaN).

Example 1:

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

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Cbrt(27))
	fmt.Println(math.Cbrt(156.25))

	fmt.Println(math.Cbrt(-27))
	fmt.Println(math.Cbrt(-156.25))

	fmt.Println(math.Cbrt(-0))
	fmt.Println(math.Cbrt(0))

	fmt.Println(math.Cbrt(math.Inf(-1)))
	fmt.Println(math.Cbrt(math.Inf(1)))

	fmt.Println(math.Cbrt(math.NaN()))
}

Output:

3
5.386086725079709
-3
-5.386086725079709
0
0
-Inf
+Inf
NaN

Example 2:

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

package main

import (
	"fmt"
	"math"
)

func main() {
	var x float64
	var CbrtX float64

	x = 27
	CbrtX = math.Cbrt(x)
	fmt.Println("Cube root of", x, "is", CbrtX)

	x = -8
	CbrtX = math.Cbrt(x)
	fmt.Println("Cube root of", x, "is", CbrtX)

	x = 1
	CbrtX = math.Cbrt(x)
	fmt.Println("Cube root of", x, "is", CbrtX)

	x = -0.23
	CbrtX = math.Cbrt(x)
	fmt.Println("Cube root of", x, "is", CbrtX)

	x = -2
	CbrtX = math.Cbrt(x)
	fmt.Println("Cube root of", x, "is", CbrtX)

	x = 10
	CbrtX = math.Cbrt(x)
	fmt.Println("Cube root of", x, "is", CbrtX)

	x = math.Inf(-1)
	CbrtX = math.Cbrt(x)
	fmt.Println("Cube root of", x, "is", CbrtX)

	x = math.Inf(1)
	CbrtX = math.Cbrt(x)
	fmt.Println("Cube root of", x, "is", CbrtX)
}

Output:

Cube root of 27 is 3
Cube root of -8 is -2
Cube root of 1 is 1
Cube root of -0.23 is -0.6126925675228417
Cube root of -2 is -1.2599210498948732
Cube root of 10 is 2.154434690031884
Cube root of -Inf is -Inf
Cube root of +Inf is +Inf

Golang math Package Constants and Functions »




Comments and Discussions!

Load comments ↻





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