May 11, 2021 C++
Typically, when we need to use numbers, we use the original data types, such as int, short, long, float, double, and so on. These data types for numbers, their possible values, and numeric ranges are discussed in the chapter on data types in C+ .
We have defined numbers in various instances of previous chapters. Here's 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
In addition to creating a variety of functions, you can also include a variety of useful functions for your use. T hese functions are written in the standard C and C?libraries and are called built-in functions. You can refer to these functions in your program.
There are a wealth of mathematical functions built into the C++, which can perform operations on a variety of numbers. The following table lists some useful built-in math functions in C+ .
In order to take advantage of these functions, you need to refer to the math header file.
Serial number | Function & Description |
---|---|
1 |
double cos(double);
This function returns the cosine of the arc angle (Double type). |
2 |
double sin(double);
This function returns the sinusoidal of the arc angle (Double type). |
3 |
double tan(double);
This function returns to the exact angle (Double type). |
4 |
double log(double);
This function returns the natural logarithm of the parameter. |
5 |
double pow(double, double);
Assume that the first parameter is x, the second parameter is Y, then the function returns the Y times of x. |
6 |
double hypot(double, double);
This function returns the square of the square of the two parameters, that is, the parameter is a straight angle side of one right triangle, and the function returns the length of the bevel. |
7 |
double sqrt(double);
This function returns the square root of the parameter. |
8 |
int abs(int);
This function returns an absolute value of an integer. |
9 |
double fabs(double);
This function returns an absolute value of any decimal number. |
10 |
double floor(double);
This function returns a maximum integer that is less than or equal to the incoming parameter. |
Here's a simple example of math:
#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 produces the following results:
sign(d) :-0.634939 abs(i) :1000 floor(d) :200 sqrt(f) :15.1812 pow( d, 2 ) :40149.7
In many cases, random numbers need to be generated. F or random number generators, there are two related functions. O ne is rand(), which returns only one pseudo-random number. The srand() function must be called before a random number can be generated.
Here's a simple example of generating random numbers. The time() function is used in the instance to get the number of seconds of system time, and a random number is generated by calling the rand() function:
#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 produces the following results:
随机数: 1748144778 随机数: 630873888 随机数: 2134540646 随机数: 219404170 随机数: 902129458 随机数: 920445370 随机数: 1319072661 随机数: 257938873 随机数: 1256201101 随机数: 580322989