square root Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/square-root/ Thu, 26 Mar 2020 05:40:25 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 square root Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/square-root/ 32 32 159787465 Square Root of a number in Go (Golang) https://vikasboss.github.io/square-root-number-golang/ https://vikasboss.github.io/square-root-number-golang/#respond Thu, 26 Mar 2020 05:40:18 +0000 https://vikasboss.github.io/?p=1846 Overview math package of GO provides a Sqrt method that can be used to get the cube root of that number. Below is the signature of the function. It takes input a...

The post Square Root of a number in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

math package of GO provides a Sqrt 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 Sqrt(x float64) float64

Some special cases of Sqrt function are

  • Sqrt(±0) = ±0
  • Sqrt(±Inf) = ±Inf
  • Sqrt(x < 0) = NaN
  • Sqrt(NaN) = NaN

Code:

package main

import (
	"fmt"
	"math"
)

func main() {
	//Square root of a integer
	res := math.Sqrt(4)
	fmt.Println(res)

	//Square root of a integer
	res = math.Sqrt(9)
	fmt.Println(res)

	//Square Root of a float
	res = math.Sqrt(30.33)
	fmt.Println(res)

	//Square Root of a negative number
	res = math.Sqrt(-9)
	fmt.Println(res)
}

Output:

2
3
5.5072679252057455

The post Square Root of a number in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/square-root-number-golang/feed/ 0 1846