# How can I calculate the exponential integral?

(I originally asked this in a different exchange.)

I'm writing a program that uses the prime-counting function. Right now, I'm using x/log(x), but I want to switch to something more accurate. A better approximation is the logarithmic integral function (actually, its Eulerian variant), which can be computed from the exponential integral. Now how can I compute the exponential integral? I'm on a macOS Intel system using Swift, so I can use the various advanced floating-point functions provided by Apple's system libraries if needed to help.

I did see a similar question, but I use a different domain (2 and above).

Of course, answers that involve a function better than Li(x) are acceptable too, as long as I can implement them easily.

• For the exponential/logarithmic integral, I tend to use the series by Ramanujan (formula 15 here) for small to medium-sized arguments, and the asymptotic series for large arguments. Nov 14 '19 at 1:17

As with most questions about the computation of special functions, the Digital Librarary of Mathematical Functions is a good place to start. In particular, see chapter 6, which deals with the exponential integral and $$\mbox{li}(x)$$.
You'll find that different methods (e.g. the power series for small $$x$$ versus asymptotic expansion for large $$x$$) work best in different ranges.