GoLang - Cbrt() and Sqrt()
In this tutorial, we will learn how to cube root and square root of the given number respectively with the help of in-built methods of the GO.
In the Go Language Cbrt() and Sqrt() functions are used to return the cube root and square root of the given number respectively. let us explore the uses cases of each separately with help of simple examples.
Cbrt()
Cbrt() is the method available in math package which will return the cube root of the given number.
Syntax:
math.Cbrt(value)
It will take value as parameter. It can be 0, Infinity etc. It is important to specify the "math" package in import.
Example 1:
Let us return the cube root of 0.
package main
import (
"fmt"
"math")
func main() {
// Cube root of 0
fmt.Println( math.Cbrt(0))
}
Output:
0Example 2:-
Let us return the cube root of some values which are greater than 0.
package main
import (
"fmt"
"math")
func main() {
// Cube root of 8
fmt.Println( math.Cbrt(8))
// Cube root of 64
fmt.Println( math.Cbrt(64))
// Cube root of 125
fmt.Println( math.Cbrt(125))
// Cube root of 123
fmt.Println( math.Cbrt(123))
}
Output:
2
4
5
4.973189833268591
Example 3:
Let us return the cube root of Infinite values
package main
import (
"fmt"
"math")
func main() {
// Cube root of Infinity -100
fmt.Println( math.Cbrt(math.Inf(-100)))
// Cube root of Infinity +100
fmt.Println( math.Cbrt(math.Inf(100)))
}
Output:
-Inf
+Inf
The cube root is negative Infinity when the infinity is negative and cube root is positive Infinity when the infinity is positive.
Example 4:
Let us return the cube root of Not a Number (NaN).
package main
import (
"fmt"
"math")
func main() {
// Cube root of Not a number
fmt.Println( math.Cbrt(math.NaN()))
}
Output:
NaN
Sqrt()
Sqrt() is the method available in math package which will return the square root of the given number. Syntax:
math.Sqrt(value)
It will take value as parameter. It can be 0, Infinity etc. It is important to specify the "math" package in import.
Example 1:
Let us return the square root of 0.
package main
import (
"fmt"
"math")
func main() {
// Square root of 0
fmt.Println( math.Sqrt(0))
}
Output:
0Example 2:
Let us return the square root of some values which are greater than 0.
package main
import (
"fmt"
"math")
func main() {
// Square root of 16
fmt.Println( math.Sqrt(16))
// Square root of 100
fmt.Println( math.Sqrt(100))
// Square root of 44
fmt.Println( math.Sqrt(44))
}
Output:
4
10
6.6332495807108
Example 3:
Let us return the square root of Infinite values
package main
import (
"fmt"
"math")
func main() {
// Square root of Infinity -100
fmt.Println( math.Sqrt(math.Inf(-100)))
// Square root of Infinity +100
fmt.Println( math.Sqrt(math.Inf(100)))
}
Output:
NaN
+Inf
The square root is NaN (Not a Number) when the infinity is negative and square root is positive Infinity when the infinity is positive.
Example 4:
Let us return the square root of Not a Number (NaN).
package main
import (
"fmt"
"math")
func main() {
// Square root of NaN
fmt.Println( math.Sqrt(math.NaN()))
}
Output:
NaN
Conclusion
Now we know how to find cube root and square root if a given number in a Golang using Cbrt() and Sqrt() functions and understood the implementation with working examples.