Computes a b . @param a the base @param b the power @return a b
(double a, double b)
| 159 | * @return a<sup>b</sup> |
| 160 | */ |
| 161 | public static double pow(double a, double b) |
| 162 | { |
| 163 | |
| 164 | /* |
| 165 | * Wright out a^b as 2^(b log2(a)) and then replace a with 'm 2^e' to get |
| 166 | * 2^(b * log2(m*2^e)) which simplifies to |
| 167 | * m^b 2^(b e) when m, e, and b are positive. |
| 168 | * |
| 169 | * m & e are by IEEE defintion positive |
| 170 | */ |
| 171 | |
| 172 | if (b < 0) |
| 173 | return 1 / pow(a, -b);//b is now made positive |
| 174 | |
| 175 | long rawBits_a = doubleToLongBits(a); |
| 176 | long mantissa_a = getMantissa(rawBits_a); |
| 177 | final int e_a = Math.getExponent(a); |
| 178 | |
| 179 | //compute m^b and exploit the fact that we know there is no need for the exponent |
| 180 | double m = longBitsToDouble(1023L << 52 | mantissa_a);//m in [1, 2] |
| 181 | |
| 182 | final double log2m = 1.790711564253215 + 0.248597253161674 * m - 3.495545043418375 / (0.714309275671154 + 1.000000000000000 * m); |
| 183 | |
| 184 | //we end up with 2^(b * log_2(m)) * 2^(b * e), which we can reduce to a single pow2 call |
| 185 | return pow2(b * log2m + b * e_a);//fun fact, double*int is faster than casting an int to a double... |
| 186 | } |
| 187 | |
| 188 | private static final double expPowConst = 1.0/Math.log(2); |
| 189 |