-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
48 lines (42 loc) · 1.34 KB
/
Copy pathmain.cpp
File metadata and controls
48 lines (42 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <vector>
// int main() {
// std::vector arr = {1, 2, 3};
// std::cout << "This is a test" << std::endl;
//
// // 开启 asan 越界检查,asan 会有一个 Sanitizer 显示 warning,点击
// // 会显示具体越界的数组
// // constexpr double arr2[] = {1.0, 2, 3};
// // std::cout << arr2[3] << std::endl;
//
// // 使用指针访问,这会触发 ASan 的内存越界检查,而不是 C++ 异常
// const int *ptr = arr.data();
// std::cout << "Attempting to access out-of-bounds memory..." << std::endl;
// // 强制越界访问
// std::cout << ptr[10] << std::endl;
//
// // 这种会直接抛出异常,asan 不会介入
// arr.at(4) = 43; // 故意越界
// std::cout << "This will not be printed if ASan catches the error" << std::endl;
//
// return 0;
// }
#include <cmath>
#include <iomanip>
#include <iostream>
double poisson_pmf_log(int k, double lambda) {
// ln(P(X=k)) = k * ln(lambda) - lambda - ln(k!)
return k * std::log(lambda) - lambda - std::lgamma(k + 1);
}
int main() {
double lambda = 20.0;
double cdf = 0.0;
std::cout << std::setprecision(15); // 打印高精度结果
for (int k = 0; k <= 39; ++k) {
cdf += std::exp(poisson_pmf_log(k, lambda));
if (k >= 37) {
std::cout << "k = " << k << " | CDF = " << cdf << std::endl;
}
}
return 0;
}