Integer functions of square and square roots

Currently math.Pow()

and math.sqrt

accept type arguments float64

.

Do we have equivalent functions that take type arguments int

?

+3


source to share


4 answers


If your return value is a float, you can use Ceil or Floor from the math package and then convert it to 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 float64, so you'll want to wrap it in int () anyway

+2


source


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



var x float64 = 10
var b = math.Pow(2, x) 

      

+1


source


What you can do is cast the float to your value.

int a=10,b=2;
math.Pow(float(a),float(b));

      

0


source


There are fast approximation algorithms described in other SO sections such as this one . If performance is important, porting one of the C algorithms to Go can be worth the effort.

0


source







All Articles