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?
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?
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.
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)
.
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
@jefsummer Unfortunately you should have written >=
instead of >
. Those results are not good.
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.
(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)»
