C++ numbers


Usually, when we need to use numbers, we use primitive data types, such as int, short, long, float, double, etc. These data types for numbers, their possible values ​​and numerical ranges, were discussed in the chapter C++ Data Types.

C++ Defining Numbers

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

#include <iostream>
using namespace std;
 
int main ()
{
   // 数字定义
   short  s;
   int    i;
   long   l;
   float  f;
   double d;
   
   // 数字赋值
   s = 10;      
   i = 1000;    
   l = 1000000; 
   f = 230.47;  
   d = 30949.374;
   
   // 数字输出
   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 results:

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 contains various useful functions for you to use. These functions are written in 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 that can perform operations on various numbers. The following table lists some useful built-in mathematical functions in C++.

To take advantage of these functions, you need to reference the math header file <cmath>.

##12345678910

The following is a simple example of a mathematical operation:

#include <iostream>
#include <cmath>
using namespace std;
 
int main ()
{
   // 数字定义
   short  s = 10;
   int    i = -1000;
   long   l = 100000;
   float  f = 230.47;
   double d = 200.374;

   // 数学运算
   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:

sign(d) :-0.634939
abs(i)  :1000
floor(d) :200
sqrt(f) :15.1812
pow( d, 2 ) :40149.7

C++ random number

In many cases, it is necessary to generate random numbers. Regarding random number generators, there are two related functions. One is rand(), this function only returns a pseudo-random number. The srand() function must be called before generating random numbers.

The following is a simple example of generating random numbers. The time() function is used in the example to obtain the seconds of the system time, and the rand() function is called to generate random numbers:

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;
 
int main ()
{
   int i,j;
 
   // 设置种子
   srand( (unsigned)time( NULL ) );

   /* 生成 10 个随机数 */
   for( i = 0; i < 10; i++ )
   {
      // 生成实际的随机数
      j= rand();
      cout <<"随机数: " << j << endl;
   }

   return 0;
}

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

随机数: 1748144778
随机数: 630873888
随机数: 2134540646
随机数: 219404170
随机数: 902129458
随机数: 920445370
随机数: 1319072661
随机数: 257938873
随机数: 1256201101
随机数: 580322989
Serial numberFunction & Description
double cos( double);This function returns the cosine of the radian angle (double type).
double sin(double);This function returns the sine of the angle in radians (double type).
double tan(double);This function returns the tangent of the radian angle (double type).
double log(double);This function returns the natural logarithm of the parameter.
double pow(double, double);Assume the first parameter is x and the second parameter is y , then the function returns x raised to the yth power.
double hypot(double, double);This function returns the square root of the sum of the squares of the two parameters, that is Say, the parameters are two right-angled sides of a right triangle, and the function will return the length of the hypotenuse.
double sqrt(double);This function returns the square root of the parameter.
int abs(int);This function returns the absolute value of an integer.
double fabs(double);This function returns the absolute value of any decimal number.
double floor(double);This function returns the largest integer that is less than or equal to the passed parameter.