1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
8 Hypot -- sqrt(p*p + q*q), but overflows only if the result does.
11 // Hypot computes Sqrt(p*p + q*q), taking care to avoid
12 // unnecessary overflow and underflow.
15 // Hypot(p, q) = +Inf if p or q is infinite
16 // Hypot(p, q) = NaN if p or q is NaN
17 func Hypot(p
, q
float64) float64 {
18 // TODO(rsc): Remove manual inlining of IsNaN, IsInf
19 // when compiler does it for us
22 case p
< -MaxFloat64 || p
> MaxFloat64 || q
< -MaxFloat64 || q
> MaxFloat64
: // IsInf(p, 0) || IsInf(q, 0):
24 case p
!= p || q
!= q
: // IsNaN(p) || IsNaN(q):
40 return p
* Sqrt(1+q
*q
)