I would like to a good (8 bits accuracy) approximation for $x^{1/2.4}$ in the range $[0, 1]$. This transform is used for converting linear intensities to SRGB compressed values, so it's important that I make it run fast.

Plot of function:

enter image description here

Using a simple polynomial isn't practical because

  1. the function has lots of high order derivatives
  2. the function is roughly asymptotic to $x$, which is very different from the asymptotic behavior of high order polynomials

I've already have code that constructs an arbitrary degree polynomial for any function by minimizing the square error and even for a 10th degree polynomial, the accuracy is still only like 6 significant bits.

Then I learned about rational function approximations, which will have a much better asymptotic behavior. But the problem is I don't know how to find the optimal coefficients. There's the Pade formulation which creates an approximation around a single point, but since it doesn't use global information, it can be a very bad fit overall just like Taylor series.

I had Mathematica create an approximation of the form $(a_0 + a_1 x + a_2 x^2) / (1 + a_3 x)$ with PadeApproximant[x^(1/2.4), {x, 0.2, {3, 2}}], which is much better than a simple 3rd degree polynomial, but still not good enough, so I want to find a globally optimal solution, probably of the same form.

I tried finding a least squares solution like before, but it involves 4 huge, non-linear polynomial equations, that is taking Mathematica forever (I've waited 1/2 hour so far) to solve.

Can anyone suggest how to solve those non-linear equations, or another way to find a rational function approximation, or an entirely different approximation?

Thanks for any help


I think your reasoning is correct -- a rational approximation is probably the best solution. But, constructing rational approximations is difficult.

You have to decide how you will measure error. If you just look at maximum difference between the value of the function and it's approximation, then you are doing "uniform" or "minimax" approximation, which is usually more difficult than least-squares approximation. As you mentioned, Pade approximation is not very good for this, because it's a Taylor-series-like approach that's fixated on a single point.

I would recommend that you try the Mathematica RationalInterpolation function, if you haven't already.

Or, if you have access to Matlab, there is an add-on called Chebfun that does a very good job of constructing minimax polynomial and rational approximations. There are commands named ratinterp and remez, and a couple of others. The name "remez" comes from the Remez Exchange Algorithm, which is the standard way of computing minimax approximations. This section of the Chebfun documentation explains the techniques that are available. Section 4.8 covers rational approximations.

There is a nice book by Trefethen that provides a modern computationally-oriented account of approximation theory, with a focus on Chebfun. Insightful, and easy to read.


I tried the following Mathematica code

gx = GeneralMiniMaxApproximation[{t, 1 - t + t^(1/2.4)}, 
                             {t, {0, 1}, 3, 3}, x, 
                              MaxIterations -> 200, WorkingPrecision -> 50]

and then

hx = gx - 1 + x

This gave an approximation $hx$ of your function

$$ \frac{x^4 - 1.3730926529x^3 - 1.2231795079x^2 - 0.0120332034x - 3.892203211 \times 10^{-7}} {x^3 - 2.47604929086x^2 - 0.13996763348x - 0.00008072719}$$

The error function looks like this:


Not quite 8 bits, but getting close. You can fiddle with the degrees of the numerator and denominator to try to get something better.


Consider $\frac{1}{2.4} \approx 0.41$, perhaps starting with a square root and approximating the ratio is easier. Or just use approximations for the logarithm and exponential. Those functions are directly implemented in hardware nowadays, and quite cheap. Or select judiciously some points and interpolate. Or approximate through splines. The magic fast inverse square root algorithm (really, its justification) might also give some ideas.

Just make sure this operation is really relevant performance wise before going down this path.

  • 1
    $\begingroup$ "Or just use approximations for the logarithm and exponential" - Right. 2^((1/2.4)log2[x]) was my fallback solution. If I can use GPUs, that's what I would use since its done in hardware. But my target processor is x86, with SSE4 instructions, and I know 2^x and log2[x] can be computed in about ~11 instructions each (3rd degree polynomial), but I think evaluating 2 approximations together has some redundancy, which you could do better on with rational functions $\endgroup$ – Yale Zhang Mar 9 '13 at 17:23

See also stackoverflow optimizations-for-pow-with-const-non-integer-exponent for other approaches to the same problem.

If you look at your graph of $x^{5/12}$ its obviously got a different shape near $x=0$, than near $x=1$. This is a good hint that simple polynomials wont fit so well over a domain including both $0$ and $1$.

So I would consider a strength reduction:

consider that in floating point representation $x = m \cdot 2^k$ where $0.5 \le m < 1$ so $$x^{5/12} = m^{5/12} \cdot 2^{5k/12}$$

MiniMaxApproximation[x^(5/12), {x, {1/2, 1}, 2, 2}]

Offers a rational O(2,2) polynomnial with a relative error less than $5.7\times10^{-7}$ for $m$ on $\left[\frac12,1\right]$.

For the second part of the strength reduction you can either use $Power\left[2,\frac {5k}{12}\right]$ or, since $k$ is an integer, optimise it with a 12 element lookup table and some modulo arithmetic.

Noting that, on most machines, division is very slow (and not good for numerical accuracy) I also considered simple polynomials and

MiniMaxApproximation[x^(5/12), {x, {1/2, 1}, 7, 0}]

Offers an O(7) polynomial with a relative error less than $2.4\times10^{-8}$ or better than half an ULP in single precision. Which should be about the same speed as the O(2,2). For completeness, here it is in Horner Form

0.246873 + 
   x (F954 + 
     x (-2.71714 + 
        x (3.86663 + 
          x (-3.85026 + x (2.47365 + (-0.919353 + 0.15006 x) x)))))

$$x^\frac{1}{2.4} =x^\frac{5}{12} = (x^\frac{5}{4})^\frac{1}{3} $$

Thus, assuming you have access to square root, you can calculate $x^\frac{5}{4}$ precisely, and thus you only need a good approximation to cube root in the range 0 to 1.

As noted, there are multiple ways one can do such an approximation. For example, use Newton-Raphson to quickly converge to a solution; one can get this to run faster by pre-calculating some values at intervals and caching them, so one can converge faster to the result by looking up the nearest starting point.


One observes that $a^{\frac{1}{2.4}} = a^{\frac{5}{12}} = a \cdot a^{-\frac{1}{3}} \cdot a^{-\frac{1}{4}}$. There is a simple division-free iteration for computing the reciprocal of the $n$-th root, $a^{-\frac{1}{n}}$, as follows: $$x_{i+1} \colon= x_{i} + \frac{1}{n} x_{i} \left(1-x^{n}_{i} a\right)$$ where $\frac{1}{n}$ can obviously be pre-computed as a constant. The iteration scheme has quadratic convergence, with $\epsilon_{i+1} \approx \left(\frac{n+1}{2}\right)\epsilon_{i}^{2}$.

One way of generating a starting approximation $x_0$ is to use simple bit-manipulation of the IEEE-754 single-precision floating-point data, as shown in this question on Stackoverflow. Many high-end processors provide sufficient instruction-level parallelism to support concurrent computation of reciprocal cube root and reciprocal quad root.

Since the accuracy of the starting approximations is quite poor at around $3.5 \cdot 10^{-2}$, one iteration for each of the reciprocal roots is insufficient to reach 8-bit accuracy; at least one of the roots must use two iterations. In this case, it is more advantageous from an accuracy perspective to apply the second iteration to the reciprocal cube root, which limits relative error in the final result to $2.43 \cdot 10^{3}$, well within the desired specification.

I performed an exhaustive test on the larger interval $[2^{-126}, 2^{10}]$, although the sRGB conversion mentioned in the question only requires the computation of $a^{\frac{5}{12}}$ on the narrower interval $[0.0031808, 1]$. Implementing the exemplary C code below using the SIMD instruction set of an x86 processor (this was indicated by asker in a comment) would appear to require at least SSE 4.1, as a 32-bit integer multiply is required.

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>

#define USE_2ND_RCBRT_ITER   (1)
#define USE_2ND_RQDRT_ITER   (1)

int float_as_int32 (float);
float int32_as_float (int32_t);
float pow_3 (float);
float pow_4 (float);

  Compute a**(5/12), suitable for sRGB transformation on [0.0031808, 1]

  Exhaustive testing across arguments in [0x1.0p-126, 0x1.0p+10] finds:
  USE_2ND_RCBRT_ITER=1, USE_2ND_RQDRT_ITER=1: max. rel. err = 2.6016e-5
  USE_2ND_RCBRT_ITER=1, USE_2ND_RQDRT_ITER=0: max. rel. err = 2.4320e-3
  USE_2ND_RCBRT_ITER=0, USE_2ND_RQDRT_ITER=1: max. rel. err = 2.4900e-3
  USE_2ND_RCBRT_ITER=0, USE_2ND_RQDRT_ITER=0: max. rel. err = 4.8074e-3
float pow_5_12 (float a)
    float c, q;

    const float THIRD  = (float)(1.0/3.0);
    const float FOURTH = (float)(1.0/4.0);
    const int32_t rcbrt_magic = 0x54a1da3e; /* for reciprocal cube root */
    const int32_t rqdrt_magic = 0x4f58482c; /* for reciprocal quad root */

    /* compute reciprocal cube root */
    c = int32_as_float (rcbrt_magic - (float_as_int32 (a) >> 16) * 0x5555);
    c = c + c * ((1.0f - pow_3 (c) * a) * THIRD);
    c = c + c * ((1.0f - pow_3 (c) * a) * THIRD);
#endif // USE_2ND_RCBRT_ITER

    /* compute reciprocal quad root */
    q = int32_as_float (rqdrt_magic - (float_as_int32 (a) >> 2));
    q = q + q * ((1.0f - pow_4 (q) * a) * FOURTH);
    q = q + q * ((1.0f - pow_4 (q) * a) * FOURTH);
#endif // USE_2ND_RQDRT_ITER

    /* a**(5/12) = a * a**(-1/4) * a**(-1/3) */
    return a * c * q;

int float_as_int32 (float a)
    int32_t r;
    memcpy (&r, &a, sizeof r);
    return r;

float int32_as_float (int32_t a)
    float r;
    memcpy (&r, &a, sizeof r);
    return r;

float pow_3 (float a)
    return a * a * a;

float pow_4 (float a)
    a = a * a;
    return a * a;

int main (void)
    float a, res, start, stop;
    double ref, relerr, maxrelerr;

    start = powf (2.0f, -126);
    stop = powf (2.0f, 10);

    maxrelerr = 0;
    a = start;
    while (a <= stop) {
        res = pow_5_12 (a);
        ref = pow ((double)a, 5.0/12); 
        relerr = fabs ((res - ref) / ref);
        if (relerr > maxrelerr) maxrelerr = relerr;
        a = int32_as_float (float_as_int32 (a) + 1);
    printf ("maxrelerr = %11.4e\n", maxrelerr);
    return EXIT_SUCCESS;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.