mirror of
https://github.com/golang/go
synced 2024-11-19 10:04:56 -07:00
1124fa300b
This is the benchmark result base on darwin with amd64 architecture: name old time/op new time/op delta Cos 10.2ns ± 2% 10.3ns ± 3% +1.18% (p=0.032 n=10+10) Cosh 25.3ns ± 3% 24.6ns ± 2% -3.00% (p=0.000 n=10+10) Hypot 6.40ns ± 2% 6.19ns ± 3% -3.36% (p=0.000 n=10+10) HypotGo 7.16ns ± 3% 6.54ns ± 2% -8.66% (p=0.000 n=10+10) J0 66.0ns ± 2% 63.7ns ± 1% -3.42% (p=0.000 n=9+10) Fixes #21812 Change-Id: I2b88fbdfc250cd548f8f08b44ce2eb172dcacf43 Reviewed-on: https://go-review.googlesource.com/84437 Reviewed-by: Giovanni Bajo <rasky@develer.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Keith Randall <khr@golang.org> Run-TryBot: Giovanni Bajo <rasky@develer.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
39 lines
773 B
Go
39 lines
773 B
Go
// Copyright 2010 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package math
|
|
|
|
/*
|
|
Hypot -- sqrt(p*p + q*q), but overflows only if the result does.
|
|
*/
|
|
|
|
// Hypot returns Sqrt(p*p + q*q), taking care to avoid
|
|
// unnecessary overflow and underflow.
|
|
//
|
|
// Special cases are:
|
|
// Hypot(±Inf, q) = +Inf
|
|
// Hypot(p, ±Inf) = +Inf
|
|
// Hypot(NaN, q) = NaN
|
|
// Hypot(p, NaN) = NaN
|
|
func Hypot(p, q float64) float64
|
|
|
|
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)
|
|
}
|