In C++, the <cmath>
module provides a variety of functions to perform these calculations efficiently and accurately. This article delves into the details of these functions, exploring their usage, examples, and practical applications.
Exponential functions deal with the constant e (approximately equal to 2.71828), which is the base of natural logarithms. The <cmath>
module includes several functions to work with exponentials.
The exp(double x)
function computes e raised to the power of x
.
#include <iostream>
#include <cmath>
int main() {
double x = 1.0;
std::cout << "exp(" << x << ") = " << exp(x) << std::endl; // Output: 2.71828
return 0;
}
#include <iostream>
#include <cmath>
int main() {
double x = 1.0;
std::cout << "exp(" << x << ") = " << exp(x) << std::endl; // Output: 2.71828
return 0;
}
The exp2(double x)
function computes 2 raised to the power of x
.
#include <iostream>
#include <cmath>
int main() {
double x = 3.0;
std::cout << "exp2(" << x << ") = " << exp2(x) << std::endl; // Output: 8
return 0;
}
The expm1(double x)
function computes e raised to the power of x
minus 1, i.e., ex−1e^x – 1ex−1. This function is useful for avoiding loss of precision when x
is close to zero.
#include <iostream>
#include <cmath>
int main() {
double x = 0.1;
std::cout << "expm1(" << x << ") = " << expm1(x) << std::endl; // Output: 0.105171
return 0;
}
Logarithmic functions are the inverse of exponential functions. They are used to find the power to which a base must be raised to obtain a given number. The <cmath>
module offers several logarithmic functions.
The log(double x)
function computes the natural logarithm (base e) of x
.
#include <iostream>
#include <cmath>
int main() {
double x = 2.71828;
std::cout << "log(" << x << ") = " << log(x) << std::endl; // Output: 1
return 0;
}
The log10(double x)
function computes the base-10 logarithm of x
.
#include <iostream>
#include <cmath>
int main() {
double x = 100.0;
std::cout << "log10(" << x << ") = " << log10(x) << std::endl; // Output: 2
return 0;
}
The log2(double x)
function computes the base-2 logarithm of x
.
#include <iostream>
#include <cmath>
int main() {
double x = 8.0;
std::cout << "log2(" << x << ") = " << log2(x) << std::endl; // Output: 3
return 0;
}
The log1p(double x)
function computes the natural logarithm of 1 + x
, i.e., loge(1+x)\log_e(1 + x)loge(1+x). This function is useful for avoiding loss of precision when x
is close to zero.
#include <iostream>
#include <cmath>
int main() {
double x = 0.1;
std::cout << "log1p(" << x << ") = " << log1p(x) << std::endl; // Output: 0.095310
return 0;
}
Exponential and logarithmic functions have numerous applications in real-world scenarios.
Compound interest can be calculated using the formula A=P⋅ertA = P \cdot e^{rt}A=P⋅ert, where P
is the principal amount, r
is the interest rate, and t
is the time.
#include <iostream>
#include <cmath>
int main() {
double P = 1000.0; // Principal amount
double r = 0.05; // Interest rate
double t = 10.0; // Time in years
double A = P * exp(r * t);
std::cout << "Compound interest after " << t << " years is " << A << std::endl; // Output: 1648.72
return 0;
}
The decay of a radioactive substance can be modeled using N(t)=N0⋅e−λtN(t) = N_0 \cdot e^{-\lambda t}N(t)=N0⋅e−λt, where N_0
is the initial quantity, λ
is the decay constant, and t
is time.
#include <iostream>
#include <cmath>
int main() {
double N0 = 100.0; // Initial quantity
double lambda = 0.01; // Decay constant
double t = 50.0; // Time
double Nt = N0 * exp(-lambda * t);
std::cout << "Remaining quantity after " << t << " units of time is " << Nt << std::endl; // Output: 60.65
return 0;
}
The pH of a solution is calculated as the negative logarithm of the hydrogen ion concentration, pH=−log10[H+].
#include <iostream>
#include <cmath>
int main() {
double H_concentration = 1e-7; // Hydrogen ion concentration
double pH = -log10(H_concentration);
std::cout << "The pH of the solution is " << pH << std::endl; // Output: 7
return 0;
}