Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
math.log versus math.log10
#1
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?
Reply
#2
Here's the docs https://docs.python.org/3/library/math.h...-functions

Log10 is more accurate than log(num,10), per the docs.
Gribouillis likes this post
Reply
#3
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?
Reply
#4
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.
Reply
#5
The math module is a thin wrapper around math.c: https://github.com/python/cpython/blob/m...le.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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
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).
Gribouillis likes this post
Reply
#7
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
Reply
#8
@jefsummer Unfortunately you should have written >= instead of >. Those results are not good.
Reply
#9
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.
Reply
#10
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  __dict__ in math library akbarza 6 940 Sep-28-2023, 08:23 AM
Last Post: buran
  How to do "fixed size" (wrapping) math in Python? AlexanderWulf 13 1,868 Jul-19-2023, 04:13 PM
Last Post: deanhystad
  math problem for experts miltmobley 5 998 Jul-09-2023, 07:14 AM
Last Post: Gribouillis
  Math python question Deonvek 6 1,151 Apr-05-2023, 09:27 PM
Last Post: deanhystad
  math formula does not give the same result as bash script [SOLVED] AlphaInc 3 970 Apr-02-2023, 07:21 PM
Last Post: AlphaInc
  Determine if keyboard interrupt versus SIGINT trapped? Jibunnokage 5 1,785 Sep-30-2022, 06:54 AM
Last Post: Gribouillis
  Convert SQLite Fetchone() Result to float for Math Extra 13 3,545 Aug-02-2022, 01:12 PM
Last Post: deanhystad
Question Parameterized math calculations? babaliaris 7 16,922 Jun-01-2022, 10:19 AM
Last Post: Gribouillis
  Math Package in python Uma 1 1,492 Dec-12-2021, 02:01 PM
Last Post: jefsummers
  Why getting ValueError : Math domain error in trig. function, math.asin() ? jahuja73 3 3,771 Feb-24-2021, 05:09 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020