Problem: Evaluate ln(x) without importing any libraries

Consider the following identity:

∫(1/x)dx≡ln(x)+c

If we also define ln(1)=0 (as exp(0)=1), we end up with ln(x) as the definite integral from 1 to x of 1/x.

Now simply integrate 1/x≡x^(-1) with respect to x resulting in x^0/0+c and...


Division by zero is not good.

Let's try an approximation, 1/x≈1/x^0.999. Evaluating the integral with limits 0 and 1 gives ln(x)≈(x^0.001-1)/0.001.

Mathematicians do not like approximations.

Let's use a limit instead! ln(x)≡lim ε → 0 ((x^ε-1)/ε)


Computers are bad with floats.

We have not yet transcended into the realm of pure mathematics. Floating point arithmetic can lead to inaccurate results. Try the following:

python -c 'epsilon=10**-10; print((2**epsilon-1)/epsilon)'

python -c 'epsilon=10**-20; print((2**epsilon-1)/epsilon)'

With floating point accuracy, less is more. Use with care.