C++Introduction to C++Learn C++

Learn To Use Mathematical And Trigonometric Functions In C++

C & C++ is good to operate in mathematical operations because of its wide variety of variable definitions, wise memory usage and it’s operators in mathematics. C++ has a lot of mathematical functions that you can do mathematical tasks. Some operators may work with integers, some works with floating numbers, most are usable with both integer and floating (or double) numbers. All functions can be found in the <cmath> library, so you must include this library before using these functions, like this.

Also note that some of compilers may require options to use these mathematical operands. (i.e in GNU C/C++ you must use -lm option)

Here are some of mathematical functions used in C++;

pow(), powf() and powl()
pow functions are are used to find power of double floating numbers, example below results a power b,

powf() is used for floating numbers and powl() is used for long floating numbers.

sqrt() and cbrt(),
sqrt() function is used to get square root of a given value, see below,

cbrt() is used to get cubic root of a given value

round(), floor() and ceil()
round() function returns rounds of number
floor() function returns the given value rounded down to its nearest integer
ceil() function returns the given value rounded up to its nearest integer

min() and max()
min() and max() functions are used to find minimum and maximum number of pairs

abs() and fabs(), Absolute

Trigonometric Functions

Here we listed some of trigonometric functions below,

All of these functions and more can be found in cmath library.

In addition to these functions, In C++ Builder, VCL and FMX applications have most of these functions starts with upper case i.e. Max(), Min(), Sin(), Cos() etc.

Finally library also has some constants, i.e Pi number which is used to calculate peripheral and circular area of a circle as below,

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Learn C++Syntax

What Is A Forced (Default) Copy Assignment Operator In Modern C++

C++C++11C++14C++17C++20Learn C++Syntax

What is Implicitly-declared Copy Assignment Operator In C++?

C++C++11C++14C++17C++20Learn C++Syntax

What is Avoiding Implicit Copy Assignment In C++?

C++C++11C++14C++17C++20Learn C++

Typical Declaration Of A Copy Assignment Operator Without std::swap