You already know that C++ provides different built in data types to work with numbers. The ranges of the data types are discussed in the “C++ Data Types” article. Now we will discuss a little bit more advanced techniques to work with numbers.
C++ library called cmath offers capability to perform different mathematical operations with numbers. To be able to use these functions you have to add an include statement in your program:
//use math #include <cmath>
Now you can use the following capabilities of cmath:
Table of Contents
1. Trigonometric functions
sine, cosine, tangent. All these functions take a double value of the radians as the parameter:
const double PI = 3.141592653589793238463; //convert 60 angles to radians double angle = 60 * PI / 180; cout << "sine of 60 is " << sin(angle) << endl; cout << "cosine of 60 is " << cos(angle) << endl; cout << "tangent of 60 is " << tan(angle) << endl;
It produces the following output:
sine of 60 is 0.866025
cosine of 60 is 0.5
tangent of 60 is 1.73205
2. length of the hypotenuse
You can get the length of the hypotenuse for a right angled triangle with the sides of length “a” and “b” – hypot(a, b):
cout << "The hypotenuse of the right triangle with one side 3 and the other 4 is " << hypot(3.0, 4.0) << endl;
3. Natural logarithm functions
It takes as the parameter double value and returns it’s natural logarithm:
cout << "The natural logarithm of 10 is " << log(10) << endl;
4. Absolute value
There are two different functions to get the absolute value of an integer
int abs(int) and of a double value double fabs(double): cout << "The absolute value of -10 is " << abs(-10) << endl; cout << "The absolute value of 12.71 is " << fabs(12.71) << endl;
5. Square Root
To get the square root of a double value use sqrt(double) function:
cout << "The square root of 16 is " << sqrt(16.0) << endl;
6. Power
If you want to rise a double number to a power, you can use power(double base, double power):
cout << "2 to the power 3 is " << pow(2.0,3.0) << endl;
If you will put all these lines of examples in one program, you will get the following output:
sine of 60 is 0.866025
cosine of 60 is 0.5
tangent of 60 is 1.73205
The hypotenuse of the right triangle with one side 3 and the other 4 is 5
The natural algorithm of 10 is 2.30259
The absolute value of -10 is 10
The absolute value of 12.71 is 12.71
The square root of 16 is 4
2 to the power 3 is 8
Random Number in C++
Sometimes you will need to use random numbers in your programs. For this purpose you can use two functions from library cstdlib and time data type from ctime. You need to add the following include statements in your program:
#include <cstdlib> #include <ctime>
If you want that your program should always generates a new sequence of random numbers – you need to set a seed to random generator according to the current time. It can be done in the following way:
//set random seed srand(time_t(NULL)); After this you can get a random number by using rand() function: //generate a random numbers sequence for (int i = 0; i != 5; ++i) cout << "This is a random number " << rand() << endl;
Here is 5 random numbers, generated by this code:
This is a random number 38
This is a random number 7719
This is a random number 21238
This is a random number 2437
This is a random number 8855
Get Random Number in Specific Range
But often your task will be to get a sequence of random numbers in a specified range. To get only numbers in the range from min to max you can use this code:
int min = 5; int max = 12; cout << "This is a random number in range from 5 to 12 " << min + (rand() % (int)(max - min + 1)) << endl;
The below expression
min + (rand() % (int)(max - min + 1))
returns a random value in range from min to max