C++ provides a rich set of mathematical functions to perform various calculations. These functions are part of the C++ Standard Library and are available through the <cmath>
header. Let’s explore some of the most commonly used math functions in C++.
Including the <cmath>
Header
To use these math functions, you need to include the <cmath>
header at the beginning of your program.
#include <iostream>
#include <cmath>
Common Math Functions
Here are some of the most commonly used math functions available in C++:
1. Power and Exponential Functions
pow(x, y)
: Computes x raised to the power y.exp(x)
: Computes the exponential function ex.#include <iostream>
#include <cmath>
int main() {
double base = 2.0;
double exponent = 3.0;
double result = pow(base, exponent);
std::cout << base << " raised to the power of " << exponent << " is " << result << std::endl;
double expValue = exp(1.0); // e^1
std::cout << "e raised to the power of 1 is " << expValue << std::endl;
return 0;
}
2. Logarithmic Functions
log(x)
: Computes the natural logarithm (base eee) of xxx.log10(x)
: Computes the base-10 logarithm of xxx.
Example
#include <iostream>
#include <cmath>
int main() {
double value = 10.0;
double naturalLog = log(value);
std::cout << "Natural logarithm of " << value << " is " << naturalLog << std::endl;
double logBase10 = log10(value);
std::cout << "Base-10 logarithm of " << value << " is " << logBase10 << std::endl;
return 0;
}
3. Trigonometric Functions
sin(x)
: Computes the sine of xxx (in radians).cos(x)
: Computes the cosine of xxx (in radians).tan(x)
: Computes the tangent of xxx (in radians).
Example
#include <iostream>
#include <cmath>
int main() {
double angle = M_PI / 4; // 45 degrees in radians
double sine = sin(angle);
std::cout << "Sine of 45 degrees is " << sine << std::endl;
double cosine = cos(angle);
std::cout << "Cosine of 45 degrees is " << cosine << std::endl;
double tangent = tan(angle);
std::cout << "Tangent of 45 degrees is " << tangent << std::endl;
return 0;
}
4. Hyperbolic Functions
sinh(x)
: Computes the hyperbolic sine of xxx.cosh(x)
: Computes the hyperbolic cosine of xxx.tanh(x)
: Computes the hyperbolic tangent of xxx.
Example
#include <iostream>
#include <cmath>
int main() {
double value = 1.0;
double hyperbolicSine = sinh(value);
std::cout << "Hyperbolic sine of " << value << " is " << hyperbolicSine << std::endl;
double hyperbolicCosine = cosh(value);
std::cout << "Hyperbolic cosine of " << value << " is " << hyperbolicCosine << std::endl;
double hyperbolicTangent = tanh(value);
std::cout << "Hyperbolic tangent of " << value << " is " << hyperbolicTangent << std::endl;
return 0;
}
5. Rounding and Remainder Functions
ceil(x)
: Rounds xxx up to the nearest integer.floor(x)
: Rounds xxx down to the nearest integer.round(x)
: Rounds xxx to the nearest integer.fmod(x, y)
: Computes the remainder of xxx divided by yyy.
Example
#include <iostream>
#include <cmath>
int main() {
double value = 5.7;
double ceiling = ceil(value);
std::cout << "Ceiling of " << value << " is " << ceiling << std::endl;
double flooring = floor(value);
std::cout << "Flooring of " << value << " is " << flooring << std::endl;
double rounded = round(value);
std::cout << "Rounded value of " << value << " is " << rounded << std::endl;
double remainder = fmod(10.5, 3.2);
std::cout << "Remainder of 10.5 divided by 3.2 is " << remainder << std::endl;
return 0;
}
6. Absolute Value Functions
abs(x)
: Computes the absolute value of an integer xxx.fabs(x)
: Computes the absolute value of a floating-point number xxx.
Example
#include <iostream>
#include <cmath>
int main() {
int intValue = -10;
double doubleValue = -10.5;
int absValue = abs(intValue);
std::cout << "Absolute value of " << intValue << " is " << absValue << std::endl;
double fabsValue = fabs(doubleValue);
std::cout << "Absolute value of " << doubleValue << " is " << fabsValue << std::endl;
return 0;
}
Conclusion
C++ provides a comprehensive set of mathematical functions through the <cmath>
header. These functions allow you to perform a wide range of calculations, from basic arithmetic to complex trigonometric and logarithmic operations. By leveraging these functions, you can handle mathematical computations effectively in your C++ programs. Happy coding!