golang 计算 math.Pow 导致数值变成了NaN
Python使用Python计算没有这个问题
>>> sR = 100 >>> sV = -18.29 >>> pow(pow(1+sV/2,1/100),sR) (-8.145000000000028+3.1086244689504383e-15j)
但使用GoLang 计算出来的结果是NaN
解决var result float64
if fmt.Sprintf("%v",result) == "NaN" {
result = 0
}
或者
if math.IsNaN(result) {
result = 0
}
封装函数
func GetString(f float64, percent bool) (s string) {
if math.IsInf(f, 0) {
return "Inf"
}
if math.IsNaN(f) {
return "NaN"
}
suffix := ""
if percent {
f *= 100
suffix += "%"
}
s = fmt.Sprintf("%d%s", int(math.Round(f)), suffix)
return
}



