Using Faster Exponential Approximation



In some scenarios/applicatons, where the precision may not be so critically important but the speed (performance) is, you may be willing to sacrifice some extent of accuracy for the speed.

In neutral networks, where the math function tex_fa6b7e7736a40da84ecdc0e4ee3c81ca Using Faster Exponential Approximation C/C++ double faster approximation where is usually small (less than 2, for instance), you can avoid the expensive exp() provided by math.h (for other programming languages, similar inbuilt system functions are provided)

The tex_474f9eefecc087e5804b33cc66a0b65e Using Faster Exponential Approximation C/C++ double faster approximation (exponential function) can be considered as the following:

exp Using Faster Exponential Approximation C/C++ double faster approximation

In practice,  cannot approach to the infinity but we can achieve a relatively good accuracy by using a large n.

For example, if we put tex_a43617c028098702649915821cef059d Using Faster Exponential Approximation C/C++ double faster approximation , then we can multiply tex_da03c601b0dd4362bd753780bdb46ced Using Faster Exponential Approximation C/C++ double faster approximation by itself 8 times due to the fact tex_19c74ac38a6f22f8ec6319a87fde9130 Using Faster Exponential Approximation C/C++ double faster approximation

With this in mind, we can come up with the following approximation:

1
2
3
4
5
6
7
inline
double exp1(double x) {
  x = 1.0 + x / 256.0;
  x *= x; x *= x; x *= x; x *= x;
  x *= x; x *= x; x *= x; x *= x;
  return x;
}
inline
double exp1(double x) {
  x = 1.0 + x / 256.0;
  x *= x; x *= x; x *= x; x *= x;
  x *= x; x *= x; x *= x; x *= x;
  return x;
}

We can also multiply a few more times, to increase the accuracy.

1
2
3
4
5
6
7
8
inline
double exp2(double x) {
  x = 1.0 + x / 1024;
  x *= x; x *= x; x *= x; x *= x;
  x *= x; x *= x; x *= x; x *= x;
  x *= x; x *= x;
  return x;
}
inline
double exp2(double x) {
  x = 1.0 + x / 1024;
  x *= x; x *= x; x *= x; x *= x;
  x *= x; x *= x; x *= x; x *= x;
  x *= x; x *= x;
  return x;
}

Now, you have the pattern, but for now, we need to test how accurate these approximations are:

exp Using Faster Exponential Approximation C/C++ double faster approximation

The above plots 3 curves, which are the exp provided by math.h, the exp 256 and the exp 1024. They show very good agreement for input smaller than 5.

We plot the difference to make it easier to see.

exp-diff Using Faster Exponential Approximation C/C++ double faster approximation

Wow, it really can be a faster alternative if the required input range smaller than 5. For negative inputs, the difference won’t be so noticeable because the value itself is so tiny that can’t be observed visually in graph.

The exp 256 is 360 times faster than the traditional exp and the exp 1024 is 330 times faster than the traditional exp.

–EOF (Coding For Speed) —

GD Star Rating
loading...
634 words Last Post: Using Faster Integer Power in Java
Next Post: Counting the number of Leading Zeros for a 32-bit Integer (Signed or Unsigned)

The Permanent URL is: Using Faster Exponential Approximation (AMP Version)

6 Comments

  1. Juan L.
  2. Bruno
    • Bruno
  3. Antoni Gual
  4. Chris Harvey

Leave a Reply