Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Math.Pow with integers in Golang

I keep getting the error "cannot use a (type int) as type float64 in argument to math.Pow, cannot use x (type int) as type float64 in argument to math.Pow, invalid operation: math.Pow(a, x) % n (mismatched types float64 and int)"

func pPrime(n int) bool {
    var nm1 int = n - 1
    var x int = nm1/2
    a := 1;
    for  a < n {
        if  (math.Pow(a, x)) % n == nm1 {
            return true
        }
    }
    return false
}
like image 444
chidieberejoel Avatar asked Sep 28 '20 20:09

chidieberejoel


People also ask

Can Pow be used with integers?

The pow() function takes 'double' as the arguments and returns a 'double' value. This function does not always work for integers.

How do you use POW in Golang?

You can find the base-a exponential of b(a**b)with the help of Pow() function provided by the math package. So, you need to add a math package in your program with the help of the import keyword to access Pow() function. If Pow(a, ±0), then this method will return 1 for any a.

How do you calculate power in go?

🏋️‍♂️ Calculate the power of a number x^y in Go To raise a number X to the power of Y in Go, use the Pow(x, y float64) function from the math package. To calculate the power of 10 up to the Y exponent in Go, use the math. Pow10(n int) function.


3 Answers

func powInt(x, y int) int {
    return int(math.Pow(float64(x), float64(y)))
}

In case you have to reuse it and keep it a little more clean.

like image 121
nikoksr Avatar answered Oct 16 '22 22:10

nikoksr


If your inputs are int and the output is always expected to be int, then you're dealing with 32-bit numbers. It's more efficient to write your own function to handle this multiplication rather than using math.Pow. math.Pow, as mentioned in the other answers, expects 64-bit values.

Here's a Benchmark comparison for 15^15 (which approaches the upper limits for 32-bit representation):

// IntPow calculates n to the mth power. Since the result is an int, it is assumed that m is a positive power
func IntPow(n, m int) int {
    if m == 0 {
        return 1
    }
    result := n
    for i := 2; i <= m; i++ {
        result *= n
    }
    return result
}

// MathPow calculates n to the mth power with the math.Pow() function
func MathPow(n, m int) int {
    return int(math.Pow(float64(n), float64(m)))
}

The result:

go test -cpu=1 -bench=.
goos: darwin
goarch: amd64
pkg: pow
BenchmarkIntPow15   195415786            6.06 ns/op
BenchmarkMathPow15  40776524            27.8 ns/op

I believe the best solution is that you should write your own function similar to IntPow(m, n int) shown above. My benchmarks show that it runs more than 4x faster on a single CPU core compared to using math.Pow.

like image 27
Chris Concannon Avatar answered Oct 17 '22 00:10

Chris Concannon


Since nobody mentioned an efficient way (logarithmic) to do Pow(x, n) for integers x and n is as follows if you want to implement it yourself:

// Assumption: n >= 0
func PowInts(x, n int) int {
   if n == 0 { return 1 }
   if n == 1 { return x }
   y := PowInts(x, n/2)
   if n % 2 == 0 { return y*y }
   return x*y*y
}
like image 27
Eissa N. Avatar answered Oct 16 '22 23:10

Eissa N.