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

bookStandard Math

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:

All of this is part of the standard math library. To use it, you need to include the appropriate header file. You can do this by including either cmath or math.h.

include.h

include.h

copy
123
#include <cmath> // OR #include <math.h>

The main difference between cmath and math.h lies in their purpose and usage. cmath is part of the C++ standard library, designed specifically for C++ and requiring the std namespace (e.g., std::sqrt()), while math.h comes from the C standard library, uses the global namespace (e.g., sqrt()), and is compatible with C++.

main.cpp

main.cpp

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 << "tan(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.

question mark

Which of the following is the most appropriate way to include the math library in a C++ program?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 4

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

What are some examples of using these functions in C++ code?

Can you explain the difference between radians and degrees when using trigonometric functions?

Are there any other useful constants or functions in the `<cmath>` library?

Awesome!

Completion rate improved to 4.35

bookStandard Math

Swipe to show menu

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:

All of this is part of the standard math library. To use it, you need to include the appropriate header file. You can do this by including either cmath or math.h.

include.h

include.h

copy
123
#include <cmath> // OR #include <math.h>

The main difference between cmath and math.h lies in their purpose and usage. cmath is part of the C++ standard library, designed specifically for C++ and requiring the std namespace (e.g., std::sqrt()), while math.h comes from the C standard library, uses the global namespace (e.g., sqrt()), and is compatible with C++.

main.cpp

main.cpp

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 << "tan(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.

question mark

Which of the following is the most appropriate way to include the math library in a C++ program?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 4
some-alt