Log from math Lib giving me a wrong result

I was doing some coding and i needed the Log function from the math Lib so I used it, but I got a weird result in the case of Log(1000) base 10

from math import log
print(log(1000,10))

I am getting 2.9999999999999996 instead of 3
can anyone explain to me why this is happening ?
and what to do to get a correct results

Thank you for your time :slight_smile:

Use log10 instead, it is specially made to give more accurate results
for base 10:

>>> from math import log, log10
>>> log(1000, 10)
2.9999999999999996
>>> log10(1000)
3.0

The standard log function is the “natural logarithm”, which uses base
e=2.7182818… When you specify another base, it uses the change of base
formula:

log(1000, 10) = log(1000)/log(10)

which has three rounding errors instead of just one, and the end result
is that you lose a little bit of accuracy.

The log10 function instead is carefully designed to be accurate for base
10, so it is more accurate.

1 Like