github.com/primecitizens/pcz/std@v0.2.1/math/pow.go (about) 1 // Copyright 2009 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 func isOddInt(x float64) bool { 8 xi, xf := Modf(x) 9 return xf == 0 && int64(xi)&1 == 1 10 } 11 12 // Special cases taken from FreeBSD's /usr/src/lib/msun/src/e_pow.c 13 // updated by IEEE Std. 754-2008 "Section 9.2.1 Special values". 14 15 // Pow returns x**y, the base-x exponential of y. 16 // 17 // Special cases are (in order): 18 // 19 // Pow(x, ±0) = 1 for any x 20 // Pow(1, y) = 1 for any y 21 // Pow(x, 1) = x for any x 22 // Pow(NaN, y) = NaN 23 // Pow(x, NaN) = NaN 24 // Pow(±0, y) = ±Inf for y an odd integer < 0 25 // Pow(±0, -Inf) = +Inf 26 // Pow(±0, +Inf) = +0 27 // Pow(±0, y) = +Inf for finite y < 0 and not an odd integer 28 // Pow(±0, y) = ±0 for y an odd integer > 0 29 // Pow(±0, y) = +0 for finite y > 0 and not an odd integer 30 // Pow(-1, ±Inf) = 1 31 // Pow(x, +Inf) = +Inf for |x| > 1 32 // Pow(x, -Inf) = +0 for |x| > 1 33 // Pow(x, +Inf) = +0 for |x| < 1 34 // Pow(x, -Inf) = +Inf for |x| < 1 35 // Pow(+Inf, y) = +Inf for y > 0 36 // Pow(+Inf, y) = +0 for y < 0 37 // Pow(-Inf, y) = Pow(-0, -y) 38 // Pow(x, y) = NaN for finite x < 0 and finite non-integer y 39 func Pow(x, y float64) float64 { 40 return pow(x, y) 41 } 42 43 func pow(x, y float64) float64 { 44 switch { 45 case y == 0 || x == 1: 46 return 1 47 case y == 1: 48 return x 49 case IsNaN(x) || IsNaN(y): 50 return NaN() 51 case x == 0: 52 switch { 53 case y < 0: 54 if isOddInt(y) { 55 return Copysign(Inf(1), x) 56 } 57 return Inf(1) 58 case y > 0: 59 if isOddInt(y) { 60 return x 61 } 62 return 0 63 } 64 case IsInf(y, 0): 65 switch { 66 case x == -1: 67 return 1 68 case (Abs(x) < 1) == IsInf(y, 1): 69 return 0 70 default: 71 return Inf(1) 72 } 73 case IsInf(x, 0): 74 if IsInf(x, -1) { 75 return Pow(1/x, -y) // Pow(-0, -y) 76 } 77 switch { 78 case y < 0: 79 return 0 80 case y > 0: 81 return Inf(1) 82 } 83 case y == 0.5: 84 return Sqrt(x) 85 case y == -0.5: 86 return 1 / Sqrt(x) 87 } 88 89 yi, yf := Modf(Abs(y)) 90 if yf != 0 && x < 0 { 91 return NaN() 92 } 93 if yi >= 1<<63 { 94 // yi is a large even int that will lead to overflow (or underflow to 0) 95 // for all x except -1 (x == 1 was handled earlier) 96 switch { 97 case x == -1: 98 return 1 99 case (Abs(x) < 1) == (y > 0): 100 return 0 101 default: 102 return Inf(1) 103 } 104 } 105 106 // ans = a1 * 2**ae (= 1 for now). 107 a1 := 1.0 108 ae := 0 109 110 // ans *= x**yf 111 if yf != 0 { 112 if yf > 0.5 { 113 yf-- 114 yi++ 115 } 116 a1 = Exp(yf * Log(x)) 117 } 118 119 // ans *= x**yi 120 // by multiplying in successive squarings 121 // of x according to bits of yi. 122 // accumulate powers of two into exp. 123 x1, xe := Frexp(x) 124 for i := int64(yi); i != 0; i >>= 1 { 125 if xe < -1<<12 || 1<<12 < xe { 126 // catch xe before it overflows the left shift below 127 // Since i !=0 it has at least one bit still set, so ae will accumulate xe 128 // on at least one more iteration, ae += xe is a lower bound on ae 129 // the lower bound on ae exceeds the size of a float64 exp 130 // so the final call to Ldexp will produce under/overflow (0/Inf) 131 ae += xe 132 break 133 } 134 if i&1 == 1 { 135 a1 *= x1 136 ae += xe 137 } 138 x1 *= x1 139 xe <<= 1 140 if x1 < .5 { 141 x1 += x1 142 xe-- 143 } 144 } 145 146 // ans = a1*2**ae 147 // if y < 0 { ans = 1 / ans } 148 // but in the opposite order 149 if y < 0 { 150 a1 = 1 / a1 151 ae = -ae 152 } 153 return Ldexp(a1, ae) 154 }