C++ Math – Detailed Tutorial with Examples

The C++ Standard Library provides a rich set of mathematical functions and algorithms that can be used to perform various mathematical operations. The functions and algorithms are defined in the <cmath> header file, which is a part of the C++ Standard Library.

Trigonometric Functions

The C++ Standard Library provides a set of trigonometric functions for performing common trigonometric operations, such as sine, cosine, and tangent. These functions are defined in the <cmath> header file and are used as follows:

#include <iostream>
#include <cmath>

int main() {
    double x = 90.0;
    std::cout << "sin(" << x << ") = " << std::sin(x) << std::endl;
    std::cout << "cos(" << x << ") = " << std::cos(x) << std::endl;
    std::cout << "tan(" << x << ") = " << std::tan(x) << std::endl;
    return 0;
}

The output of the above program will be:

sin(90) = 1
cos(90) = 6.12323e-17
tan(90) = 1.63312e+16

Exponential and Logarithmic Functions

The C++ Standard Library provides a set of exponential and logarithmic functions for performing operations such as exponentiation, natural logarithm, and base-10 logarithm. These functions are defined in the <cmath> header file and are used as follows:

#include <iostream>
#include <cmath>

int main() {
    double x = 2.0;
    std::cout << "exp(" << x << ") = " << std::exp(x) << std::endl;
    std::cout << "log(" << x << ") = " << std::log(x) << std::endl;
    std::cout << "log10(" << x << ") = " << std::log10(x) << std::endl;
    return 0;
}

The output of the above program will be:

exp(2) = 7.38906
log(2) = 0.693147
log10(2) = 0.30103

Power and Root Functions

The C++ Standard Library provides a set of functions for performing power and root operations, such as square root, cube root, and exponentiation. These functions are defined in the <cmath> header file and are used as follows:

#include <iostream>
#include <cmath>
int main() {
double x = 4.0;
std::cout << "sqrt(" << x << ") = " << std::sqrt(x) << std::endl;
std::cout << "cbrt(" << x << ") = " << std::cbrt(x) << std::endl;
std::cout << "pow(" << x << ",2) = " << std::pow(x,2) << std::endl;
return 0;
}

The output of the above program will be:

sqrt(4) = 2
cbrt(4) = 1.5874
pow(4,2) = 16

These are some of the common mathematical functions provided by the C++ Standard Library. It is important to note that all the functions provided by the library are highly optimized for performance and accuracy. With the help of these functions, you can perform various mathematical operations with ease in your C++ programs.

Conclusion

In conclusion, the C++ Standard Library provides a rich set of mathematical functions and algorithms that can be used for various mathematical operations. From trigonometric functions to exponential and logarithmic functions, the <cmath> header file has everything one needs for their mathematical needs in C++. It is important to note that the library is highly optimized for efficiency and accuracy, making it a reliable choice for mathematical operations in C++ programming.

Leave a Reply

Your email address will not be published. Required fields are marked *