# How to get natural constant/Euler's number?

I guess this is as much a mathematics and computer science question as it is a Swift question.

I came across the need for 𝑒 today while writing a function that calculates continuously compounding interest using the formula interest = 𝑒 interest rate × number of compounding periods. Surprisingly, despite its importance and usefulness, I wasn't able to find 𝑒 in the standard library, Numerics package, or Foundation. So, what are some ways to computing or approximating 𝑒, with what's available in Swift?

You don't need the value itself, just use `exp(interestRate * numberOfCompoundingPeriods)`

3 Likes

The exponential function is part of the Numerics package:

``````import Numerics // or RealModule

let interest = Double.exp(...)``````
3 Likes

And if you do need the actual constant, for some reason, it's `.exp(1)`.

3 Likes

Thanks @Nickolas_Pohilets, @Martin, and @scanon! I misinterpreted `exp` as the equivalent to the `^` operator in some other languages.

What's the justification for not defining the constant itself as .e?

3 Likes

Is `e` useful for any operation other than `exp` and `log`?

Yes

I admit I skimmed the Wikipedia article, but for the benefit of everyone can you please point out where it describes a use of `e` that isn’t as the base of an exponentiation or a logarithm?

It arises in probability. E.g. randomly select a number between 1 to n, n times. What's the probability a particular number, 1 say, never comes up? as n gets larger and larger the probability tends to 1 / e

`1/e`, aka `exp(-1)`?

No one has ever asked for it. It comes up occasionally not as the base of an exponential or logarithm, but it’s pretty infrequent by comparison.

1 Like

Well, yes. But if you were asked to calculate 1 / e, in maths or computing, you would not normally reach for `exp()`. You could also reformulate the problem so the answer is e.

If your goal is to compute `e`, then having an `.e` constant doesn’t do you much good.

If the only practical use of `.e` would be to reimplement `exp()` or `log()`, I’d rather use Steve’s high-quality implementations of those functions.