[issue45348] math.log(243, 3) value issue

Dennis Sweeney report at bugs.python.org
Sat Oct 2 20:43:23 EDT 2021


Dennis Sweeney <sweeney.dennis650 at gmail.com> added the comment:

According to https://docs.python.org/3/library/math.html#math.log :

"""With two arguments, return the logarithm of x to the given base, calculated as log(x)/log(base)."""

Indeed, this is consistent with that:

>>> from math import log
>>> log(243)
5.493061443340548
>>> log(3)
1.0986122886681098
>>> 5.493061443340548/1.0986122886681098
4.999999999999999

math.log is a floating-point operation, not an integer operation, and this is the nature of floating point operations: there can be rounding errors whenever there are intermediate steps of the computation. Strictly speaking, I would say this is not a bug, and results of floating point operations should generally be compared with math.isclose(), not with ==. Just like you can't expect (1/49)*49 == 1.0.

On the other hand, it may be theoretically possible to do some clever floating point tricks to get more accurate results out of math.log(a, b), like maybe some kind of correction factor involving the platform libm pow() function. I don't know if those are typically any more reliable than the status quo quotient-of-logs.

----------
nosy: +Dennis Sweeney

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45348>
_______________________________________


More information about the Python-bugs-list mailing list