Lesson 12 - Mathematical functions in C++ - The cmath library
Lesson highlights
Are you looking for a quick reference on the
cmath
library in C++ instead of a thorough-full lesson? Here it
is:
Using cmath
constants:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout << "Pi: " << M_PI << endl;
cout << "e: " << M_E << endl;
}
Min/max of 2 values:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout << "Min: " << fmin(5, 10) << endl;
cout << "Max: " << fmax(5, 10) << endl;
}
All roundings:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout << "Round: " << round(-0.2) << endl;
cout << "Ceiling: " << ceil(-0.2) << endl;
cout << "Floor: " << floor(-0.2) << endl;
cout << "Truncate: " << trunc(-0.2) << endl;
double d = 2.72;
int a = (int)round(d); // casting the rounded double to the int type
}
Abs/signbit:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout << "Abs: " << abs(-10) << endl;
cout << "Sign: " << signbit(-10) << endl;
}
Trigonometric functions:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout << "Sin: " << sin(M_PI) << endl; // almost 0
cout << "Cos: " << cos(M_PI) << endl;
cout << "Tan: " << tan(M_PI) << endl; // almost 0
cout << "Acos: " << acos(0) << endl;
cout << "Asin: " << asin(-1) << endl;
cout << "Atan: " << atan(0) << endl;
}
Powers, roots, logarithms:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout << "Pow: " << pow(2, 3) << endl;
cout << "Sqrt: " << sqrt(144) << endl;
cout << "Exp: " << exp(2) << endl;
cout << "Log: " << log(100) << endl;
cout << "Log10: " << log10(100) << endl;
}
Dividing whole numbers in C++ always results in another whole number:
{CPP_CONSOLE}
int a = 5 / 2;
double b = 5 / 2;
double c = 5.0 / 2;
double d = 5 / 2.0;
double e = 5.0 / 2.0;
int f = 5 / 2.0;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
cout << "d = " << d << endl;
cout << "e = " << e << endl;
cout << "f = " << f << endl;
{/CPP_CONSOLE}
The remainder after division (modulo):
{CPP_CONSOLE}
cout << (5 % 2) << endl; // prints 1
{/CPP_CONSOLE}
Would you like to learn more? A complete lesson on this topic follows.
In today's lesson of the C++ basics course, we're going to take a look at the
cmath
standard library. It provides a variety of functions for
solving common math problems, we're going to mention the most important of them
all.
fmin()
, fmax()
,
fdim()
Let's start with the simple functions All 3 functions take two numbers of the double
data type
as parameters. Fmin()
returns the smallest number,
fmax()
returns the largest one. The fdim()
function
returns x-y
if (x > y
) or 0
otherwise.
round()
, ceil()
,
floor()
, and trunc()
All three functions are related to rounding and they all accept a parameter
of the double
type. Their return value is also of the
double
type. Round()
takes a decimal number as a
parameter and returns the number, rounded as a double data
type. It rounds in the same way we learned in school (anything over
0.5
is rounded upwards, otherwise the number is rounded downwards).
Ceil()
rounds upwards and floor()
rounds downwards no
matter what. Trunc()
cuts the decimal part off and leaves the whole
number part intact (does not round it whatsoever).
We'll certainly be using round()
very often. I've used the other
functions for things like determining the number of pages in a table (e.g. of a
table printed to the console). When we have 33
items and we only
print 10
items per page, they would take up 3.3
pages.
Therefore, the result must be rounded up since we would actually need
4
pages.
If you think that floor()
and truncate()
do the
same thing, think again! They behave differently for negative numbers.
Floor()
rounds negative numbers down to an even "more negative"
number, trunc()
always rounds to zero when the input is
negative.
We round decimal numbers and store them in int
variables like
this:
double d = 2.72; int a = (int)round(d);
Casting to int
is necessary despite the fact that the
round()
method returns a whole number. It is still of the
double
type, due to the fact that all mathematical functions have
the same interface. The return types of the following functions won't be
mentioned anymore (since they'll all be double
).
abs()
and signbit()
Abs()
returns the absolute value of its parameter and
signbit()
returns 1
if the parameter is negative or
0
otherwise. The signbit()
function is not supported
by all compilers so it's possible that it won't be available everywhere.
sin()
, cos()
,
tan()
Classic trigonometric functions, all take an angle which has to be entered in
radians (not degrees if your country uses them). To convert degrees to radians
we multiply them by (M_PI / 180)
.
acos()
, asin()
,
atan()
Inverse trigonometric (arcus, sometimes cyclometric) functions, which return
the original angle according to the trigonometric value. They're the inverse
functions for sin()
, cos()
, and tan()
.
The parameter is a function value and the returned value is the original angle
in radians (returned as a double
). If we wanted to get an angle in
degrees, we'd have to divide the radians by (180 / M_PI)
.
pow()
and sqrt()
Pow()
takes two input parameters. The first is the base of the
power and the second is the exponent. If we wanted to calculate something like
23, the code would be as follows (don't forget to include the cmath
library for all of the examples):
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout << pow(2, 3);
return 0;
}
Sqrt()
is an abbreviation of SQuare RooT, which returns the
square root of the number given as a double.
exp()
, log()
,
log10()
Exp()
returns Euler's number raised to the given exponent.
Log()
returns the natural logarithm of the given number.
Log10()
returns the decadic logarithm of the number.
Hopefully, you noticed that the method list lacks any general root function. We can, however, calculate it using the functions the cmath library provides.
We know that roots work like this: 3rd root of 8 = 8^(1/3)
.
Therefore, we can write:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout << pow(8, (1.0/3.0));
return 0;
}
It is very important to write at least one number with a decimal point when
we are dividing, otherwise, C++ will assume that we want it to apply
whole-number division, and the result would have been 8 ^ 0 = 1
in
this case.
Division
Programming languages often differ in how they perform the division of numbers. You need to be aware of these issues to avoid being, unpleasantly, surprised afterwards. Let's write a simple program:
{CPP_CONSOLE}
int a = 5 / 2;
double b = 5 / 2;
double c = 5.0 / 2;
double d = 5 / 2.0;
double e = 5.0 / 2.0;
int f = 5 / 2.0;
cout << "a=" << a << " b=" << b << " c=" << c << " d=" << d << " e=" << e << " f=" << f << endl;
{/CPP_CONSOLE}
We divide 5/2
several times in the code. Mathematically, it's
2.5
. Nonetheless, the results will not be the same in all cases.
Can you guess what we'll get in each case? Go ahead, give it a try
The program output will be as follows:
Console application
a=2 b=2 c=2.5 d=2.5 e=2.5 f=2
We see the result of this division is sometimes decimal and sometimes whole. The data type of the variable we're assigning the result to is not all that matters. What matters most is the data type of the numbers we divide by. If one of the numbers is decimal, the outcome will always be a decimal number. The division of 2 integers always returns an integer. Keep in mind that if you compute the average and want a decimal result, at least one variable must be cast to double.
int sum = 10; int count = 4; double average = (double)sum / (double)count;
Note: For example, the PHP language always returns the decimal result for a division. When you divide in different programming languages make sure you check how division works internally before you use it.
The remainder after division
In our applications, we often need the remainder after dividing integers
(i.e. modulo). In our example 5/2
, the integer result is
2
and the modulo is 1
(what left over). Modulo is
often used to determine whether a number is even (the remainder of a division by
2
is 0
). You would also use it, for example, to draw a
checkerboard and fill in the fields based on whether they are even or odd,
calculate the deviance of your position in a square grid, etc.
In C++ and C-like languages in general, modulo is a percent sign, i.e.
%
:
{CPP_CONSOLE}
cout << "The remainder after 5 / 2 is " << 5 % 2 << endl; // prints 1
{/CPP_CONSOLE}
We're done for today. In the next lesson, Solved tasks for C++ lessons 11-12, we'll introduce declaring custom functions, which is very important since doing so allows us to split our program up into multiple logical parts.
In the following exercise, Solved tasks for C++ lessons 11-12, we're gonna practice our knowledge from previous lessons.