The log() function computes the natural logarithm ln(x)—logarithm to base e (Euler’s number, ≈ 2.718). It is the inverse of exp() and appears in growth, decay, entropy, and machine-learning formulas.
01
ln(x)
Base e.
02
math.h
Link -lm.
03
x > 0
Domain.
04
log(1)=0
Identity.
05
exp
Inverse.
06
vs log10
Base 10.
Fundamentals
Definition and Usage
The natural logarithm answers: “To what power must e be raised to get x?” If et = x, then log(x) = t. That is why log(exp(t)) = t.
In C, log(x) requires x > 0. log(1) = 0 because e&sup0; = 1. log(e) = 1 because e¹ = e.
💡
Beginner Tip
Do not pass zero or negatives to log(). The old suggestion to use clog() or log10() for those cases is wrong—neither accepts zero or negative reals either. Check x > 0 first.
log10(100) = 2 because 10² = 100. log(100) uses base e instead—different question, different answer.
Applications
🚀 Common Use Cases
Continuous growth/decay — solve for time with log.
Machine learning — cross-entropy and log-softmax.
Information theory — entropy uses natural log (nats) or log2.
Signal processing — decibel and log-scale conversions.
Inverse of exp() — undo exponential transforms.
🧠 How log() Works
1
Receive positive x
Domain: x > 0 only.
Input
2
Find exponent t
Solve et = x for t.
Compute
3
Return ln(x)
The power t as a double.
Output
=
🔢
Natural log
Undo exp: exp(log(x)) == x.
Important
📝 Notes
Natural log uses base e ≈ 2.71828, not base 10.
Domain: x > 0 only. Zero and negatives are errors.
log(1) = 0; log(e) = 1.
Inverse of exp() for positive values.
For base-10 logarithm, use log10(), not log().
Link with -lm on GCC/Clang Unix-like builds.
Performance
⚡ Optimization
log() is implemented in the platform math library. In hot loops computing many logarithms, profile first—often the algorithm structure matters more than micro-optimizing individual log calls. Use logf when float precision suffices.
Wrap Up
Conclusion
log() computes the natural logarithm ln(x) for positive x. It pairs with exp() as an inverse, solves continuous-growth equations, and differs from log10() in its base.
Continue with log10() for base-10 logarithms, or review exp() for the forward exponential.
Assume clog fixes invalid domain (it is for complex)
Call log on a sum without log rules—log(a+b) ≠ log(a)+log(b)
Ignore NaN/INF from bad inputs
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about log()
Natural logarithm in C, explained simply.
5
Core concepts
🔢01
ln(x)
Base e.
Basics
📚02
x > 0
Domain.
Rule
📈03
log(1)=0
Anchor.
Identity
📄04
exp
Inverse.
Pair
🔄05
vs log10
Base 10.
Compare
❓ Frequently Asked Questions
log(x) returns the natural logarithm ln(x) — the logarithm to base e (Euler's number, about 2.718). It answers: what power must e be raised to, to get x? For example, log(2.0) is about 0.693.
Include <math.h> and link with -lm on many systems: gcc program.c -o program -lm. The function is declared as double log(double x).
No. The domain is x > 0 only. log(0) returns negative infinity (-HUGE_VAL) and may set errno to ERANGE. log(negative) returns NaN and may set errno to EDOM. Always validate input is positive.
log(x) is the natural logarithm (base e). log10(x) is the common logarithm (base 10). log10(100) is 2.0; log(100) is about 4.605. Use log10 for 'how many digits' style base-10 thinking.
They are inverse operations. log(exp(x)) equals x for all real x. exp(log(x)) equals x when x > 0. If e^t = y, then t = log(y).
logf(x) takes float and returns float. logl(x) takes long double and returns long double. Use the variant matching your variable type.
Did you know?
The constant ln(2) ≈ 0.693 appears in the rule of 72 for estimating doubling time: divide 72 by the interest rate percent for a quick approximation. Precisely, doubling time at continuous rate r is log(2) / r.