C++ std::log
The natural logarithm turns a positive growth multiplier into a continuous growth exponent.
A simple example
std::log
- Start with 1
- Do this Find the natural logarithm
- Result 0
#include <iostream>
#include <cmath>
int main() {
std::cout << (std::log(1.0)) << '\n';
}
0
Syntax
std::log(ratio)
Run these examples in C++
Save one example at a time as lesson.cpp. Use C++17 and a Unix-style shell.
c++ -std=c++17 -Wall -Wextra lesson.cpp -o lesson
./lessonMeasure the continuous growth needed to double
A student models growth that compounds continuously. Work backwards from a doubling of the starting amount to find the corresponding change on the natural-log scale.
#include <cmath>
#include <cstdio>
int main() {
const double ratio = 2.0;
std::printf("%.3f\n", std::log(ratio));
return 0;
}
0.693
Doubling in one period requires a continuous exponent of about 0.693 because exp(0.693) is close to two.
Identify a month with flat growth
A class is comparing growth against an unchanged starting amount. Examine the multiplier at the reference point so a later increase can be interpreted relative to the original quantity.
#include <cmath>
#include <cstdio>
int main() {
const double multiplier = 1.0;
std::printf("%.3f\n", std::log(multiplier));
return 0;
}
0.000
A multiplier of one means no change. Its continuous growth exponent is zero.
Compare two light readings
A lab sample is four times brighter than the reference. The log-scale display uses the natural logarithm of that ratio.
Using 4 gives 1.386 for this measurement.
What to watch for
Supply a positive ratio. Base-ten logarithms use a different scale.