psihodelia - 7 months ago 72

C Question

I am looking for a way to implement a fast logistic function. The classic definition of logistic function is:

`y(x) = 1 / (1 + (1/e^x))`

or equally:

`y(x) = (e^x) / (e^x + 1)`

However, my special logistic function has a base E instead of e, so:

`y(x) = E^x / (E^x + 1)`

E and x in my case are 32-bit integer, fixed point in base 2 of 16.16 type. E is so near as possible to the real constant e.

What is the fastest algorithm for such function ? I would prefer no lookup tables. Something like bit-manipulations should be perfect.

UPDATE:

Intuitively, I feel that there exists a very fast and simple method, based on Euler's formula:

`e^(i*x) = cos(x) + i*sin(x)`

where i is the imaginary unit.

Answer

Powers do not easily convert to bit shifts, because

```
E^x = 2^log2(E^x) = 2^(x*log2(E))
```

and this will give you a fractional number of bits to shift. You could calculate x*log2(E) and then decompose the power into a sum of separate bit shifts, but this is unlikely be as fast as a lookup table followed by a couple of iterations of Newton's method. Then again, this depends on how expensive your fixed-point arithmetic operations are.