C++ Numbers
Typically, when we need to use numbers, we use primitive data types such as int, short, long, float, and double, among others. The possible values and ranges of these numeric data types were discussed in the C++ Data Types chapter.
C++ Defining Numbers
We have already defined numbers in various examples in previous chapters. Below is a comprehensive example of defining various types of numbers in C++:
Example
#include <iostream>
using namespace std;
int main ()
{
// Number definitions
short s;
int i;
long l;
float f;
double d;
// Number assignments
s = 10;
i = 1000;
l = 1000000;
f = 230.47;
d = 30949.374;
// Number outputs
cout << "short s :" << s << endl;
cout << "int i :" << i << endl;
cout << "long l :" << l << endl;
cout << "float f :" << f << endl;
cout << "double d :" << d << endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
short s :10
int i :1000
long l :1000000
float f :230.47
double d :30949.4
C++ Mathematical Operations
In C++, in addition to creating various functions, it also includes useful functions for your use. These functions are part of the standard C and C++ libraries and are called built-in functions. You can reference these functions in your program.
C++ has a rich set of built-in mathematical functions for performing various operations on numbers. The table below lists some useful C++ built-in mathematical functions.
To utilize these functions, you need to include the math header file <cmath>.
No. | Function & Description |
---|---|
1 | double cos(double); <br>This function returns the cosine of an angle (double type) in radians. |
2 | double sin(double); <br>This function returns the sine of an angle (double type) in radians. |
3 | double tan(double); <br>This function returns the tangent of an angle (double type) in radians. |
4 | double log(double); <br>This function returns the natural logarithm of the argument. |
5 | double pow(double, double); <br>Assuming the first parameter is x and the second is y, this function returns x raised to the power of y. |
6 | double hypot(double, double); <br>This function returns the square root of the sum of squares of its arguments, effectively returning the length of the hypotenuse for right-angled triangles. |
7 | double sqrt(double); <br>This function returns the square root of the argument. |
8 | int abs(int); <br>This function returns the absolute value of an integer. |
9 | double fabs(double); <br>This function returns the absolute value of any floating-point number. |
10 | double floor(double); <br>This function returns the largest integer less than or equal to the argument. |
Here is a simple example regarding mathematical operations:
Example
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
// Number definitions
short s = 10;
int i = -1000;
long l = 100000;
float f = 230.47;
double d = 200.374;
// Mathematical operations
cout << "sin(d) :" << sin(d) << endl;
cout << "abs(i) :" << abs(i) << endl;
cout << "floor(d) :" << floor(d) << endl;
cout << "sqrt(f) :" << sqrt(f) << endl;
cout << "pow( d, 2) :" << pow(d, 2) << endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
sin(d) :-0.634939
abs(i) :1000
floor(d) :200
sqrt(f) :15.1812
pow( d, 2) :40149.7
floor(d) :200 sqrt(f) :15.1812 pow( d, 2 ) :40149.7
C++ Random Numbers
In many cases, you need to generate random numbers. There are two related functions for random number generation. One is rand(), which returns a pseudo-random number. Before generating random numbers, you must call the srand() function.
Below is a simple example of generating random numbers. The example uses the time() function to get the number of seconds since the system epoch and generates random numbers by calling the rand() function:
Example
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main ()
{
int i,j;
// Set the seed
srand( (unsigned)time( NULL ) );
/* Generate 10 random numbers */
for( i = 0; i < 10; i++ )
{
// Generate actual random number
j= rand();
cout <<"Random Number: " << j << endl;
}
return 0;
}
When the above code is compiled and executed, it produces the following result:
Random Number: 1748144778
Random Number: 630873888
Random Number: 2134540646
Random Number: 219404170
Random Number: 902129458
Random Number: 920445370
Random Number: 1319072661
Random Number: 257938873
Random Number: 1256201101
Random Number: 580322989