Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Math in C++ | Introduction
C++ Data Types
course content

Course Content

C++ Data Types

C++ Data Types

1. Introduction
2. Numerical Data Types
3. Text Data Type
4. Other Data Types and Concepts

bookMath in C++

In addition to the +, -, *, /, and % operations, we can perform many more mathematical operations using functions from the <cmath> library. Here is a table of the most used ones:

Don't forget to include the library using the following line:

Here is an example of using each of the functions mentioned above:

cpp

main

copy
1234567891011121314151617
#include <iostream> #include <cmath> int main() { std::cout << "cos(0) = " << cos(0) << std::endl; std::cout << "sin(0) = " << sin(0) << std::endl; std::cout << "tanh(pi/4) = " << tan(M_PI/4) << std::endl; std::cout << "exp(1) = " << exp(1) << std::endl; std::cout << "log(e) = " << log(M_E) << std::endl; std::cout << "pow(2, 3) = " << pow(2, 3) << std::endl; std::cout << "sqrt(4) = " << sqrt(4) << std::endl; std::cout << "cbrt(8) = " << cbrt(8) << std::endl; std::cout << "ceil(7.8) = " << ceil(7.8) << std::endl; std::cout << "floor(7.8) = " << floor(7.8) << std::endl; std::cout << "round(7.8) = " << round(7.8) << std::endl; }

Note

Constants M_PI and M_E for value of π (3.1415...) and e (2.7183...) are also defined in the <cmath> library.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 4
We're sorry to hear that something went wrong. What happened?
some-alt