github.com/prattmic/llgo-embedded@v0.0.0-20150820070356-41cfecea0e1e/third_party/gofrontend/libgo/go/math/hypot.go (about) 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. 4 5 package math 6 7 /* 8 Hypot -- sqrt(p*p + q*q), but overflows only if the result does. 9 */ 10 11 // Hypot returns Sqrt(p*p + q*q), taking care to avoid 12 // unnecessary overflow and underflow. 13 // 14 // Special cases are: 15 // Hypot(±Inf, q) = +Inf 16 // Hypot(p, ±Inf) = +Inf 17 // Hypot(NaN, q) = NaN 18 // Hypot(p, NaN) = NaN 19 func Hypot(p, q float64) float64 { 20 return hypot(p, q) 21 } 22 23 func hypot(p, q float64) float64 { 24 // special cases 25 switch { 26 case IsInf(p, 0) || IsInf(q, 0): 27 return Inf(1) 28 case IsNaN(p) || IsNaN(q): 29 return NaN() 30 } 31 if p < 0 { 32 p = -p 33 } 34 if q < 0 { 35 q = -q 36 } 37 if p < q { 38 p, q = q, p 39 } 40 if p == 0 { 41 return 0 42 } 43 q = q / p 44 return p * Sqrt(1+q*q) 45 }