Search code examples
mathanalysis

How are exponents calculated?


I'm trying to determine the asymptotic run-time of one of my algorithms, which uses exponents, but I'm not sure of how exponents are calculated programmatically.

I'm specifically looking for the pow() algorithm used for double-precision, floating point numbers.


Solution

  • I've had a chance to look at fdlibm's implementation. The comments describe the algorithm used:

     *                    n
     * Method:  Let x =  2   * (1+f)
     *      1. Compute and return log2(x) in two pieces:
     *              log2(x) = w1 + w2,
     *         where w1 has 53-24 = 29 bit trailing zeros.
     *      2. Perform y*log2(x) = n+y' by simulating muti-precision
     *         arithmetic, where |y'|<=0.5.
     *      3. Return x**y = 2**n*exp(y'*log2)
    

    followed by a listing of all the special cases handled (0, 1, inf, nan).

    The most intense sections of the code, after all the special-case handling, involve the log2 and 2** calculations. And there are no loops in either of those. So, the complexity of floating-point primitives notwithstanding, it looks like a asymptotically constant-time algorithm.

    Floating-point experts (of which I'm not one) are welcome to comment. :-)