## Lesson 11 - Mathematical functions in C++ - The cmath library

C and C++ C++ Basics Mathematical functions in C++ - The cmath library

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 2^{3},
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 applicationa=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, Functions in the C++ language, we'll introduce declaring custom functions, which is very important since doing so allows us to split our program up into multiple logical parts.

**Unicorn College**- prestigious college providing IT and economical education.

**Comments**

Nobody has commented yet - be the first!