这是一个很好的正整数幂算法,它首先处理一些简单的情况,然后使用循环测试指数的二进制位。例如在二进制中查找3^11 11 是 1011 所以循环中的阶段是
- bitMask = 1011,evenPower = 3,结果 = 3
- bitMask = 101,evenPower = 3*3 = 9,结果 = 3*9 = 27
- bitMask = 10,evenPower = 9*9 = 81,结果 = 27
- bitMask = 1, evenPower = 81*81 = 6561, result = 27*6561 = 177147
这是每个循环的 evenPower 平方,如果底部位为 1,则结果乘以 evenPower。代码已从 Patricia Shanahan 的方法 http://mindprod.com/jgloss/power.html 中提取,该方法又起源于 Kunth,可以是可追溯到公元前 200 年的印度。
/**
* A fast routine for computing integer powers.
* Code adapted from {@link <a href="http://mindprod.com/jgloss/power.html">efficient power</a>} by Patricia Shanahan pats@acm.org
* Almost identical to the method Knuth gives on page 462 of The Art of Computer Programming Volume 2 Seminumerical Algorithms.
* @param l number to be taken to a power.
* @param n power to take x to. 0 <= n <= Integer.MAX_VALUE
* Negative numbers will be treated as unsigned positives.
* @return x to the power n
*
*/
public static final double power(double l,int n)
{
assert n>=0;
double x=l;
switch(n){
case 0: x = 1.0; break;
case 1: break;
case 2: x *= x; break;
case 3: x *= x*x; break;
case 4: x *= x; x *= x; break;
case 5: { double y = x*x; x *= y*y; } break;
case 6: { double y = x*x; x = y*y*y; } break;
case 7: { double y = x*x; x *= y*y*y; } break;
case 8: x *= x; x *= x; x *= x; break;
default:
{
int bitMask = n;
double evenPower = x;
double result;
if ( (bitMask & 1) != 0 )
result = x;
else
result = 1;
bitMask >>>= 1;
while ( bitMask != 0 ) {
evenPower *= evenPower;
if ( (bitMask & 1) != 0 )
result *= evenPower;
bitMask >>>= 1;
} // end while
x = result;
}
}
return x;
}
对于真正的指数,您基本上需要找到 exp 和 log 的方法。您可以使用最简单的泰勒级数,但有更好的方法。我们有
exp(x) = 1 + x + x^2/2 + x^3/6 + x^4/24 + x^5/120 + x^6/6! + ...
ln(1+x) = x - x^2 /2 + x^3 /3 - x^4 / 4 + x^5 / 5 - x^6/6 + ... |x|<1
要查找 x^y 注释 ln(x^y) = y*ln(x)。现在我们需要在正确的范围内获得参数,以便我们可以使用我们的幂级数。设 x = m * 2^ex,尾数和指数选择为 1/sqrt(2)ln(m*2^ex) = ln(m) + ex*ln(2)。令 h = m-1 并找到 ln(1+h)。
使用 java 和 float,因为有一种简单的方法可以找到 IEEE 表示的内部结构(我使用了 float,因为要处理的位数更少)
int b = Float.floatToIntBits(x);
int sign = (b & 0x80000000) == 0 ? 1 : -1;
int mattissa = b & 0x007fffff;
int ex = ((b & 0x7f800000) >> 23 ) - 127;
在 javascript 中,Number.toExponential 对我们来说可能是最简单的并解析结果。
接下来在所需范围内构造一个数字 z 1/sqrt(2)
int bits = mattissa | 0x3f800000;
float z = Float.intBitsToFloat(bits);
if(z>root2) {
z = z/2;
++ex;
}
用这个函数用泰勒级数求1+x的对数
static float ln1px(float x) {
float x_2 = x*x; // powers of x
float x_3 = x_2 * x;
float x_4 = x_3 * x;
float x_5 = x_4 * x;
float x_6 = x_5 * x;
float res = x - x_2 /2 + x_3 /3 - x_4 / 4 + x_5 / 5 - x_6/6;
return res;
}
这对三位有效数字似乎很好,当 x 接近 0 时通常会好得多。
然后可以找到我们号码 x 的日志
float w = z - 1;
float ln_z = ln1px(w);
float ln_x = ln_z + ln2 * ex;
System.out.println("ln "+ln_x+"\t"+Math.log(x));
如果我们写出 y = n + a ,现在到实际的幂,其中 n 是整数,a 是小数。所以
x^y=x^(n+a) = x^n * x^a。使用此答案中的第一个算法找到x^n。写x=m*2^ex 然后ln((m*2^ex)^a) = y<em>ln(m) + y</em>ex*ln(2) 和
x^a=exp(ln((m*2^ex)^a)) = exp(a * ln(m)) * exp(a * ln(2))^ex
这两个指数项的值相当小,所以泰勒级数应该是好的。
指数函数的泰勒级数需要一个函数
static float exp(float x) {
float x_2 = x*x; // powers of x
float x_3 = x_2 * x;
float x_4 = x_3 * x;
float x_5 = x_4 * x;
float x_6 = x_5 * x;
float res = 1+ x + x_2 /2 + x_3 /6 + x_4 / 24 + x_5 / 120 + x_6/ 720;
return res;
}
我们终于可以拼凑起来了
// Get integer and fractional parts of y
int n = (int) Math.floor(y);
float a = y-n;
float x_n = power(x,n); // x^n
float a_ln_m = a * ln_z; // ln(m^a) = a ln(m)
float a_ln_2 = a * ln2; // ln(2^a) = a ln(2)
float m_a = exp(a_ln_m); // m^a = exp(a ln(m))
float _2_a = exp(a_ln_2); // 2^a = exp(a ln(2))
float _2_a_ex = power(_2_a,ex); // (2^ex)^a = 2^(a*ex) = (2^a)^ex
float x_a = m_a * _2_a_ex; // x^a = m^a * 2^(a*ex)
float x_y = x_n * x_a; // x^y = x^n * x^a
System.out.println("x^y "+x_y+"\t"+Math.pow(x,y));
这应该是完整的程序,你需要一些聪明来应对消极的论点等。
请注意,这不是特别准确,因为我只使用了泰勒级数的几个术语。其他SO问题有更详细的解答How can I write a power function myself?