有一道LeetCode测试326. 3的幂,使用Java实现了一个数学方法:
public class Solution {
public boolean isPowerOfThree(int n) {
return (Math.log(n) / Math.log(3) + epsilon) % 1 <= 2 * epsilon;
}
}
当我打算将这个解决方案转换为 Golang 时
import "math"
func isPowerOfThree(n int) bool {
return (math.Log10(float64(n)) / math.Log10(3)) % 1 == 0.0
}
然后就出现了编译错误,如下:
Line 4: Char 53: invalid operation: math.Log10(float64(n)) / math.Log10(3) % 1 (operator % not defined on float64) (solution.go)
我查看了math包,但没有像%
运算符那样的支持函数,在Golang中是否有任何有效的类似%
的运算符?非常感谢:)