Python Forum
math.log versus math.log10 - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: math.log versus math.log10 (/thread-37279.html)

Pages: 1 2


math.log versus math.log10 - stevendaprano - May-22-2022

math.log(num, 10) is usually, but not always, equal to math.log10(num). In my random testing, when they are different, log(num, 10) is always smaller than log10(num).

(The difference appears to be 1 ULP.)

Does anyone know whether that can be taken as guaranteed?


RE: math.log versus math.log10 - jefsummers - May-22-2022

Here's the docs https://docs.python.org/3/library/math.html#power-and-logarithmic-functions

Log10 is more accurate than log(num,10), per the docs.


RE: math.log versus math.log10 - stevendaprano - May-23-2022

Thanks but that doesn't answer my question. My question is whether log(num, 10) is always less than or equal to log10(num)? Or could it sometimes be greater than log10?


RE: math.log versus math.log10 - Gribouillis - May-23-2022

If I understand well the documentation, the result of log(num, 10) is obtained by dividing log(num) by log(10), so there are rounding errors due to the division of two floating numbers at the given precision, and also perhaps to the fact that log(num) has larger magnitude that log10(num). It seems very unlikely that these rounding errors always happen in the same direction and even more unlikely that this is guaranteed by the specifications.


RE: math.log versus math.log10 - DeaD_EyE - May-23-2022

The math module is a thin wrapper around math.c: https://github.com/python/cpython/blob/main/Modules/mathmodule.c#L2315

For my understanding, the C-Functions are called.
To answer the question, you must look for the C implementation of log and log10.


RE: math.log versus math.log10 - snippsat - May-23-2022

This was reported in issue3724 and closed in issue 6765
So the conclusion was to add in Doc calculated as log(x)/log(base) in math.log
In math.log10 added This is usually more accurate than log(x, 10).


RE: math.log versus math.log10 - jefsummers - May-23-2022

Don't count on it.
import math
greater = 0
lesser = 0
for counter in range(1,100):
    if math.log10(counter) > math.log(counter,10):
        greater += 1
    else:
        lesser += 1
print(greater, lesser)
Output:
64 35
I then did it for a million
Output:
583631 416368



RE: math.log versus math.log10 - Gribouillis - May-23-2022

@jefsummer Unfortunately you should have written >= instead of >. Those results are not good.


RE: math.log versus math.log10 - stevendaprano - May-23-2022

Ah, apart from you accidentally counting the case where they are equal as "greater", that's a good experimental test which answers my question.

I re-did the test with the correct test, and found that in the first million integers, there were 583630 cases where log(num, 10) was less than log10(num), and just 14 cases where it was greater. (The rest of the cases where equal.)

Thanks for the suggestion.


RE: math.log versus math.log10 - Gribouillis - May-23-2022

(May-23-2022, 02:09 PM)snippsat Wrote: In math.log10 added This is usually more accurate than log(x, 10).
They did not even dare say «This is always more accurate than log(x, 10)» Big Grin