Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer functions of square and square root

Tags:

go

Currently, math.Pow() and math.sqrt take float64 type arguments.

Do we have equivalent functions that take int type arguments?

like image 878
callmekatootie Avatar asked Jan 06 '15 17:01

callmekatootie


People also ask

What is the function of a square root?

(usually just referred to as the "square root function") is a function that maps the set of nonnegative real numbers onto itself. In geometrical terms, the square root function maps the area of a square to its side length.

What is the square root of an integer?

Given an integer x, find it's square root. If x is not a perfect square, then return floor(√x). Examples : Input: x = 4 Output: 2 Explanation: The square root of 4 is 2.

What is square & square root?

Squares are the numbers, generated after multiplying a value by itself. Whereas square root of a number is value which on getting multiplied by itself gives the original value. Hence, both are vice-versa methods. For example, the square of 2 is 4 and the square root of 4 is 2.


2 Answers

If your return value is a float, you can use Ceil or Floor from the math package and then convert it to an int.

n := 5.5
o := math.Floor(n)
p := int(math.Pow(o, 2))

fmt.Println("Float:", n)
fmt.Println("Floor:", o)
fmt.Println("Square:", p)

5.5
5
25

Keep in mind that Floor still returns a float64, so you will still want to wrap it in int()

like image 50
Devin Nathan-Turner Avatar answered Oct 21 '22 06:10

Devin Nathan-Turner


just create a float64 object using the int value. Example if int = 10.

var x float64 = 10
var b = math.Pow(2, x) 
like image 25
medium Avatar answered Oct 21 '22 08:10

medium