import math
print(math.log1p(2)) # 1.0986122886681098
print(math.log1p(4)) # 1.6094379124341003
print(math.log1p(0)) # 0.0
Using negative number import math
print(math.log1p(-2))
Above code will generate error. import math
x = -0.001 # 0.1% decrease
log_value = math.log1p(x)
print(log_value) # -0.0010005003335835335
import math
x = 1e-10
print(math.log1p(x)) # High precision for small x
print(math.log(1 + x)) # Slight loss of precision
Explanation:# Log of a small percentage increase in a financial model
growth_rate = 0.001 # 0.1% growth
log_growth = math.log1p(growth_rate)
print(log_growth)
This approach is more accurate than using math.log(1 + growth_rate) and ensures that small growth rates are calculated correctly without precision loss.
import math
# Logarithm of a small probability value in a logistic regression model
probability = 0.0001
log_prob = math.log1p(-probability)
print(log_prob)
This ensures accurate computation for small probability values when modeling predictions.Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.