English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ number

Generally, when we need to use numbers, we use primitive data types such as int, short, long, float, and double, etc. The possible values and value ranges of these data types used for numbers, we have already discussed in the C++ discussed in the chapter on data types.

C++ number definition

We have defined numbers in various examples in previous chapters. Here is a C++ A comprehensive example of defining various types of numbers:

#include <iostream>
using namespace std;
 
int main()
{
   // Number definition
   short s;
   int i;
   long l;
   float f;
   double d;
   
   // Number Assignment
   s = 25;      
   i = 3000;    
   l = 5000000; 
   f = 530.57;  
   d = 50348.574;
   
   // Number Output
   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 will produce the following results:

short s ;25
int i ;3000
long l ;5000000
float f ;530.57
double d ;50348.6

C++ Mathematical operations

in C++ which not only allows you to create various functions but also includes various useful functions for you to use. These functions are written in standard C and C++ library, calledBuilt-infunctions. You can use these functions in your program.

C++ Built-in rich mathematical functions, which can perform operations on various numbers. The following table lists C++ to use some useful built-in mathematical functions.

To use these functions, you need to include the math header file <cmath>.

Serial NumberFunction & Description
1double cos(double);
This function returns the cosine of the angle in radians (double type).
2double sin(double);
This function returns the sine of the angle in radians (double type).
3double tan(double);
This function returns the tangent of the radian angle (double type).
4double log(double);
This function returns the natural logarithm of the parameter.
5double pow(double, double);
Assuming the first parameter is x and the second parameter is y, this function returns x to the power of y.
6double hypot(double, double);
This function returns the square root of the sum of the squares of two parameters, that is, the parameters are the two legs of a right triangle, and the function returns the length of the hypotenuse.
7double sqrt(double);
This function returns the square root of the parameter.
8int abs(int);
This function returns the absolute value of an integer.
9double fabs(double);
This function returns the absolute value of any floating-point number.
10double floor(double);
This function returns the largest integer less than or equal to the passed parameter.

Below is a simple example of mathematical operations:

#include <iostream>
#include <cmath>
using namespace std;
 
int main()
{
   // Number definition
   short s = 20;
   int i = -2000;
   long l = 200000;
   float f = 280.45;
   double d = 300.456;
 
   // 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 will produce the following results:

sin(d)-0.907332
abs(i)2000
floor(d)300
sqrt(f)16.7466
pow( 2) :90273.8

C++ random number

In many cases, it is necessary to generate random numbers. There are two related functions for random number generation. One is rand(), which only returns a pseudo-random number. It is necessary to call srand() function.

Below is a simple example of generating random numbers. The example uses time() A function to get the second count of the system time, generating random numbers by calling the rand() function:

#include <iostream>
#include <ctime>
#include <cstdlib>
 
using namespace std;
 
int main()
{
   int i, j;
 
   // Set Seed
   srand((unsigned)time(NULL));
 
   /* Generate 8 random numbers */
   for(i = 0; i < 8; i++ )
   {
      // Generate actual random numbers
      j = rand();
      cout << "Random Number: " << j << endl;
   }
 
   return 0;
}

When the above code is compiled and executed, it will produce the following results:

Random Number: 21763
Random Number: 15941
Random Number: 846
Random Number: 16376
Random Number: 31767
Random Number: 28949
Random Number: 22265
Random Number: 21475