Logarithmic calculations are essential for data scaling, calculating information entropy, and implementing physics formulas.
The Python standard library math module provides a general-purpose log() function, as well as log10() and log2() for fixed bases.
This article explains how to use these functions and how to choose the right one for your purpose.
1. Calculating with an Arbitrary Base: math.log(x, base)
The math.log() function calculates the logarithm $\log_{base}(x)$ for any base by specifying the base in the second argument.
Syntax:
import math
result = math.log(value, base)
Specific Usage Example:
Here is an example of calculating “to what power must 3 be raised to equal 81 ($\log_3 81$).”
import math
target_value = 81
base_value = 3
# Calculate log_3(81)
result = math.log(target_value, base_value)
print(f"log{base_value}({target_value}) = {result}")
Execution Result:
log3(81) = 4.0
The answer is 4.0 ($3^4 = 81$). The return value is always a floating-point number (float).
2. Calculating Natural Logarithms (Base $e$): math.log(x)
If you omit the second argument (the base) of math.log(), it automatically uses Napier’s number $e$ (the base of natural logarithms). In other words, it calculates $\ln(x)$ or $\log_e(x)$.
import math
# The log of Napier's number e itself should be 1.0
val = math.e
result_natural = math.log(val)
print(f"ln(e) = {result_natural}")
# Natural logarithm of 10
print(f"ln(10) = {math.log(10)}")
Execution Result:
ln(e) = 1.0
ln(10) = 2.302585092994046
3. Calculating Common Logarithms (Base 10): math.log10(x)
If you want to calculate base-10 logarithms (common logarithms), you can write math.log(x, 10), but using the dedicated math.log10(x) function provides higher precision and better readability.
Specific Usage Example: Calculating Decibels (dB)
This is often used to express signal amplification (gain) in decibels. Here is a calculation of the decibel value when the power ratio is 100 times.
import math
# Ratio of output power to input power (100 times)
power_ratio = 100
# Decibel calculation: 10 * log10(ratio)
decibel = 10 * math.log10(power_ratio)
print(f"A power ratio of {power_ratio} times is {decibel} dB.")
Execution Result:
A power ratio of 100 times is 20.0 dB.
4. Calculating Base-2 Logarithms: math.log2(x)
For “base-2 logarithms,” which frequently appear in computer science, use math.log2(x). This is more precise than math.log(x, 2).
Specific Usage Example: Bit Count for Information
Here is an example of calculating the number of bits required to represent a certain number of data patterns (states).
import math
# Number of states to represent (e.g., 256 colors)
states = 256
# Calculate required bits
bits_needed = math.log2(states)
print(f"It takes {bits_needed} bits to represent {states} states.")
Execution Result:
It takes 8.0 bits to represent 256 states.
Summary
- math.log(x, base): Calculates the logarithm with an arbitrary base.
- math.log(x): Calculates the natural logarithm (base $e$) if the base is omitted.
- math.log10(x): Common logarithm (base 10). Suitable for decibel calculations and estimating digit counts.
- math.log2(x): Binary logarithm (base 2). Suitable for calculating information entropy and bit counts.
Choosing the dedicated function (log10, log2) according to your purpose makes the intent of your code clearer.
