2008-03-28 14:56:47 -06:00
|
|
|
// Copyright 2009 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.
|
|
|
|
|
2008-06-27 18:06:23 -06:00
|
|
|
package math
|
2008-03-28 14:56:47 -06:00
|
|
|
|
2009-12-15 21:43:12 -07:00
|
|
|
func isOddInt(x float64) bool {
|
|
|
|
xi, xf := Modf(x)
|
|
|
|
return xf == 0 && int64(xi)&1 == 1
|
|
|
|
}
|
2008-03-28 14:56:47 -06:00
|
|
|
|
2010-06-02 21:55:47 -06:00
|
|
|
// Special cases taken from FreeBSD's /usr/src/lib/msun/src/e_pow.c
|
|
|
|
// updated by IEEE Std. 754-2008 "Section 9.2.1 Special values".
|
|
|
|
|
2009-03-05 14:31:01 -07:00
|
|
|
// Pow returns x**y, the base-x exponential of y.
|
2010-06-02 21:55:47 -06:00
|
|
|
//
|
|
|
|
// Special cases are (in order):
|
|
|
|
// Pow(x, ±0) = 1 for any x
|
|
|
|
// Pow(1, y) = 1 for any y
|
|
|
|
// Pow(x, 1) = x for any x
|
|
|
|
// Pow(NaN, y) = NaN
|
|
|
|
// Pow(x, NaN) = NaN
|
|
|
|
// Pow(±0, y) = ±Inf for y an odd integer < 0
|
|
|
|
// Pow(±0, -Inf) = +Inf
|
|
|
|
// Pow(±0, +Inf) = +0
|
|
|
|
// Pow(±0, y) = +Inf for finite y < 0 and not an odd integer
|
|
|
|
// Pow(±0, y) = ±0 for y an odd integer > 0
|
|
|
|
// Pow(±0, y) = +0 for finite y > 0 and not an odd integer
|
|
|
|
// Pow(-1, ±Inf) = 1
|
|
|
|
// Pow(x, +Inf) = +Inf for |x| > 1
|
|
|
|
// Pow(x, -Inf) = +0 for |x| > 1
|
|
|
|
// Pow(x, +Inf) = +0 for |x| < 1
|
|
|
|
// Pow(x, -Inf) = +Inf for |x| < 1
|
|
|
|
// Pow(+Inf, y) = +Inf for y > 0
|
|
|
|
// Pow(+Inf, y) = +0 for y < 0
|
|
|
|
// Pow(-Inf, y) = Pow(-0, -y)
|
|
|
|
// Pow(x, y) = NaN for finite x < 0 and finite non-integer y
|
2009-01-20 15:40:40 -07:00
|
|
|
func Pow(x, y float64) float64 {
|
2008-11-20 11:54:02 -07:00
|
|
|
switch {
|
2010-06-02 21:55:47 -06:00
|
|
|
case y == 0 || x == 1:
|
2009-11-09 13:07:39 -07:00
|
|
|
return 1
|
2008-11-20 11:54:02 -07:00
|
|
|
case y == 1:
|
2009-11-09 13:07:39 -07:00
|
|
|
return x
|
2008-11-20 11:54:02 -07:00
|
|
|
case y == 0.5:
|
2009-11-09 13:07:39 -07:00
|
|
|
return Sqrt(x)
|
2008-11-20 11:54:02 -07:00
|
|
|
case y == -0.5:
|
2009-11-09 22:23:52 -07:00
|
|
|
return 1 / Sqrt(x)
|
2012-02-01 08:08:31 -07:00
|
|
|
case IsNaN(x) || IsNaN(y):
|
2009-12-15 21:43:12 -07:00
|
|
|
return NaN()
|
|
|
|
case x == 0:
|
|
|
|
switch {
|
|
|
|
case y < 0:
|
2010-06-02 21:55:47 -06:00
|
|
|
if isOddInt(y) {
|
|
|
|
return Copysign(Inf(1), x)
|
|
|
|
}
|
2009-12-15 21:43:12 -07:00
|
|
|
return Inf(1)
|
|
|
|
case y > 0:
|
2010-06-02 21:55:47 -06:00
|
|
|
if isOddInt(y) {
|
|
|
|
return x
|
|
|
|
}
|
2009-12-15 21:43:12 -07:00
|
|
|
return 0
|
|
|
|
}
|
2012-02-01 08:08:31 -07:00
|
|
|
case IsInf(y, 0):
|
2009-12-15 21:43:12 -07:00
|
|
|
switch {
|
2010-06-02 21:55:47 -06:00
|
|
|
case x == -1:
|
|
|
|
return 1
|
2011-09-29 10:54:20 -06:00
|
|
|
case (Abs(x) < 1) == IsInf(y, 1):
|
2010-06-02 21:55:47 -06:00
|
|
|
return 0
|
|
|
|
default:
|
|
|
|
return Inf(1)
|
2009-12-15 21:43:12 -07:00
|
|
|
}
|
2012-02-01 08:08:31 -07:00
|
|
|
case IsInf(x, 0):
|
2010-06-02 21:55:47 -06:00
|
|
|
if IsInf(x, -1) {
|
|
|
|
return Pow(1/x, -y) // Pow(-0, -y)
|
|
|
|
}
|
2009-12-15 21:43:12 -07:00
|
|
|
switch {
|
|
|
|
case y < 0:
|
|
|
|
return 0
|
|
|
|
case y > 0:
|
2010-06-02 21:55:47 -06:00
|
|
|
return Inf(1)
|
2009-12-15 21:43:12 -07:00
|
|
|
}
|
2008-11-20 11:54:02 -07:00
|
|
|
}
|
2008-03-28 14:56:47 -06:00
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
absy := y
|
|
|
|
flip := false
|
2008-11-20 11:54:02 -07:00
|
|
|
if absy < 0 {
|
2009-12-15 16:35:38 -07:00
|
|
|
absy = -absy
|
|
|
|
flip = true
|
2008-11-20 11:54:02 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
yi, yf := Modf(absy)
|
2008-11-20 11:54:02 -07:00
|
|
|
if yf != 0 && x < 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return NaN()
|
2008-11-20 11:54:02 -07:00
|
|
|
}
|
|
|
|
if yi >= 1<<63 {
|
2009-11-09 22:23:52 -07:00
|
|
|
return Exp(y * Log(x))
|
2008-03-28 14:56:47 -06:00
|
|
|
}
|
|
|
|
|
2010-04-09 15:37:33 -06:00
|
|
|
// ans = a1 * 2**ae (= 1 for now).
|
2011-01-19 21:09:00 -07:00
|
|
|
a1 := 1.0
|
2009-12-15 16:35:38 -07:00
|
|
|
ae := 0
|
2008-03-28 14:56:47 -06:00
|
|
|
|
2010-04-09 15:37:33 -06:00
|
|
|
// ans *= x**yf
|
2008-11-20 11:54:02 -07:00
|
|
|
if yf != 0 {
|
|
|
|
if yf > 0.5 {
|
2009-12-15 16:35:38 -07:00
|
|
|
yf--
|
|
|
|
yi++
|
2008-03-28 14:56:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
a1 = Exp(yf * Log(x))
|
2008-03-28 14:56:47 -06:00
|
|
|
}
|
|
|
|
|
2010-04-09 15:37:33 -06:00
|
|
|
// ans *= x**yi
|
2008-11-20 11:54:02 -07:00
|
|
|
// by multiplying in successive squarings
|
|
|
|
// of x according to bits of yi.
|
|
|
|
// accumulate powers of two into exp.
|
2009-12-15 16:35:38 -07:00
|
|
|
x1, xe := Frexp(x)
|
2008-11-20 12:56:48 -07:00
|
|
|
for i := int64(yi); i != 0; i >>= 1 {
|
|
|
|
if i&1 == 1 {
|
2009-12-15 16:35:38 -07:00
|
|
|
a1 *= x1
|
|
|
|
ae += xe
|
2008-11-20 12:56:48 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
x1 *= x1
|
|
|
|
xe <<= 1
|
2008-11-20 12:56:48 -07:00
|
|
|
if x1 < .5 {
|
2009-12-15 16:35:38 -07:00
|
|
|
x1 += x1
|
|
|
|
xe--
|
2008-03-28 14:56:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-09 15:37:33 -06:00
|
|
|
// ans = a1*2**ae
|
2008-11-20 11:54:02 -07:00
|
|
|
// if flip { ans = 1 / ans }
|
|
|
|
// but in the opposite order
|
|
|
|
if flip {
|
2009-12-15 16:35:38 -07:00
|
|
|
a1 = 1 / a1
|
|
|
|
ae = -ae
|
2008-03-28 14:56:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return Ldexp(a1, ae)
|
2008-03-28 14:56:47 -06:00
|
|
|
}
|