go 中的 hypot 实现疑问。
資深大佬 : sxshi110 55
go 中的 hypot 实现源码:
func hypot(p, q float64) float64 { // special cases switch { case IsInf(p, 0) || IsInf(q, 0): return Inf(1) case IsNaN(p) || IsNaN(q): return NaN() } p, q = Abs(p), Abs(q) if p < q { p, q = q, p } if p == 0 { return 0 } q = q / p return p * Sqrt(1+q*q) }
为什么不直接这样实现:
func hypot(p, q float64) float64 { switch { case IsInf(p, 0) || IsInf(q, 0): return Inf(1) case IsNaN(p) || IsNaN(q): return NaN() } return Sqrt(p*p+q*q) }
请教其中有什么差别
大佬有話說 (4)