📅  最后修改于: 2020-12-17 05:06:32             🧑  作者: Mango
通常,在使用Numbers时,我们使用原始数据类型,例如int,short,long,float和double等。在讨论C++数据类型时,已经解释了number数据类型,其可能的值和数字范围。
在上一章中给出的各种示例中,您已经定义了数字。这是另一个在C++中定义各种类型数字的合并示例-
#include
using namespace std;
int main () {
// number definition:
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 printing;
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;
}
编译并执行上述代码后,将产生以下结果-
short s :10
int i :1000
long l :1000000
float f :230.47
double d :30949.4
除了可以创建的各种功能之外,C++还包括一些可以使用的有用功能。这些函数在标准C和C++库中可用,并且称为内置函数。这些功能可以包含在您的程序中,然后使用。
C++具有一组丰富的数学运算,可以对各种数字进行运算。下表列出了C++中可用的一些有用的内置数学函数。
要使用这些功能,您需要包括数学头文件
Sr.No | Function & Purpose |
---|---|
1 |
double cos(double); This function takes an angle (as a double) and returns the cosine. |
2 |
double sin(double); This function takes an angle (as a double) and returns the sine. |
3 |
double tan(double); This function takes an angle (as a double) and returns the tangent. |
4 |
double log(double); This function takes a number and returns the natural log of that number. |
5 |
double pow(double, double); The first is a number you wish to raise and the second is the power you wish to raise it t |
6 |
double hypot(double, double); If you pass this function the length of two sides of a right triangle, it will return you the length of the hypotenuse. |
7 |
double sqrt(double); You pass this function a number and it gives you the square root. |
8 |
int abs(int); This function returns the absolute value of an integer that is passed to it. |
9 |
double fabs(double); This function returns the absolute value of any decimal number passed to it. |
10 |
double floor(double); Finds the integer which is less than or equal to the argument passed to it. |
以下是一个简单的示例,显示了一些数学运算-
#include
#include
using namespace std;
int main () {
// number definition:
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;
}
编译并执行上述代码后,将产生以下结果-
sign(d) :-0.634939
abs(i) :1000
floor(d) :200
sqrt(f) :15.1812
pow( d, 2 ) :40149.7
在许多情况下,您希望生成一个随机数。实际上,您需要了解两个有关随机数生成的功能。第一个是rand() ,此函数将仅返回伪随机数。解决此问题的方法是先调用srand()函数。
以下是一个生成少量随机数的简单示例。这个例子利用time()函数获取系统时间的秒数,随机植入rand()函数-
#include
#include
#include
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 : 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