首页 > 解决方案 > 如何在 Golang 中将 Math.Pow 与整数一起使用

问题描述

我不断收到错误消息“不能在 math.Pow 的参数中使用 a(int 类型)作为 float64 类型,不能在 math.Pow 的参数中使用 x(int 类型)作为 float64 类型,无效操作:math.Pow(a, x ) % n (不匹配的类型 float64 和 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
}

标签: gofloating-pointtype-conversioninteger

解决方案


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

如果您必须重复使用它并保持它更干净一点。


推荐阅读