Currently, math.Pow()
and math.sqrt
take float64
type arguments.
Do we have equivalent functions that take int
type arguments?
(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.
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.
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.
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()
just create a float64 object using the int value. Example if int = 10.
var x float64 = 10
var b = math.Pow(2, x)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With