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
๐ŸŒ
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 ยป
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ math.html
math โ€” Mathematical functions
February 23, 2026 - >>> 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).
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ log-functions-python
Log functions in Python - GeeksforGeeks
August 14, 2024 - Syntax : math.log2(a) Parameters : a : The numeric value Return Value : Returns logarithm base 2 of a Exceptions : Raises ValueError if a negative no. is passed as argument. ... # Python code to demonstrate the working of # log2(a) import math # Printing the log base 2 of 14 print ("Logarithm base 2 of 14 is : ", end="") print (math.log2(14))
๐ŸŒ
Project Jupyter
jupyter.org
Project Jupyter | Home
Jupyter supports over 40 programming languages, including Python, R, Julia, and Scala.
๐ŸŒ
IncludeHelp
includehelp.com โ€บ python โ€บ log-functions-of-math-module.aspx
Log functions of math module in Python
April 20, 2019 - Return value: float โ€“ it returns a float value that is the base-10 logarithm of the given number x. # python code to demonstrate example of # different math module log functions # importing math module import math # number x = 10 # natural logarithm (base-e) using log(x) print("natural logarithm of ", x, " is = ", math.log(x)) # logarithm (base-5) using log(x, base) print("base-5 logarithm of ", x, " is = ", math.log(x,5)) # natural logarithm (base-e) of 1+ number using log1p(x) print("natural logarithm of 1+ ", x, " is = ", math.log(1+x)) # base-2 logarithm using log2(x) print("base-2 logarithm of ", x, " is = ", math.log2(x)) # base-10 logarithm using log10(x) print("base-10 logarithm of ", x, " is = ", math.log10(x))
๐ŸŒ
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
๐ŸŒ
Python Examples
pythonexamples.org โ€บ python-math-log
Python math.log() - Natural Logarithm
Natural logarithm of a negative number. import math x = -2 result = math.log(x) print('log(x) :', result)
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-math-module
Python Math Module - GeeksforGeeks
July 26, 2025 - Helps in performing logarithmic and exponential operations easily. Supports real-world applications like physics, statistics, geometry and finance. To work with mathematical constants and functions in Python, you need to import the math module:
๐ŸŒ
Sarthaks eConnect
sarthaks.com โ€บ 3484691 โ€บ python-math-log-method
Python math.log() Method - Sarthaks eConnect | Largest Online Education Community
April 10, 2023 - LIVE Course for free ยท The math.log() method in Python returns the natural logarithm (base e) of a given number. The natural logarithm of a number x, denoted by ln(x), is the power to which e must be raised to equal x
๐ŸŒ
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.log() โ€” Mathematical functions โ€” Python 3.11.4 documentation
๐ŸŒ
How-To Geek
howtogeek.com โ€บ home โ€บ programming โ€บ the math module in python: 6 common calculations you can make
The math Module in Python: 6 Common Calculations You Can Make
October 5, 2025 - Another common constant you can use with Python is e, or 2.718281828459045. This is the base of the natural logarithm and is commonly used for exponential growth, such as compound interest.
๐ŸŒ
Symbolab
symbolab.com
Math Solver - Trusted Online AI Math Calculator | Symbolab
It breaks problems into step-by-step solutions, showing how each part contributes to the answer. Whether itโ€™s solving an equation, simplifying an expression, or working through a calculus limit, Symbolabโ€™s AI Math Solver is designed to help learners follow the logic and build confidence along the way.
๐ŸŒ
GitConnected
levelup.gitconnected.com โ€บ recapping-the-math-module-in-python-403ffcac4ce4
Recapping the Math Module in Python | by Ebo Jackson | Level Up Coding
June 15, 2023 - import math x = 1.5 print(math.sinh(x)) # Output: 2.1292794550948173 print(math.cosh(x)) # Output: 2.3524096152432476 print(math.tanh(x)) # Output: 0.905148253644866 ยท Exponential and logarithmic functions: import math x = 2.0 print(math.exp(x)) # Output: 7.3890560989306495 print(math.log(x)) # Output: 0.6931471805599453 print(math.log10(x)) # Output: 0.3010299956639812 print(math.sqrt(x)) # Output: 1.4142135623730951 ยท
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ number_log.htm
Python math.log() Method
This method returns natural logarithm of x, for x > 0. The following example shows the usage of the Python math.log() method.
๐ŸŒ
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.
๐ŸŒ
Matplotlib
matplotlib.org โ€บ stable โ€บ tutorials โ€บ pyplot.html
Pyplot tutorial โ€” Matplotlib 3.10.8 documentation
# Fixing random state for reproducibility np.random.seed(19680801) # make up some data in the open interval (0, 1) y = np.random.normal(loc=0.5, scale=0.4, size=1000) y = y[(y > 0) & (y < 1)] y.sort() x = np.arange(len(y)) # plot with various axes scales plt.figure() # linear plt.subplot(221) plt.plot(x, y) plt.yscale('linear') plt.title('linear') plt.grid(True) # log plt.subplot(222) plt.plot(x, y) plt.yscale('log') plt.title('log') plt.grid(True) # symmetric log plt.subplot(223) plt.plot(x, y - y.mean()) plt.yscale('symlog', linthresh=0.01) plt.title('symlog') plt.grid(True) # logit plt.subp