# 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. – J. M. Nov 14 '19 at 1:17

## 2 Answers

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.

I see that Swift has standard elementary functions, Bessel functions, and the Gamma function but it doesn't appear to have li or Ei. if I knew of a special functions library for Swift then I'd recommend that you use it rather than writing your own routine.

If you only need up to double precision, there's a c implementation in cephes that you should be able to adapt fairly easily : https://github.com/jeremybarnes/cephes/blob/60f27df395b8322c2da22c83751a2366b82d50d1/misc/expn.c