math.log uses floats and these almost always involve some rounding errors.

>>> math.log(1000, 10)
2.9999999999999996

If you need it to be exact you should change your algorithm to generate the powers of 10 (simply multiply the last power by 10) and continue as long as the new power is smaller than your input number.

>>> limit = 12345
>>> power = 10
>>> while power < limit:
...     print power
...     power = power * 10
... 
10
100
1000
10000

This is guaranteed to be exact as it does not involve any floating point numbers. (And is a lot faster, too)

Answer from bikeshedder on Stack Overflow
🌐
Python
docs.python.org β€Ί 3 β€Ί library β€Ί math.html
math β€” Mathematical functions
2 weeks ago - >>> from math import exp, expm1 >>> exp(1e-5) - 1 # gives result accurate to 11 places 1.0000050000069649e-05 >>> expm1(1e-5) # result accurate to full precision 1.0000050000166668e-05 Β· Added in version 3.2. ... With one argument, return the natural logarithm of x (to base e).
🌐
W3Schools
w3schools.com β€Ί python β€Ί ref_math_log.asp
Python math.log() Method
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... # Import math Library import math # Return the natural logarithm of different numbers print(math.log(2.7183)) print(math.log(2)) print(math.log(1)) Try it Yourself Β»
Discussions

math.log in Python - Stack Overflow
I am writing a Python program to print all the powers of ten before an inputted number. For example, if the input is 12345, the program should output 10, 100, 1000, 10000. Here is my program - im... More on stackoverflow.com
🌐 stackoverflow.com
Math.log() method complexity
because log is base 2. No, math.log2() is base 2, math.log() returns the natural logarithm (so base e, 2.71etc). Regardless of that detail how did you establish that 'it took more step to compute'? More on reddit.com
🌐 r/learnpython
18
4
April 22, 2021
Error in math.log() method
Hi! there, (Python version 3.10.12) the issue is log10(2)=x ( the values of x is 0.30102999566398114) then 10^0.30102999566398114 should be equal to 2.0 but it is giving it as 1.9999999999999998 but for 10^0.30102999… More on discuss.python.org
🌐 discuss.python.org
0
September 5, 2024
Source code for logarithmic function
You can look at the source code here, written in C: https://github.com/python/cpython/blob/main/Modules/mathmodule.c More on reddit.com
🌐 r/learnpython
5
2
March 25, 2023
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί log-functions-python
Log functions in Python - GeeksforGeeks
August 14, 2024 - Syntax : math.log(a,Base) Parameters : a : The numeric value Base : Base to which the logarithm has to be computed. Return Value : Returns natural log if 1 argument is passed and log with specified base if 2 arguments are passed. Exceptions : Raises ValueError if a negative no. is passed as argument. ... # Python code to demonstrate the working of # log(a,Base) import math # Printing the log base e of 14 print ("Natural logarithm of 14 is : ", end="") print (math.log(14)) # Printing the log base 5 of 14 print ("Logarithm base 5 of 14 is : ", end="") print (math.log(14,5))
🌐
Codecademy
codecademy.com β€Ί docs β€Ί python β€Ί math module β€Ί math.log()
Python | Math Module | math.log() | Codecademy
March 17, 2024 - Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today. ... The math.log() function returns a float value, the natural logarithm of n or the logarithm of n to base.
Find elsewhere
🌐
Note.nkmk.me
note.nkmk.me β€Ί home β€Ί python
Power and Logarithmic Functions in Python: exp, log, log10, log2 | note.nkmk.me
August 10, 2023 - math.log2() β€” Mathematical functions β€” Python 3.11.4 documentation
🌐
Python.org
discuss.python.org β€Ί python help
Error in math.log() method - Python Help - Discussions on Python.org
September 5, 2024 - Hi! there, (Python version 3.10.12) the issue is log10(2)=x ( the values of x is 0.30102999566398114) then 10^0.30102999566398114 should be equal to 2.0 but it is giving it as 1.9999999999999998 but for 10^0.3010299956639812 it is giving output as 2.0 print(math.log(2,10)) # 0.30102999566398114 print(math.pow(10,0.30102999566398114)) # 1.9999999999999998 mean while, print(math.pow(10,0.3010299956639812)) # 2.0
🌐
Tutorialspoint
tutorialspoint.com β€Ί home β€Ί python β€Ί python number log
Python math.log() Method
February 21, 2009 - The Python math.log() method is used to compute the natural logarithm (with base e) of a positive numerical value.
🌐
Reddit
reddit.com β€Ί r/python β€Ί pro-tip: calculating the log (with arbitrary base) of a number
r/Python on Reddit: Pro-tip: calculating the log (with arbitrary base) of a number
April 7, 2009 -

Apparently everyone knew about this trick except for me.

I often find myself wanting to get the log2 of a number. I figured there was an obvious answer but I could never figure out how to do it, given that most math libraries only offer two log functions: log (typically base e) and log10 (base 10). Sometimes you luck out and get a log that offers an "arbitrary base" parameter, but most of the time not.

So how do you calculate the log2() of a number, if you only have loge() or log10()? Incredibly easy.

num = log10(1024.0) // what number you'd like to process
den = log10(2.0) // base goes here
result = num / den

>>> from math import log10
>>> log10(1024.0)/log10(2.0) # log2(2**10)
10.0
>>> log10(65536)/log10(16) # log16(2**16)
4.0

# and for sanity:
>>> from math import log, log10, e
>>> log(65536)
11.090354888959125
>>> log10(65536)/log10(e)
11.090354888959125
🌐
NumPy
numpy.org β€Ί doc β€Ί 2.3 β€Ί reference β€Ί generated β€Ί numpy.log.html
numpy.log β€” NumPy v2.3 Manual
67. https://personal.math.ubc.ca/~cbm/aands/page_67.htm ... Try it in your browser! >>> import numpy as np >>> np.log([1, np.e, np.e**2, 0]) array([ 0., 1., 2., -inf])
🌐
Centron
centron.de β€Ί startseite β€Ί python log() function to calculate logarithm
Python log() Function to Calculate Logarithm
February 7, 2025 - Python provides several variants of the log() function to calculate logarithms to specific bases or in unique scenarios: ... The math.log2(x) function computes the logarithm of x to the base 2, commonly used in computer science.
🌐
Analytics Vidhya
analyticsvidhya.com β€Ί home β€Ί different types of log functions in python
Different Types of Log Functions in Python
May 27, 2025 - Understanding the concept of logarithms and their applications is crucial for effectively leveraging Log functions in Python. Calculates the logarithm of a number a in a given base base. ... Returns the logarithm of a in base base.
🌐
W3Schools
w3schools.com β€Ί python β€Ί ref_math_log10.asp
Python math.log10() Method
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... # Import math Library import math # Return the base-10 logarithm of different numbers print(math.log10(2.7183)) print(math.log10(2)) print(math.log10(1)) Try it Yourself Β»