赞
踩
@Author:Runsen
C++提供了大量的数学函数,可以直接在程序中使用。
作为 C 语言的一个子集,C++ 从 C 的 math.h 头文件中派生出大部分这些数学函数。
在 C++ 中,数学函数包含在头文件<cmath>
中。
下面列出了 C++ 中的重要数学函数及和示例
1 | cos | cout<< cos ( 60.0 * PI / 180.0 ); |
---|---|---|
2 | sin | con ( 60.0 * PI / 180.0 ); |
3 | tan | out<< tan ( 45.0 * PI / 180.0 ) |
4 | acos | double param = 0.5; cout<< acos (param) * 180.0 / PI; |
5 | asin | cout<< asin (param) * 180.0 / PI; 31.4159 |
6 | atan | cout<< atan (param) * 180.0 / PI; |
7 | pow | cout<<”2^3 = “<< pow(2,3); |
8 | sqrt | cout<< sqrt(49); |
9 | ceil | cout<< ceil(3.8); |
10 | floor | cout<< floor(2.3); |
11 | fmod | cout<< fmod(5.3,2); 1.3 |
12 | trunc | cout<< trunc(2.3); 2 |
13 | round | cout<< round(4.6); 5 |
14 | remainder | cout<< remainder(18.5 ,4.2); 1.7 |
15 | fmax | cout<< fmax(100.0,1.0); 100 |
16 | fmin | cout<< fmin(100.0,1.0); 1 |
17 | fdim | cout<< fdim(2.0,1.0); 1 |
18 | fabs | cout<< fabs(3.1416); 3.1416 |
19 | abs | cout<< abs(3.1416); 3.1416 |
20 | exp | cout<< exp(5.0); 148.413 |
21 | log | cout<< log(5); 1.60944 |
22 | log10 | cout<< log10(5); |
#include <iostream> #include <cmath> using namespace std; int main() { int PI = 3.142; cout << "cos(60) = " << cos(60.0 * PI / 180.0) << endl; cout << "sin(60) = " << sin(60.0 * PI / 180.0) << endl; cout << "tan(45) = " << tan(45.0 * PI / 180.0) << endl; cout << "acos(0.5) = " << acos(0.5) * 180.0 / PI << endl; cout << "asin(0.5) = " << asin(0.5) * 180.0 / PI << endl; cout << "atan(1.0) = " << atan(1.0) * 180.0 / PI << endl; cout << "2^3 = " << pow(2, 3) << endl; cout << "sqrt(49) = " << sqrt(49) << endl; cout << "ceil(3.8) = " << ceil(3.8) << endl; cout << "floor(2.3) = " << floor(2.3) << endl; cout << "fmod(5.3,2) = " << fmod(5.3, 2) << endl; cout << "trunc(5.3,2) = " << trunc(2.3) << endl; cout << "round(4.6) = " << round(4.6) << endl; cout << "remainder(18.5,4.2) = " << remainder(18.5, 4.2) << endl; cout << "fmax(100.0,1.0) = " << fmax(100.0, 1.0) << endl; cout << "fmin(100.0,1.0) = " << fmin(100.0, 1.0) << endl; cout << "fdim(2.0,1.0) = " << fdim(2.0, 1.0) << endl; cout << "fabs(3.1416) = " << fabs(3.1416) << endl; cout << "abs(3.1416) = " << abs(3.1416) << endl; cout << "log(5) = " << log(5) << endl; cout << "exp(5.0) = " << exp(5.0) << endl; cout << "log10(5) = " << log10(5) << endl; return 0; }
输出如下
cos(60) = 0.540302 sin(60) = 0.841471 tan(45) = 0.931596 acos(0.5) = 62.8319 asin(0.5) = 31.4159 atan(1.0) = 47.1239 2^3 = 8 sqrt(49) = 7 ceil(3.8) = 4 floor(2.3) = 2 fmod(5.3,2) = 1.3 trunc(5.3,2) = 2 round(4.6) = 5 remainder(18.5,4.2) = 1.7 fmax(100.0,1.0) = 100 fmin(100.0,1.0) = 1 fdim(2.0,1.0) = 1 fabs(3.1416) = 3.1416 abs(3.1416) = 3.1416 log(5) = 1.60944 exp(5.0) = 148.413 log10(5) = 0.69897
在中定义了两个类用于实现随机数:
#include <iostream>
#include <random>
using namespace std;
int main()
{
random_device example;
cout << "default random_device:" << endl;
cout << "minimum: " << example.min() << endl;
cout << "maximum: " << example.max() << endl;
cout << "entropy: " << example.entropy() << endl;
cout << "a random number: " << example() << endl;
return 0;
}
输出如下
random_device:
minimum: 0
maximum: 4294967295
entropy: 32
a random number: 773368156
uniform_int_distribution:它产生随机整数值 i,它们均匀分布在闭区间 [a,b] 上,由以下概率质量函数描述:
#include <iostream> #include <random> using namespace std; // Driver program int main() { // random generator engine unsigned s = 2; // The random number generator default_random_engine generator(s); uniform_int_distribution<int> distribution(1, 10); cout << "Some random numbers between 1 and 10"; for (int i = 0; i < 10;i++) cout << distribution(generator) << endl; cout << endl; return 0; }
uniform_real_distribution:是产生浮点值的随机数分布,由以下概率密度函数描述:
#include <iostream> #include <random> using namespace std; int main() { unsigned s = 2; // The random number generator default_random_engine generator(s); uniform_real_distribution<float> distribution(1, 10); cout << "Random numbers between 1 and 10"; for (int i = 0; i < 10; ++i) cout << distribution(generator) << endl; cout << endl; return 0; }
binomial_distribution:它是根据二项式离散分布产生整数的随机数分布,由这个概率质量函数给出:
#include <iostream> #include <random> #include <chrono> using namespace std; int main() { unsigned seed = chrono::system_clock::now().time_since_epoch().count(); default_random_engine generator(seed); binomial_distribution<int> distribution(15, 0.4); cout << "some binomial results (t=15, p=0.4): "; for (int i = 0; i < 15; ++i) { // Use of operator() cout << distribution(generator) << " "; } cout << endl; return 0; }
geometry_distribution:它是一种随机数分布,根据几何离散分布生成整数,由以下概率质量函数给出:
在这里插入图片描述
C++ 库有一个名为 rand() 的函数,每次调用该函数都将返回一个非负整数。要使用 rand() 函数,以下是其用法示例:
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 100; i++) {
cout << rand() << ' ';
}
return 0;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。