aboutsummaryrefslogtreecommitdiff
path: root/libgo/go/math/pow.go
diff options
context:
space:
mode:
Diffstat (limited to 'libgo/go/math/pow.go')
-rw-r--r--libgo/go/math/pow.go20
1 files changed, 19 insertions, 1 deletions
diff --git a/libgo/go/math/pow.go b/libgo/go/math/pow.go
index 7ba426f4..e255ee4 100644
--- a/libgo/go/math/pow.go
+++ b/libgo/go/math/pow.go
@@ -99,7 +99,16 @@ func pow(x, y float64) float64 {
return NaN()
}
if yi >= 1<<63 {
- return Exp(y * Log(x))
+ // yi is a large even int that will lead to overflow (or underflow to 0)
+ // for all x except -1 (x == 1 was handled earlier)
+ switch {
+ case x == -1:
+ return 1
+ case (Abs(x) < 1) == (y > 0):
+ return 0
+ default:
+ return Inf(1)
+ }
}
// ans = a1 * 2**ae (= 1 for now).
@@ -121,6 +130,15 @@ func pow(x, y float64) float64 {
// accumulate powers of two into exp.
x1, xe := Frexp(x)
for i := int64(yi); i != 0; i >>= 1 {
+ if xe < -1<<12 || 1<<12 < xe {
+ // catch xe before it overflows the left shift below
+ // Since i !=0 it has at least one bit still set, so ae will accumulate xe
+ // on at least one more iteration, ae += xe is a lower bound on ae
+ // the lower bound on ae exceeds the size of a float64 exp
+ // so the final call to Ldexp will produce under/overflow (0/Inf)
+ ae += xe
+ break
+ }
if i&1 == 1 {
a1 *= x1
ae += xe