NumPy 源码解析(三十六)
NumPy
Copyright (c) 2005-2017, NumPy Developers. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
-
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
-
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
-
Neither the name of the NumPy Developers nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Julia
The ziggurat methods were derived from Julia.
Copyright (c) 2009-2019: Jeff Bezanson, Stefan Karpinski, Viral B. Shah, and other contributors:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
.\numpy\numpy\random\src\distributions\logfactorial.c
/*
* logfact[k] holds log(k!) for k = 0, 1, 2, ..., 125.
* logfact数组存储了0到125的阶乘的自然对数值。
*/
static const double logfact[] = {
0, // log(0!) = 0
0, // log(1!) = 0
0.69314718055994529, // log(2!) ≈ 0.69314718055994529
1.791759469228055, // log(3!) ≈ 1.791759469228055
3.1780538303479458, // log(4!) ≈ 3.1780538303479458
4.7874917427820458, // log(5!) ≈ 4.7874917427820458
6.5792512120101012, // log(6!) ≈ 6.5792512120101012
8.5251613610654147, // log(7!) ≈ 8.5251613610654147
10.604602902745251, // log(8!) ≈ 10.604602902745251
12.801827480081469, // log(9!) ≈ 12.801827480081469
15.104412573075516, // log(10!) ≈ 15.104412573075516
17.502307845873887, // log(11!) ≈ 17.502307845873887
19.987214495661885, // log(12!) ≈ 19.987214495661885
22.552163853123425, // log(13!) ≈ 22.552163853123425
25.19122118273868, // log(14!) ≈ 25.19122118273868
27.89927138384089, // log(15!) ≈ 27.89927138384089
30.671860106080672, // log(16!) ≈ 30.671860106080672
33.505073450136891, // log(17!) ≈ 33.505073450136891
36.395445208033053, // log(18!) ≈ 36.395445208033053
39.339884187199495, // log(19!) ≈ 39.339884187199495
42.335616460753485, // log(20!) ≈ 42.335616460753485
45.380138898476908, // log(21!) ≈ 45.380138898476908
48.471181351835227, // log(22!) ≈ 48.471181351835227
51.606675567764377, // log(23!) ≈ 51.606675567764377
54.784729398112319, // log(24!) ≈ 54.784729398112319
58.003605222980518, // log(25!) ≈ 58.003605222980518
61.261701761002001, // log(26!) ≈ 61.261701761002001
64.557538627006338, // log(27!) ≈ 64.557538627006338
67.88974313718154, // log(28!) ≈ 67.88974313718154
71.257038967168015, // log(29!) ≈ 71.257038967168015
74.658236348830158, // log(30!) ≈ 74.658236348830158
78.092223553315307, // log(31!) ≈ 78.092223553315307
81.557959456115043, // log(32!) ≈ 81.557959456115043
85.054467017581516, // log(33!) ≈ 85.054467017581516
88.580827542197682, // log(34!) ≈ 88.580827542197682
92.136175603687093, // log(35!) ≈ 92.136175603687093
95.719694542143202, // log(36!) ≈ 95.719694542143202
99.330612454787428, // log(37!) ≈ 99.330612454787428
102.96819861451381, // log(38!) ≈ 102.96819861451381
106.63176026064346, // log(39!) ≈ 106.63176026064346
110.32063971475739, // log(40!) ≈ 110.32063971475739
114.03421178146171, // log(41!) ≈ 114.03421178146171
117.77188139974507, // log(42!) ≈ 117.77188139974507
121.53308151543864, // log(43!) ≈ 121.53308151543864
125.3172711493569, // log(44!) ≈ 125.3172711493569
129.12393363912722, // log(45!) ≈ 129.12393363912722
132.95257503561632, // log(46!) ≈ 132.95257503561632
136.80272263732635, // log(47!) ≈ 136.80272263732635
140.67392364823425, // log(48!) ≈ 140.67392364823425
144.5657439463449, // log(49!) ≈ 144.5657439463449
148.47776695177302, // log(50!) ≈ 148.47776695177302
152.40959258449735, // log(51!) ≈ 152.40959258449735
156.3608363030788, // log(52!) ≈ 156.3608363030788
160.3311282166309, // log(53!) ≈ 160.3311282166309
164.32011226319517, // log(54!) ≈ 164.32011226319517
168.32744544842765, // log(55!) ≈ 168.32744544842765
172.35279713916279, // log(56!) ≈ 172.35279713916279
176.39584840699735, // log(57!) ≈ 176.39584840699735
180.45629141754378, // log(58!) ≈ 180.45629141754378
184.53382886144948, // log(59!) ≈ 184.53382886144948
188.6281734236716, // log(60!) ≈ 188.6281734236716
192.7390472878449, // log(61!) ≈ 192.7390472878449
196.86618167289001, // log(62!) ≈ 196.86618167289001
201.00931639928152, // log(63!) ≈ 201.00931639928152
205.1681994826412, // log(64!) ≈ 205.1681994826412
209.34258675253685, // log(65!) ≈ 209.34258675253685
213.53224149456327, // log(66!) ≈ 213.53224149456327
217.73693411395422, // log(67!) ≈ 217.73693411395422
221.95644181913033, // log(68!) ≈ 221.95644181913033
226.1905483237276, // log(69!) ≈ 226.1905483237276
230.43904356577696, // log(70!) ≈ 230.43904356577696
234.70172344281826, // log(71!) ≈ 234.70172344281826
238.97838956183432, // log(72!) ≈ 238.97838956183432
243.26884900298271, // log(73!) ≈ 243.26884900298271
247
# 定义一个包含多个浮点数的列表,表示一组数值数据
numbers = [
462.60817852687489,
467.4121995716082,
472.22438392698058,
477.04466549258564,
481.87297922988796
]
};
/*
* 计算 log(k!) 的函数
*/
double logfactorial(int64_t k)
{
// 预先计算的常数,等于 ln(sqrt(2*pi))
const double halfln2pi = 0.9189385332046728;
// 如果 k 在预先计算的 logfact 数组的索引范围内,则直接使用查找表中的值
if (k < (int64_t) (sizeof(logfact)/sizeof(logfact[0]))) {
/* 使用查找表中的值 */
return logfact[k];
}
/*
* 使用斯特林级数展开,截断到 1/k**3 项。
* (在一个 Python 实现中,该近似方法对于 k 最大达到 10000000 时,结果在最佳 64 位浮点值的 ±2 个最低位误差之内。)
*/
return (k + 0.5)*log(k) - k + (halfln2pi + (1.0/k)*(1/12.0 - 1/(360.0*k*k)));
}
.\numpy\numpy\random\src\distributions\logfactorial.h
#ifndef LOGFACTORIAL_H
#define LOGFACTORIAL_H
# 如果 LOGFACTORIAL_H 宏未定义,则定义它,这样可以避免头文件的多重包含问题
#include <stdint.h>
# 包含标准整数类型头文件,以便使用 int64_t 这种整数类型
double logfactorial(int64_t k);
# 声明一个函数 logfactorial,接受一个 int64_t 类型的整数参数 k,返回一个 double 类型的浮点数
#endif
# 结束条件编译指令,确保当 LOGFACTORIAL_H 宏已定义时,头文件内容正常结束
.\numpy\numpy\random\src\distributions\random_hypergeometric.c
/*
* Generate a sample from the hypergeometric distribution.
*
* Assume sample is not greater than half the total. See below
* for how the opposite case is handled.
*
* We initialize the following:
* computed_sample = sample
* remaining_good = good
* remaining_total = good + bad
*
* In the loop:
* * computed_sample counts down to 0;
* * remaining_good is the number of good choices not selected yet;
* * remaining_total is the total number of choices not selected yet.
*
* In the loop, we select items by choosing a random integer in
* the interval [0, remaining_total), and if the value is less
* than remaining_good, it means we have selected a good one,
* so remaining_good is decremented. Then, regardless of that
* result, computed_sample is decremented. The loop continues
* until either computed_sample is 0, remaining_good is 0, or
* remaining_total == remaining_good. In the latter case, it
* means there are only good choices left, so we can stop the
* loop early and select what is left of computed_sample from
* the good choices (i.e. decrease remaining_good by computed_sample).
*
* When the loop exits, the actual number of good choices is
* good - remaining_good.
*
* If sample is more than half the total, then initially we set
* computed_sample = total - sample
* and at the end we return remaining_good (i.e. the loop in effect
* selects the complement of the result).
*
* It is assumed that when this function is called:
* * good, bad and sample are nonnegative;
* * the sum good+bad will not result in overflow;
* * sample <= good+bad.
*/
static int64_t hypergeometric_sample(bitgen_t *bitgen_state,
int64_t good, int64_t bad, int64_t sample)
{
int64_t remaining_total, remaining_good, result, computed_sample;
int64_t total = good + bad;
if (sample > total/2) {
computed_sample = total - sample; // Set computed_sample to the complement when sample is more than half the total
}
else {
computed_sample = sample; // Use sample directly when it is less than or equal to half the total
}
remaining_total = total;
remaining_good = good;
while ((computed_sample > 0) && (remaining_good > 0) &&
(remaining_total > remaining_good)) {
// random_interval(bitgen_state, max) returns an integer in
// [0, max] *inclusive*, so we decrement remaining_total before
// passing it to random_interval().
--remaining_total; // Decrement remaining_total since random_interval is inclusive
if ((int64_t) random_interval(bitgen_state,
remaining_total) < remaining_good) {
// Selected a "good" one, so decrement remaining_good.
--remaining_good; // Decrement remaining_good for each "good" choice selected
}
--computed_sample; // Decrement computed_sample for each iteration of the loop
}
if (remaining_total == remaining_good) {
// Only "good" choices are left, adjust remaining_good accordingly
remaining_good -= computed_sample;
}
if (sample > total/2) {
result = remaining_good; // Return remaining_good as result when sample is more than half the total
}
# 如果条件不满足,则执行以下语句块
else:
# 计算 result 的值为 good 减去 remaining_good
result = good - remaining_good
# 返回变量 result 的值作为函数的结果
return result
// 结束预处理指令部分
// 定义常数 D1 和 D2,这些常数用于计算超几何分布的变量
#define D1 1.7155277699214135
#define D2 0.8989161620588988
/*
* 使用比例-均匀算法生成超几何分布的变量
*
* 在代码中,变量名 a, b, c, g, h, m, p, q, K, T, U 和 X 对应于
* Stadlober 1989 年论文中“算法 HRUA”从第 82 页开始使用的名称。
*
* 假设调用此函数时:
* * good, bad 和 sample 都为非负数;
* * good+bad 的和不会导致溢出;
* * sample <= good+bad。
*
* 参考文献:
* - Ernst Stadlober 的论文 "Sampling from Poisson, Binomial and
* Hypergeometric Distributions: Ratio of Uniforms as a Simple and
* Fast Alternative" (1989)
* - Ernst Stadlober, "The ratio of uniforms approach for generating
* discrete random variates", Journal of Computational and Applied
* Mathematics, 31, pp. 181-189 (1990).
*/
// 声明静态函数 hypergeometric_hrua,返回值类型为 int64_t
static int64_t hypergeometric_hrua(bitgen_t *bitgen_state,
int64_t good, int64_t bad, int64_t sample)
{
// 声明变量
int64_t mingoodbad, maxgoodbad, popsize;
int64_t computed_sample;
double p, q;
double mu, var;
double a, c, b, h, g;
int64_t m, K;
// 计算总体大小
popsize = good + bad;
// 计算实际采样量
computed_sample = MIN(sample, popsize - sample);
// 计算最小的 good 和 bad 的值
mingoodbad = MIN(good, bad);
// 计算最大的 good 和 bad 的值
maxgoodbad = MAX(good, bad);
/*
* 不与 Stadlober (1989) 相符的变量
* 这里 Stadlober
* ---------------- ---------
* mingoodbad M
* popsize N
* computed_sample n
*/
// 计算成功概率 p 和失败概率 q
p = ((double) mingoodbad) / popsize;
q = ((double) maxgoodbad) / popsize;
// mu 是分布的均值
mu = computed_sample * p;
// var 是分布的方差
var = ((double)(popsize - computed_sample) *
computed_sample * p * q / (popsize - 1));
// 计算参数 a 和 c
a = mu + 0.5;
c = sqrt(var + 0.5);
/*
* h 是 2*s_hat(参见 Stadlober 的论文 (1989),公式 (5.17);
* 或 Stadlober (1990),公式 8)。s_hat 是主导标准化超几何 PMF 的
* “表山”函数的比例尺度(“标准化”意味着具有最大值为 1)。
*/
h = D1*c + D2;
// 计算参数 m
m = (int64_t) floor((double)(computed_sample + 1) * (mingoodbad + 1) /
(popsize + 2));
// 计算参数 g
g = (logfactorial(m) +
logfactorial(mingoodbad - m) +
logfactorial(computed_sample - m) +
logfactorial(maxgoodbad - computed_sample + m));
/*
* b is the upper bound for random samples:
* ... min(computed_sample, mingoodbad) + 1 is the length of the support.
* ... floor(a + 16*c) is 16 standard deviations beyond the mean.
*
* The idea behind the second upper bound is that values that far out in
* the tail have negligible probabilities.
*
* There is a comment in a previous version of this algorithm that says
* "16 for 16-decimal-digit precision in D1 and D2",
* but there is no documented justification for this value. A lower value
* might work just as well, but I've kept the value 16 here.
*/
// 计算随机样本的上界
b = MIN(MIN(computed_sample, mingoodbad) + 1, floor(a + 16*c));
// 开始采样循环,直到满足条件退出循环
while (1) {
double U, V, X, T;
double gp;
// 生成两个均匀分布的随机数
U = next_double(bitgen_state);
V = next_double(bitgen_state); // "U star" in Stadlober (1989)
// 计算变量 X
X = a + h*(V - 0.5) / U;
// 快速拒绝策略:
if ((X < 0.0) || (X >= b)) {
// 如果 X 不在有效范围内,则继续下一次循环
continue;
}
// 计算 K,并转换为整数
K = (int64_t) floor(X);
// 计算 gp 值
gp = (logfactorial(K) +
logfactorial(mingoodbad - K) +
logfactorial(computed_sample - K) +
logfactorial(maxgoodbad - computed_sample + K));
// 计算 T 值
T = g - gp;
// 快速接受策略:
if ((U*(4.0 - U) - 3.0) <= T) {
// 如果条件满足,则退出循环
break;
}
// 快速拒绝策略:
if (U*(U - T) >= 1) {
// 如果条件不满足,则继续下一次循环
continue;
}
if (2.0*log(U) <= T) {
// 接受样本
break;
}
}
// 根据条件调整 K 的值
if (good > bad) {
K = computed_sample - K;
}
if (computed_sample < sample) {
K = good - K;
}
// 返回最终确定的 K 值
return K;
}
.\numpy\numpy\random\src\distributions\random_mvhg_count.c
/*
* random_multivariate_hypergeometric_count
*
* Draw variates from the multivariate hypergeometric distribution--
* the "count" algorithm.
*
* Parameters
* ----------
* bitgen_t *bitgen_state
* Pointer to a `bitgen_t` instance.
* int64_t total
* The sum of the values in the array `colors`. (This is redundant
* information, but we know the caller has already computed it, so
* we might as well use it.)
* size_t num_colors
* The length of the `colors` array.
* int64_t *colors
* The array of colors (i.e. the number of each type in the collection
* from which the random variate is drawn).
* int64_t nsample
* The number of objects drawn without replacement for each variate.
* `nsample` must not exceed sum(colors). This condition is not checked;
* it is assumed that the caller has already validated the value.
* size_t num_variates
* The number of variates to be produced and put in the array
* pointed to by `variates`. One variate is a vector of length
* `num_colors`, so the array pointed to by `variates` must have length
* `num_variates * num_colors`.
* int64_t *variates
* The array that will hold the result. It must have length
* `num_variates * num_colors`.
* The array is not initialized in the function; it is expected that the
* array has been initialized with zeros when the function is called.
*
* Notes
* -----
* The "count" algorithm for drawing one variate is roughly equivalent to the
* following numpy code:
*
* choices = np.repeat(np.arange(len(colors)), colors)
* selection = np.random.choice(choices, nsample, replace=False)
* variate = np.bincount(selection, minlength=len(colors))
*
* This function uses a temporary array with length sum(colors).
*
* Assumptions on the arguments (not checked in the function):
* * colors[k] >= 0 for k in range(num_colors)
* * total = sum(colors)
* * 0 <= nsample <= total
* * the product total * sizeof(size_t) does not exceed SIZE_MAX
* * the product num_variates * num_colors does not overflow
*/
int random_multivariate_hypergeometric_count(bitgen_t *bitgen_state,
int64_t total,
size_t num_colors, int64_t *colors,
int64_t nsample,
size_t num_variates, int64_t *variates)
{
size_t *choices;
bool more_than_half;
if ((total == 0) || (nsample == 0) || (num_variates == 0)) {
// 如果 total, nsample 或者 num_variates 为 0,则不进行任何操作,直接返回
return 0;
}
// 为 choices 数组分配内存空间,大小为 total 乘以 sizeof(size_t)
choices = malloc(total * (sizeof *choices));
if (choices == NULL) {
// 如果分配内存失败,返回错误码 -1
return -1;
}
/*
* 如果 colors 数组包含,例如,[3 2 5],那么 choices 数组将包含 [0 0 0 1 1 2 2 2 2 2]。
*/
// 循环遍历每种颜色的个数,生成选择数组
for (size_t i = 0, k = 0; i < num_colors; ++i) {
// 根据每种颜色的个数,依次将该颜色索引添加到选择数组中
for (int64_t j = 0; j < colors[i]; ++j) {
choices[k] = i;
++k;
}
}
// 检查采样数是否超过总数的一半
more_than_half = nsample > (total / 2);
if (more_than_half) {
// 如果超过一半,则将采样数调整为总数减去采样数
nsample = total - nsample;
}
// 对每个变量和每种颜色的组合进行采样和洗牌
for (size_t i = 0; i < num_variates * num_colors; i += num_colors) {
/*
* Fisher-Yates 洗牌算法,但只针对选择数组的前 `nsample` 个条目。
* 循环结束后,choices[:nsample] 包含从整个数组中随机采样的结果。
*/
for (size_t j = 0; j < (size_t) nsample; ++j) {
size_t tmp, k;
// 注意:nsample 不大于 total,因此在 `(size_t) total - j - 1` 中不会发生整数下溢
k = j + (size_t) random_interval(bitgen_state,
(size_t) total - j - 1);
tmp = choices[k];
choices[k] = choices[j];
choices[j] = tmp;
}
/*
* 计算 choices[:nsample] 中每个值的出现次数。
* 结果存储在 sample[i:i+num_colors] 中,表示多元超几何分布的样本。
*/
for (size_t j = 0; j < (size_t) nsample; ++j) {
variates[i + choices[j]] += 1;
}
// 如果采样数超过总数的一半,则进行特殊处理
if (more_than_half) {
for (size_t k = 0; k < num_colors; ++k) {
variates[i + k] = colors[k] - variates[i + k];
}
}
}
// 释放动态分配的选择数组内存
free(choices);
// 返回成功代码
return 0;
}
# 结束函数或代码块的定义,这里表示一个函数或类的结束。
.\numpy\numpy\random\src\distributions\random_mvhg_marginals.c
#include "numpy/random/distributions.h"
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <math.h>
#include "logfactorial.h"
// 定义一个函数,用于生成多元超几何分布的边缘分布样本
void random_multivariate_hypergeometric_marginals(bitgen_t *bitgen_state,
int64_t total,
size_t num_colors, int64_t *colors,
int64_t nsample,
size_t num_variates, int64_t *variates)
{
bool more_than_half;
// 如果总数、样本数或变量数为零,无需执行任何操作,直接返回
if ((total == 0) || (nsample == 0) || (num_variates == 0)) {
// Nothing to do.
return;
}
// 判断样本数是否超过总数的一半
more_than_half = nsample > (total / 2);
if (more_than_half) {
// 如果样本数超过总数的一半,则调整样本数为总数减去样本数
nsample = total - nsample;
}
// 遍历每个变量和颜色的组合
for (size_t i = 0; i < num_variates * num_colors; i += num_colors) {
int64_t num_to_sample = nsample;
int64_t remaining = total;
// 对于每个颜色,采样指定数量的对象
for (size_t j = 0; (num_to_sample > 0) && (j + 1 < num_colors); ++j) {
int64_t r;
remaining -= colors[j];
// 使用随机超几何分布生成随机样本数
r = random_hypergeometric(bitgen_state,
colors[j], remaining, num_to_sample);
variates[i + j] = r;
num_to_sample -= r;
}
// 如果仍有剩余的样本需要采样,则将其放入最后一个颜色的位置
if (num_to_sample > 0) {
variates[i + num_colors - 1] = num_to_sample;
}
// 如果样本数超过了总数的一半,需要进行反转操作
if (more_than_half) {
for (size_t k = 0; k < num_colors; ++k) {
variates[i + k] = colors[k] - variates[i + k];
}
}
}
}
.\numpy\numpy\random\src\distributions\ziggurat_constants.h
// 定义一个静态常量数组 ki_double,包含64位无符号整数,表示双精度浮点数的位模式
static const uint64_t ki_double[] = {
// 下面是具体的双精度浮点数位模式数据
0x000EF33D8025EF6AULL, // 第一个双精度浮点数的位模式
0x0000000000000000ULL, // 第二个双精度浮点数的位模式
0x000C08BE98FBC6A8ULL, // 第三个双精度浮点数的位模式
0x000DA354FABD8142ULL, // 第四个双精度浮点数的位模式
0x000E51F67EC1EEEAULL, // 第五个双精度浮点数的位模式
0x000EB255E9D3F77EULL, // 第六个双精度浮点数的位模式
0x000EEF4B817ECAB9ULL, // 第七个双精度浮点数的位模式
0x000F19470AFA44AAULL, // 第八个双精度浮点数的位模式
0x000F37ED61FFCB18ULL, // 第九个双精度浮点数的位模式
0x000F4F469561255CULL, // 第十个双精度浮点数的位模式
0x000F61A5E41BA396ULL, // 第十一个双精度浮点数的位模式
0x000F707A755396A4ULL, // 第十二个双精度浮点数的位模式
0x000F7CB2EC28449AULL, // 第十三个双精度浮点数的位模式
0x000F86F10C6357D3ULL, // 第十四个双精度浮点数的位模式
0x000F8FA6578325DEULL, // 第十五个双精度浮点数的位模式
0x000F9724C74DD0DAULL, // 第十六个双精度浮点数的位模式
0x000F9DA907DBF509ULL, // 第十七个双精度浮点数的位模式
0x000FA360F581FA74ULL, // 第十八个双精度浮点数的位模式
0x000FA86FDE5B4BF8ULL, // 第十九个双精度浮点数的位模式
0x000FACF160D354DCULL, // 第二十个双精度浮点数的位模式
0x000FB0FB6718B90FULL, // 第二十一个双精度浮点数的位模式
0x000FB49F8D5374C6ULL, // 第二十二个双精度浮点数的位模式
0x000FB7EC2366FE77ULL, // 第二十三个双精度浮点数的位模式
0x000FBAECE9A1E50EULL, // 第二十四个双精度浮点数的位模式
0x000FBDAB9D040BEDULL, // 第二十五个双精度浮点数的位模式
0x000FC03060FF6C57ULL, // 第二十六个双精度浮点数的位模式
0x000FC2821037A248ULL, // 第二十七个双精度浮点数的位模式
0x000FC4A67AE25BD1ULL, // 第二十八个双精度浮点数的位模式
0x000FC6A2977AEE31ULL, // 第二十九个双精度浮点数的位模式
0x000FC87AA92896A4ULL, // 第三十个双精度浮点数的位模式
0x000FCA325E4BDE85ULL, // 第三十一个双精度浮点数的位模式
0x000FCBCCE902231AULL, // 第三十二个双精度浮点数的位模式
0x000FCD4D12F839C4ULL, // 第三十三个双精度浮点数的位模式
0x000FCEB54D8FEC99ULL, // 第三十四个双精度浮点数的位模式
0x000FD007BF1DC930ULL, // 第三十五个双精度浮点数的位模式
0x000FD1464DD6C4E6ULL, // 第三十六个双精度浮点数的位模式
0x000FD272A8E2F450ULL, // 第三十七个双精度浮点数的位模式
0x000FD38E4FF0C91EULL, // 第三十八个双精度浮点数的位模式
0x000FD49A9990B478ULL, // 第三十九个双精度浮点数的位模式
0x000FD598B8920F53ULL, // 第四十个双精度浮点数的位模式
0x000FD689C08E99ECULL, // 第四十一个双精度浮点数的位模式
0x000FD76EA9C8E832ULL, // 第四十二个双精度浮点数的位模式
0x000FD848547B08E8ULL, // 第四十三个双精度浮点数的位模式
0x000FD9178BAD2C8CULL, // 第四十四个双精度浮点数的位模式
0x000FD9DD07A7ADD2ULL, // 第四十五个双精度浮点数的位模式
0x000FDA9970105E8CULL, // 第四十六个双精度浮点数的位模式
0x000FDB4D5DC02E20ULL, // 第四十七个双精度浮点数的位模式
0x000FDBF95C5BFCD0ULL, // 第四十八个双精度浮点数的位模式
0x000FDC9DEBB99A7DULL, // 第四十九个双精度浮点数的位模式
0x000FDD3B8118729DULL, // 第五十个双精度浮点数的位模式
0x000FDDD288342F90ULL, // 第五十一个双精度浮点数的位模式
0x000FDE6364369F64ULL, // 第五十二个双精度浮点数的位模式
0x000FDEEE708D514EULL, // 第五十三个双精度浮点数的位模式
0x000FDF7401A6B42EULL, // 第五十四个双精度浮点数的位模式
0x000FDFF46599ED40ULL, // 第五十五个双精度浮点数的位模式
0x000FE06FE4BC24F2ULL, // 第五十六个双精度浮点数
# 这是一个长整数的列表,使用十六进制表示
0x000FED721D414FE8ULL, # 第一个长整数
0x000FED8357E4A982ULL, # 第二个长整数
0x000FED9406A42CC8ULL, # 第三个长整数
0x000FEDA42B85B704ULL, # 第四个长整数
0x000FEDB3C8746AB4ULL, # 第五个长整数
0x000FEDC2DF416652ULL, # 第六个长整数
0x000FEDD171A46E52ULL, # 第七个长整数
0x000FEDDF813C8AD3ULL, # 第八个长整数
0x000FEDED0F909980ULL, # 第九个长整数
0x000FEDFA1E0FD414ULL, # 第十个长整数
0x000FEE06AE124BC4ULL, # 第十一个长整数
0x000FEE12C0D95A06ULL, # 第十二个长整数
0x000FEE1E579006E0ULL, # 第十三个长整数
0x000FEE29734B6524ULL, # 第十四个长整数
0x000FEE34150AE4BCULL, # 第十五个长整数
0x000FEE3E3DB89B3CULL, # 第十六个长整数
0x000FEE47EE2982F4ULL, # 第十七个长整数
0x000FEE51271DB086ULL, # 第十八个长整数
0x000FEE59E9407F41ULL, # 第十九个长整数
0x000FEE623528B42EULL, # 第二十个长整数
0x000FEE6A0B5897F1ULL, # 第二十一个长整数
0x000FEE716C3E077AULL, # 第二十二个长整数
0x000FEE7858327B82ULL, # 第二十三个长整数
0x000FEE7ECF7B06BAULL, # 第二十四个长整数
0x000FEE84D2484AB2ULL, # 第二十五个长整数
0x000FEE8A60B66343ULL, # 第二十六个长整数
0x000FEE8F7ACCC851ULL, # 第二十七个长整数
0x000FEE94207E25DAULL, # 第二十八个长整数
0x000FEE9851A829EAULL, # 第二十九个长整数
0x000FEE9C0E13485CULL, # 第三十个长整数
0x000FEE9F557273F4ULL, # 第三十一个长整数
0x000FEEA22762CCAEULL, # 第三十二个长整数
0x000FEEA4836B42ACULL, # 第三十三个长整数
0x000FEEA668FC2D71ULL, # 第三十四个长整数
0x000FEEA7D76ED6FAULL, # 第三十五个长整数
0x000FEEA8CE04FA0AULL, # 第三十六个长整数
0x000FEEA94BE8333BULL, # 第三十七个长整数
0x000FEEA950296410ULL, # 第三十八个长整数
0x000FEEA8D9C0075EULL, # 第三十九个长整数
0x000FEEA7E7897654ULL, # 第四十个长整数
0x000FEEA678481D24ULL, # 第四十一个长整数
0x000FEEA48AA29E83ULL, # 第四十二个长整数
0x000FEEA21D22E4DAULL, # 第四十三个长整数
0x000FEE9F2E352024ULL, # 第四十四个长整数
0x000FEE9BBC26AF2EULL, # 第四十五个长整数
0x000FEE97C524F2E4ULL, # 第四十六个长整数
0x000FEE93473C0A3AULL, # 第四十七个长整数
0x000FEE8E40557516ULL, # 第四十八个长整数
0x000FEE88AE369C7AULL, # 第四十九个长整数
0x000FEE828E7F3DFDULL, # 第五十个长整数
0x000FEE7BDEA7B888ULL, # 第五十一个长整数
0x000FEE749BFF37FFULL, # 第五十二个长整数
0x000FEE6CC3A9BD5EULL, # 第五十三个长整数
0x000FEE64529E007EULL, # 第五十四个长整数
0x000FEE5B45A32888ULL, # 第五十五个长整数
0x000FEE51994E57B6ULL, # 第五十六个长整数
0x000FEE474A0006CFULL, # 第五十七个长整数
0x000FEE3C53E12C50ULL, # 第五十八个长整数
0x000FEE30B2E02AD8ULL, # 第五十九个长整数
0x000FEE2462AD8205ULL, # 第六十个长整数
0x000FEE175EB83C5AULL, # 第六十一个长整数
0x000FEE09A22A1447ULL, # 第六十二个长整数
0x000FEDFB27E349CCULL, # 第六十三个长整数
0x000FEDEBEA76216CULL, # 第六十四个长整数
0x000FEDDBE422047EULL, # 第六十五个长整数
0x000FEDCB0ECE39D3ULL, # 第六十六个长整数
0x000FEDB964042CF4ULL, # 第六十七个长整数
0x000FEDA6DCE938C9ULL, # 第六十八个长整数
0x000FED937237E98DULL, # 第六十九个长整数
0x000FED7F1C38A836ULL, # 第七十个长整数
0x000FED69D2B9C02BULL, # 第七十一个长整数
0x000FED538D06AE00ULL, # 第七十二个长整数
0x000FED3C41DEA422ULL, # 第七十三个长整数
0x000FED23E76A2FD8ULL, # 第七十四个长整数
0x000FED0A732FE644ULL, # 第七十五个长整数
0x000FECEFDA07FE34ULL, # 第七十六个长整数
0x000FECD4100EB7B8ULL, # 第七十七个长整数
0x000FECB708956EB4ULL, # 第七十八个长整数
0x000FEC98B61230C1ULL, # 第七十九个长整数
0x000FEC790A0DA978ULL, # 第八十个长整数
0x000FEC57F50F31FEULL, # 第八十一个长整数
0x000FEC356686C962ULL, # 第八十二个长整数
0x000FEC114CB4B335ULL, # 第八十三个长整数
0x000FEBEB948E6FD0ULL, # 第八十四个长整数
0x000FEBC429A0
# 一系列十六进制数值,看起来像是一个长数组或者常量列表的一部分
0x000FD40149E2F012ULL, 0x000FD1A1A7B4C7ACULL, 0x000FCEE204761F9EULL,
0x000FCBA8D85E11B2ULL, 0x000FC7D26ECD2D22ULL, 0x000FC32B2F1E22EDULL,
0x000FBD6581C0B83AULL, 0x000FB606C4005434ULL, 0x000FAC40582A2874ULL,
0x000F9E971E014598ULL, 0x000F89FA48A41DFCULL, 0x000F66C5F7F0302CULL,
0x000F1A5A4B331C4AULL};
static const double wi_double[] = {
8.68362706080130616677e-16, // 第一个双精度浮点数
4.77933017572773682428e-17, // 第二个双精度浮点数
6.35435241740526230246e-17, // 第三个双精度浮点数
7.45487048124769627714e-17, // 第四个双精度浮点数
8.32936681579309972857e-17, // 第五个双精度浮点数
9.06806040505948228243e-17, // 第六个双精度浮点数
9.71486007656776183958e-17, // 第七个双精度浮点数
1.02947503142410192108e-16, // 第八个双精度浮点数
1.08234302884476839838e-16, // 第九个双精度浮点数
1.13114701961090307945e-16, // 第十个双精度浮点数
1.17663594570229211411e-16, // 第十一个双精度浮点数
1.21936172787143633280e-16, // 第十二个双精度浮点数
1.25974399146370927864e-16, // 第十三个双精度浮点数
1.29810998862640315416e-16, // 第十四个双精度浮点数
1.33472037368241227547e-16, // 第十五个双精度浮点数
1.36978648425712032797e-16, // 第十六个双精度浮点数
1.40348230012423820659e-16, // 第十七个双精度浮点数
1.43595294520569430270e-16, // 第十八个双精度浮点数
1.46732087423644219083e-16, // 第十九个双精度浮点数
1.49769046683910367425e-16, // 第二十个双精度浮点数
1.52715150035961979750e-16, // 第二十一个双精度浮点数
1.55578181694607639484e-16, // 第二十二个双精度浮点数
1.58364940092908853989e-16, // 第二十三个双精度浮点数
1.61081401752749279325e-16, // 第二十四个双精度浮点数
1.63732852039698532012e-16, // 第二十五个双精度浮点数
1.66323990584208352778e-16, // 第二十六个双精度浮点数
1.68859017086765964015e-16, // 第二十七个双精度浮点数
1.71341701765596607184e-16, // 第二十八个双精度浮点数
1.73775443658648593310e-16, // 第二十九个双精度浮点数
1.76163319230009959832e-16, // 第三十个双精度浮点数
1.78508123169767272927e-16, // 第三十一个双精度浮点数
1.80812402857991522674e-16, // 第三十二个双精度浮点数
1.83078487648267501776e-16, // 第三十三个双精度浮点数
1.85308513886180189386e-16, // 第三十四个双精度浮点数
1.87504446393738816849e-16, // 第三十五个双精度浮点数
1.89668097007747596212e-16, // 第三十六个双精度浮点数
1.91801140648386198029e-16, // 第三十七个双精度浮点数
1.93905129306251037069e-16, // 第三十八个双精度浮点数
1.95981504266288244037e-16, // 第三十九个双精度浮点数
1.98031606831281739736e-16, // 第四十个双精度浮点数
2.00056687762733300198e-16, // 第四十一个双精度浮点数
2.02057915620716538808e-16, // 第四十二个双精度浮点数
2.04036384154802118313e-16, // 第四十三个双精度浮点数
2.05993118874037063144e-16, // 第四十四个双精度浮点数
2.07929082904140197311e-16, // 第四十五个双精度浮点数
2.09845182223703516690e-16, // 第四十六个双精度浮点数
2.11742270357603418769e-16, // 第四十七个双精度浮点数
2.13621152594498681022e-16, // 第四十八个双精度浮点数
2.15482589785814580926e-16, // 第四十九个双精度浮点数
2.17327301775643674990e-16, // 第五十个双精度浮点数
2.19155970504272708519e-16, // 第五十一个双精度浮点数
2.20969242822353175995e-16, // 第五十二个双精度浮点数
2.22767733047895534948e-16, // 第五十三个双精度浮点数
2.24552025294143552381e-16, // 第五十四个双精度浮点数
2.26322675592856786566e-16, // 第五十五个双精度浮点数
2.28080213834501706782e-16, // 第五十六个双精度浮点数
2.29825145544246839061e-16, // 第五十七个双精度浮点数
2.31557953510408037008e-16, // 第五十八个双精度浮点数
2.33279099280043561128e-16, // 第五十九个双精度浮点数
2.34989024534709550938e-16, // 第六十个双精度浮点数
2.36688152357916037468e-16, // 第六十一个双精度浮点数
2.38376888404542434981e-16, // 第六十二个双精度浮点数
2.40055621981350627349e-16, // 第六十三个双精度浮点数
2.41724727046750252175e-16, // 第六十四个双精度浮点数
# 下面是一系列浮点数,以科学计数法表示,作为数据的一部分
2.96229297362806647792e-16, 2.97721928420902891115e-16,
2.99213070138601307081e-16, 3.00702866332133102993e-16,
3.02191459196806151971e-16, 3.03678989421180184427e-16,
3.05165596297821922381e-16, 3.06651417830895451744e-16,
3.08136590840829717032e-16, 3.09621251066292253306e-16,
3.11105533263689296831e-16, 3.12589571304399892784e-16,
3.14073498269944617203e-16, 3.15557446545280064031e-16,
3.17041547910402852545e-16, 3.18525933630440648871e-16,
3.20010734544401137886e-16, 3.21496081152744704901e-16,
3.22982103703941557538e-16, 3.24468932280169778077e-16,
3.25956696882307838340e-16, 3.27445527514370671802e-16,
3.28935554267536967851e-16, 3.30426907403912838589e-16,
3.31919717440175233652e-16, 3.33414115231237245918e-16,
3.34910232054077845412e-16, 3.36408199691876507948e-16,
3.37908150518594979994e-16, 3.39410217584148914282e-16,
3.40914534700312603713e-16, 3.42421236527501816058e-16,
3.43930458662583133920e-16, 3.45442337727858401604e-16,
3.46957011461378353333e-16, 3.48474618808741370700e-16,
3.49995300016538099813e-16, 3.51519196727607440975e-16,
3.53046452078274009054e-16, 3.54577210797743572160e-16,
3.56111619309838843415e-16, 3.57649825837265051035e-16,
3.59191980508602994994e-16, 3.60738235468235137839e-16,
3.62288744989419151904e-16, 3.63843665590734438546e-16,
3.65403156156136995766e-16, 3.66967378058870090021e-16,
3.68536495289491401456e-16, 3.70110674588289834952e-16,
3.71690085582382297792e-16, 3.73274900927794352614e-16,
3.74865296456848868882e-16, 3.76461451331202869131e-16,
3.78063548200896037651e-16, 3.79671773369794425924e-16,
3.81286316967837738238e-16, 3.82907373130524317507e-16,
3.84535140186095955858e-16, 3.86169820850914927119e-16,
3.87811622433558721164e-16, 3.89460757048192620674e-16,
3.91117441837820542060e-16, 3.92781899208054153270e-16,
3.94454357072087711446e-16, 3.96135049107613542983e-16,
3.97824215026468259474e-16, 3.99522100857856502444e-16,
4.01228959246062907451e-16, 4.02945049763632792393e-16,
4.04670639241074995115e-16, 4.06406002114225038723e-16,
4.08151420790493873480e-16, 4.09907186035326643447e-16,
4.11673597380302570170e-16, 4.13450963554423599878e-16,
4.15239602940268833891e-16, 4.17039844056831587498e-16,
4.18852026071011229572e-16, 4.20676499339901510978e-16,
4.22513625986204937320e-16, 4.24363780509307796137e-16,
4.26227350434779809917e-16, 4.28104737005311666397e-16,
4.29996355916383230161e-16, 4.31902638100262944617e-16,
4.33824030562279080411e-16, 4.35760997273684900553e-16,
4.37714020125858747008e-16, 4.39683599951052137423e-16,
4.41670257615420348435e-16, 4.43674535190656726604e-16,
4.45696997211204306674e-16, 4.47738232024753387312e-16,
4.49798853244554968009e-16, 4.51879501313005876278e-16,
4.53980845187003400947e-16, 4.56103584156742206384e-16,
4.58248449810956667052e-16, 4.60416208163115281428e-16,
# 定义一个包含64个浮点数的数组
array = {
4.62607661954784567754e-16, 4.64823653154320737780e-16,
4.67065065671263059081e-16, 4.69332828309332890697e-16,
4.71627917983835129766e-16, 4.73951363232586715165e-16,
4.76304248053313737663e-16, 4.78687716104872284247e-16,
4.81102975314741720538e-16, 4.83551302941152515162e-16,
4.86034051145081195402e-16, 4.88552653135360343280e-16,
4.91108629959526955862e-16, 4.93703598024033454728e-16,
4.96339277440398725619e-16, 4.99017501309182245754e-16,
5.01740226071808946011e-16, 5.04509543081872748637e-16,
5.07327691573354207058e-16, 5.10197073234156184149e-16,
5.13120268630678373200e-16, 5.16100055774322824569e-16,
5.19139431175769859873e-16, 5.22241633800023428760e-16,
5.25410172417759732697e-16, 5.28648856950494511482e-16,
5.31961834533840037535e-16, 5.35353631181649688145e-16,
5.38829200133405320160e-16, 5.42393978220171234073e-16,
5.46053951907478041166e-16, 5.49815735089281410703e-16,
5.53686661246787600374e-16, 5.57674893292657647836e-16,
5.61789555355541665830e-16, 5.66040892008242216739e-16,
5.70440462129138908417e-16, 5.75001376891989523684e-16,
5.79738594572459365014e-16, 5.84669289345547900201e-16,
5.89813317647789942685e-16, 5.95193814964144415532e-16,
6.00837969627190832234e-16, 6.06778040933344851394e-16,
6.13052720872528159123e-16, 6.19708989458162555387e-16,
6.26804696330128439415e-16, 6.34412240712750598627e-16,
6.42623965954805540945e-16, 6.51560331734499356881e-16,
6.61382788509766415145e-16, 6.72315046250558662913e-16,
6.84680341756425875856e-16, 6.98971833638761995415e-16,
7.15999493483066421560e-16, 7.37242430179879890722e-16,
7.65893637080557275482e-16, 8.11384933765648418565e-16}
static const double fi_double[] = {
// 定义一个静态常量双精度浮点数组,存储一系列浮点数
1.00000000000000000000e+00, // 第一个元素
9.77101701267671596263e-01, // 第二个元素
9.59879091800106665211e-01, // 第三个元素
9.45198953442299649730e-01, // 第四个元素
9.32060075959230460718e-01, // 第五个元素
9.19991505039347012840e-01, // 第六个元素
9.08726440052130879366e-01, // 第七个元素
8.98095921898343418910e-01, // 第八个元素
8.87984660755833377088e-01, // 第九个元素
8.78309655808917399966e-01, // 第十个元素
8.69008688036857046555e-01, // 第十一个元素
8.60033621196331532488e-01, // 第十二个元素
8.51346258458677951353e-01, // 第十三个元素
8.42915653112204177333e-01, // 第十四个元素
8.34716292986883434679e-01, // 第十五个元素
8.26726833946221373317e-01, // 第十六个元素
8.18929191603702366642e-01, // 第十七个元素
8.11307874312656274185e-01, // 第十八个元素
8.03849483170964274059e-01, // 第十九个元素
7.96542330422958966274e-01, // 第二十个元素
7.89376143566024590648e-01, // 第二十一个元素
7.82341832654802504798e-01, // 第二十二个元素
7.75431304981187174974e-01, // 第二十三个元素
7.68637315798486264740e-01, // 第二十四个元素
7.61953346836795386565e-01, // 第二十五个元素
7.55373506507096115214e-01, // 第二十六个元素
7.48892447219156820459e-01, // 第二十七个元素
7.42505296340151055290e-01, // 第二十八个元素
7.36207598126862650112e-01, // 第二十九个元素
7.29995264561476231435e-01, // 第三十个元素
7.23864533468630222401e-01, // 第三十一个元素
7.17811932630721960535e-01, // 第三十二个元素
7.11834248878248421200e-01, // 第三十三个元素
7.05928501332754310127e-01, // 第三十四个元素
7.00091918136511615067e-01, // 第三十五个元素
6.94321916126116711609e-01, // 第三十六个元素
6.88616083004671808432e-01, // 第三十七个元素
6.82972161644994857355e-01, // 第三十八个元素
6.77388036218773526009e-01, // 第三十九个元素
6.71861719897082099173e-01, // 第四十个元素
6.66391343908750100056e-01, // 第四十一个元素
6.60975147776663107813e-01, // 第四十二个元素
6.55611470579697264149e-01, // 第四十三个元素
6.50298743110816701574e-01, // 第四十四个元素
6.45035480820822293424e-01, // 第四十五个元素
6.39820277453056585060e-01, // 第四十六个元素
6.34651799287623608059e-01, // 第四十七个元素
6.29528779924836690007e-01, // 第四十八个元素
6.24450015547026504592e-01, // 第四十九个元素
6.19414360605834324325e-01, // 第五十个元素
6.14420723888913888899e-01, // 第五十一个元素
6.09468064925773433949e-01, // 第五十二个元素
6.04555390697467776029e-01, // 第五十三个元素
5.99681752619125263415e-01, // 第五十四个元素
5.94846243767987448159e-01, // 第五十五个元素
5.90047996332826008015e-01, // 第五十六个元素
5.85286179263371453274e-01, // 第五十七个元素
5.80559996100790898232e-01, // 第五十八个元素
5.75868682972353718164e-01, // 第五十九个元素
5.71211506735253227163e-01, // 第六十个元素
5.66587763256164445025e-01, // 第六十一个元素
5.61996775814524340831e-01, // 第六十二个元素
5.57437893618765945014e-01, // 第六十三个元素
5.52910490425832290562e-01, // 第六十四个元素
5.48413963255265812791e-01, // 第六十五个元素
5.43947731190026262382e-01, // 第六十六个元素
5.39511234256952132426e-01, // 第六十七个元素
5.35103932380457614215e-01, // 第六十八个元素
5.30725304403662057062e-01, // 第六十九个元素
5.26374847171684479008e-01, // 第七十个元素
5.22052074672321841931e-01, // 第七十一个元素
5.17756517229756352272e-01, // 第七十二个元素
5.13487720747326958914e-01, // 第七十三个元素
5.09245245995747941592e-01, // 第七十四个元素
5.05028667943468123624e-01, // 第七十五个元素
5.00837575126148681903e-01, // 第七十六个元素
4.96671569052489714213e-01, // 第七十七个元素
4.92530263643868537748e-01, // 第七十八个元素
4.88413284705458028423e-01, // 第七十九个元素
4.84320269426683325253e-01, // 第八十个元素
4.80250865909046753544e-01, // 第八十一个元素
4.76204732719505863248e-01, // 第八十二个元素
4.72181538467730199660e-01, // 第八十三个元素
# 下面是一系列浮点数,格式为科学计数法,表示连续的数值
4.10693148270188157500e-01, 4.07017284329473372217e-01,
4.03359739221114510510e-01, 3.99720314980197222177e-01,
3.96098818515832451492e-01, 3.92495061459315619512e-01,
3.88908860018788715696e-01, 3.85340034840077283462e-01,
3.81788410873393657674e-01, 3.78253817245619183840e-01,
3.74736087137891138443e-01, 3.71235057668239498696e-01,
3.67750569779032587814e-01, 3.64282468129004055601e-01,
3.60830600989648031529e-01, 3.57394820145780500731e-01,
3.53974980800076777232e-01, 3.50570941481406106455e-01,
3.47182563956793643900e-01, 3.43809713146850715049e-01,
3.40452257044521866547e-01, 3.37110066637006045021e-01,
3.33783015830718454708e-01, 3.30470981379163586400e-01,
3.27173842813601400970e-01, 3.23891482376391093290e-01,
3.20623784956905355514e-01, 3.17370638029913609834e-01,
3.14131931596337177215e-01, 3.10907558126286509559e-01,
3.07697412504292056035e-01, 3.04501391976649993243e-01,
3.01319396100803049698e-01, 2.98151326696685481377e-01,
2.94997087799961810184e-01, 2.91856585617095209972e-01,
2.88729728482182923521e-01, 2.85616426815501756042e-01,
2.82516593083707578948e-01, 2.79430141761637940157e-01,
2.76356989295668320494e-01, 2.73297054068577072172e-01,
2.70250256365875463072e-01, 2.67216518343561471038e-01,
2.64195763997261190426e-01, 2.61187919132721213522e-01,
2.58192911337619235290e-01, 2.55210669954661961700e-01,
2.52241126055942177508e-01, 2.49284212418528522415e-01,
2.46339863501263828249e-01, 2.43408015422750312329e-01,
2.40488605940500588254e-01, 2.37581574431238090606e-01,
2.34686861872330010392e-01, 2.31804410824338724684e-01,
2.28934165414680340644e-01, 2.26076071322380278694e-01,
2.23230075763917484855e-01, 2.20396127480151998723e-01,
2.17574176724331130872e-01, 2.14764175251173583536e-01,
2.11966076307030182324e-01, 2.09179834621125076977e-01,
2.06405406397880797353e-01, 2.03642749310334908452e-01,
2.00891822494656591136e-01, 1.98152586545775138971e-01,
1.95425003514134304483e-01, 1.92709036903589175926e-01,
1.90004651670464985713e-01, 1.87311814223800304768e-01,
1.84630492426799269756e-01, 1.81960655599522513892e-01,
1.79302274522847582272e-01, 1.76655321443734858455e-01,
1.74019770081838553999e-01, 1.71395595637505754327e-01,
1.68782774801211288285e-01, 1.66181285764481906364e-01,
1.63591108232365584074e-01, 1.61012223437511009516e-01,
1.58444614155924284882e-01, 1.55888264724479197465e-01,
1.53343161060262855866e-01, 1.50809290681845675763e-01,
1.48286642732574552861e-01, 1.45775208005994028060e-01,
1.43274978973513461566e-01, 1.40785949814444699690e-01,
1.38308116448550733057e-01, 1.35841476571253755301e-01,
1.33386029691669155683e-01, 1.30941777173644358090e-01,
1.28508722279999570981e-01, 1.26086870220185887081e-01,
1.23676228201596571932e-01, 1.21276805484790306533e-01,
1.18888613442910059947e-01, 1.16511665625610869035e-01,
# 给定一串数字,看似是一个浮点数数组的初始化,但缺少对应的变量名和其他代码上下文,难以准确注释其具体作用和含义。
1.14145977827838487895e-01, 1.11791568163838089811e-01,
1.09448457146811797824e-01, 1.07116667774683801961e-01,
1.04796225622487068629e-01, 1.02487158941935246892e-01,
1.00189498768810017482e-01, 9.79032790388624646338e-02,
9.56285367130089991594e-02, 9.33653119126910124859e-02,
9.11136480663737591268e-02, 8.88735920682758862021e-02,
8.66451944505580717859e-02, 8.44285095703534715916e-02,
8.22235958132029043366e-02, 8.00305158146630696292e-02,
7.78493367020961224423e-02, 7.56801303589271778804e-02,
7.35229737139813238622e-02, 7.13779490588904025339e-02,
6.92451443970067553879e-02, 6.71246538277884968737e-02,
6.50165779712428976156e-02, 6.29210244377581412456e-02,
6.08381083495398780614e-02, 5.87679529209337372930e-02,
5.67106901062029017391e-02, 5.46664613248889208474e-02,
5.26354182767921896513e-02, 5.06177238609477817000e-02,
4.86135532158685421122e-02, 4.66230949019303814174e-02,
4.46465522512944634759e-02, 4.26841449164744590750e-02,
4.07361106559409394401e-02, 3.88027074045261474722e-02,
3.68842156885673053135e-02, 3.49809414617161251737e-02,
3.30932194585785779961e-02, 3.12214171919203004046e-02,
2.93659397581333588001e-02, 2.75272356696031131329e-02,
2.57058040085489103443e-02, 2.39022033057958785407e-02,
2.21170627073088502113e-02, 2.03510962300445102935e-02,
1.86051212757246224594e-02, 1.68800831525431419000e-02,
1.51770883079353092332e-02, 1.34974506017398673818e-02,
1.18427578579078790488e-02, 1.02149714397014590439e-02,
8.61658276939872638800e-03, 7.05087547137322242369e-03,
5.52240329925099155545e-03, 4.03797259336302356153e-03,
2.60907274610215926189e-03, 1.26028593049859797236e-03};
# 上述代码片段看起来像是定义了一个浮点数数组,但是缺少了数组变量名以及数组的用途和上下文,因此无法确定其具体含义和作用。
// 定义一个静态常量数组 ki_float,存储了一系列无符号32位整数值,每个值都表示一个浮点数的位模式
static const uint32_t ki_float[] = {
0x007799ECUL, 0x00000000UL, 0x006045F5UL, 0x006D1AA8UL, 0x00728FB4UL,
0x007592AFUL, 0x00777A5CUL, 0x0078CA38UL, 0x0079BF6BUL, 0x007A7A35UL,
0x007B0D2FUL, 0x007B83D4UL, 0x007BE597UL, 0x007C3788UL, 0x007C7D33UL,
0x007CB926UL, 0x007CED48UL, 0x007D1B08UL, 0x007D437FUL, 0x007D678BUL,
0x007D87DBUL, 0x007DA4FCUL, 0x007DBF61UL, 0x007DD767UL, 0x007DED5DUL,
0x007E0183UL, 0x007E1411UL, 0x007E2534UL, 0x007E3515UL, 0x007E43D5UL,
0x007E5193UL, 0x007E5E67UL, 0x007E6A69UL, 0x007E75AAUL, 0x007E803EUL,
0x007E8A32UL, 0x007E9395UL, 0x007E9C72UL, 0x007EA4D5UL, 0x007EACC6UL,
0x007EB44EUL, 0x007EBB75UL, 0x007EC243UL, 0x007EC8BCUL, 0x007ECEE8UL,
0x007ED4CCUL, 0x007EDA6BUL, 0x007EDFCBUL, 0x007EE4EFUL, 0x007EE9DCUL,
0x007EEE94UL, 0x007EF31BUL, 0x007EF774UL, 0x007EFBA0UL, 0x007EFFA3UL,
0x007F037FUL, 0x007F0736UL, 0x007F0ACAUL, 0x007F0E3CUL, 0x007F118FUL,
0x007F14C4UL, 0x007F17DCUL, 0x007F1ADAUL, 0x007F1DBDUL, 0x007F2087UL,
0x007F233AUL, 0x007F25D7UL, 0x007F285DUL, 0x007F2AD0UL, 0x007F2D2EUL,
0x007F2F7AUL, 0x007F31B3UL, 0x007F33DCUL, 0x007F35F3UL, 0x007F37FBUL,
0x007F39F3UL, 0x007F3BDCUL, 0x007F3DB7UL, 0x007F3F84UL, 0x007F4145UL,
0x007F42F8UL, 0x007F449FUL, 0x007F463AUL, 0x007F47CAUL, 0x007F494EUL,
0x007F4AC8UL, 0x007F4C38UL, 0x007F4D9DUL, 0x007F4EF9UL, 0x007F504CUL,
0x007F5195UL, 0x007F52D5UL, 0x007F540DUL, 0x007F553DUL, 0x007F5664UL,
0x007F5784UL, 0x007F589CUL, 0x007F59ACUL, 0x007F5AB5UL, 0x007F5BB8UL,
0x007F5CB3UL, 0x007F5DA8UL, 0x007F5E96UL, 0x007F5F7EUL, 0x007F605FUL,
0x007F613BUL, 0x007F6210UL, 0x007F62E0UL, 0x007F63AAUL, 0x007F646FUL,
0x007F652EUL, 0x007F65E8UL, 0x007F669CUL, 0x007F674CUL, 0x007F67F6UL,
0x007F689CUL, 0x007F693CUL, 0x007F69D9UL, 0x007F6A70UL, 0x007F6B03UL,
0x007F6B91UL, 0x007F6C1BUL, 0x007F6CA0UL, 0x007F6D21UL, 0x007F6D9EUL,
0x007F6E17UL, 0x007F6E8CUL, 0x007F6EFCUL, 0x007F6F68UL, 0x007F6FD1UL,
0x007F7035UL, 0x007F7096UL, 0x007F70F3UL, 0x007F714CUL, 0x007F71A1UL,
0x007F71F2UL, 0x007F723FUL, 0x007F7289UL, 0x007F72CFUL, 0x007F7312UL,
0x007F7350UL, 0x007F738BUL, 0x007F73C3UL, 0x007F73F6UL, 0x007F7427UL,
0x007F7453UL, 0x007F747CUL, 0x007F74A1UL, 0x007F74C3UL, 0x007F74E0UL,
0x007F74FBUL, 0x007F7511UL, 0x007F7524UL, 0x007F7533UL, 0x007F753FUL,
0x007F7546UL, 0x007F754AUL, 0x007F754BUL, 0x007F7547UL, 0x007F753FUL,
0x007F7534UL, 0x007F7524UL, 0x007F7511UL, 0x007F74F9UL, 0x007F74DEUL,
0x007F74BEUL, 0x007F749AUL, 0x007F7472UL, 0x007F7445UL, 0x007F7414UL,
0x007F73DFUL, 0x007F73A5UL, 0x007F7366UL, 0x007F7323UL, 0x007F72DAUL,
0x007F728DUL, 0x007F723AUL, 0x007F71E3UL, 0x007F7186UL, 0x007F7123UL,
0x007F70BBUL, 0x007F704DUL, 0x007F6FD9UL, 0x007F6F5FUL, 0x007F6EDFUL,
0x007F6E58UL, 0x007F6DCBUL, 0x007F6D37UL, 0x007F6C9CUL, 0x007F6BF9UL,
0x007F6B4FUL, 0x007F6A9CUL, 0x007F69E2UL, 0x007F691FUL, 0x007F6854UL,
0x007F677FUL, 0x007F66A1UL, 0x007F65B8UL, 0x007F64C6UL, 0x007F63C8UL,
};
# 定义一个长整型数组,包含多个十六进制表示的无符号长整型数值
0x007F62C0UL, 0x007F61ABUL, 0x007F608AUL, 0x007F5F5DUL, 0x007F5E21UL,
0x007F5CD8UL, 0x007F5B7FUL, 0x007F5A17UL, 0x007F589EUL, 0x007F5713UL,
0x007F5575UL, 0x007F53C4UL, 0x007F51FEUL, 0x007F5022UL, 0x007F4E2FUL,
0x007F4C22UL, 0x007F49FAUL, 0x007F47B6UL, 0x007F4553UL, 0x007F42CFUL,
0x007F4028UL, 0x007F3D5AUL, 0x007F3A64UL, 0x007F3741UL, 0x007F33EDUL,
0x007F3065UL, 0x007F2CA4UL, 0x007F28A4UL, 0x007F245FUL, 0x007F1FCEUL,
0x007F1AEAUL, 0x007F15A9UL, 0x007F1000UL, 0x007F09E4UL, 0x007F0346UL,
0x007EFC16UL, 0x007EF43EUL, 0x007EEBA8UL, 0x007EE237UL, 0x007ED7C8UL,
0x007ECC2FUL, 0x007EBF37UL, 0x007EB09DUL, 0x007EA00AUL, 0x007E8D0DUL,
0x007E7710UL, 0x007E5D47UL, 0x007E3E93UL, 0x007E1959UL, 0x007DEB2CUL,
0x007DB036UL, 0x007D6203UL, 0x007CF4B9UL, 0x007C4FD2UL, 0x007B3630UL,
0x0078D2D2UL;
static const float wi_float[] = {
// 下面是一个静态常量数组,包含了一系列的浮点数值
4.66198677960027669255e-07f, // 第一个元素
2.56588335019207033255e-08f, // 第二个元素
3.41146697750176784592e-08f, // 第三个元素
4.00230311410932959821e-08f, // 第四个元素
4.47179475877737745459e-08f, // 第五个元素
4.86837785973537366722e-08f, // 第六个元素
5.21562578925932412861e-08f, // 第七个元素
5.52695199001886257153e-08f, // 第八个元素
5.81078488992733116465e-08f, // 第九个元素
6.07279932024587421409e-08f, // 第十个元素
6.31701613261172047795e-08f, // 第十一个元素
6.54639842900233842742e-08f, // 第十二个元素
6.76319905583641815324e-08f, // 第十三个元素
6.96917493470166688656e-08f, // 第十四个元素
7.16572544283857476692e-08f, // 第十五个元素
7.35398519048393832969e-08f, // 第十六个元素
7.53488822443557479279e-08f, // 第十七个元素
7.70921367281667127885e-08f, // 第十八个元素
7.87761895947956022626e-08f, // 第十九个元素
8.04066446825615346857e-08f, // 第二十个元素
8.19883218760237408659e-08f, // 第二十一个元素
8.35254002936857088917e-08f, // 第二十二个元素
8.50215298165053411740e-08f, // 第二十三个元素
8.64799190652369040985e-08f, // 第二十四个元素
8.79034055989140110861e-08f, // 第二十五个元素
8.92945125124233511541e-08f, // 第二十六个元素
9.06554945027956262312e-08f, // 第二十七个元素
9.19883756905278607229e-08f, // 第二十八个元素
9.32949809202232869780e-08f, // 第二十九个元素
9.45769618559625849039e-08f, // 第三十个元素
9.58358188855612866442e-08f, // 第三十一个元素
9.70729196232813152662e-08f, // 第三十二个元素
9.82895146313061088986e-08f, // 第三十三个元素
9.94867508514382224721e-08f, // 第三十四个元素
1.00665683139461669691e-07f, // 第三十五个元素
1.01827284217853923044e-07f, // 第三十六个元素
1.02972453302539369464e-07f, // 第三十七个元素
1.04102023612124921572e-07f, // 第三十八个元素
1.05216768930574060431e-07f, // 第三十九个元素
1.06317409364335657741e-07f, // 第四十个元素
1.07404616410877866490e-07f, // 第四十一个元素
1.08479017436113134283e-07f, // 第四十二个元素
1.09541199642370962438e-07f, // 第四十三个元素
1.10591713595628691212e-07f, // 第四十四个元素
1.11631076370069356306e-07f, // 第四十五个元素
1.12659774359245895023e-07f, // 第四十六个元素
1.13678265795837113569e-07f, // 第四十七个元素
1.14686983015899673063e-07f, // 第四十八个元素
1.15686334498432158725e-07f, // 第四十九个元素
1.16676706706789039179e-07f, // 第五十个元素
1.17658465754873988919e-07f, // 第五十一个元素
1.18631958917986203582e-07f, // 第五十二个元素
1.19597516005596215528e-07f, // 第五十三个元素
1.20555450611113917226e-07f, // 第五十四个元素
1.21506061251817163689e-07f, // 第五十五个元素
1.22449632410483948386e-07f, // 第五十六个元素
1.23386435488872536840e-07f, // 第五十七个元素
1.24316729681986364321e-07f, // 第五十八个元素
1.25240762781015530062e-07f, // 第五十九个元素
1.26158771911939892267e-07f, // 第六十个元素
1.27070984215989333455e-07f, // 第六十一个元素
1.27977617477468922011e-07f, // 第六十二个元素
1.28878880703854958297e-07f, // 第六十三个元素
1.29774974662539874521e-07f, // 第六十四个元素
1.30666092378141980504e-07f, // 第六十五个元素
1.31552419593887221722e-07f, // 第六十六个元素
1.32434135200211397569e-07f, // 第六十七个元素
1.33311411633413359243e-07f, // 第六十八个元素
1.34184415246907777059e-07f, // 第六十九个元素
1.35053306657377859830e-07f, // 第七十个元素
1.35918241067904315860e-07f, // 第七十一个元素
1.36779368569952053923e-07f, // 第七十二个元素
1.37636834425917531047e-07f, // 第七十三个元素
1.38490779333783508675e-07f, // 第七十四个元素
1.39341339675287344817e-07f, // 第七十五个元素
1.40188647748881762555e-07f, // 第七十六个元素
1.41032831988654882776e-07f, // 第七十七个元素
1
# 下面是一系列浮点数,可能是某种数据的系数或者参数
1.55821918133584372604e-07f, 1.56627258437898192833e-07f,
1.57431480314857468671e-07f, 1.58234665111056041043e-07f,
1.59036893036289199880e-07f, 1.59838243233728855017e-07f,
1.60638793847630850137e-07f, 1.61438622088746393909e-07f,
1.62237804297600106296e-07f, 1.63036416005787357730e-07f,
1.63834531995435479082e-07f, 1.64632226356965902954e-07f,
1.65429572545287097020e-07f, 1.66226643434541294491e-07f,
1.67023511371523209274e-07f, 1.67820248227882200051e-07f,
1.68616925451215588827e-07f, 1.69413614115155757272e-07f,
1.70210384968549673733e-07f, 1.71007308483826142122e-07f,
1.71804454904642543391e-07f, 1.72601894292900061024e-07f,
1.73399696575213681990e-07f, 1.74197931588920988271e-07f,
1.74996669127712165834e-07f, 1.75795978986961275677e-07f,
1.76595931008838063924e-07f, 1.77396595127278238022e-07f,
1.78198041412889183130e-07f, 1.79000340117867431104e-07f,
1.79803561721004406185e-07f, 1.80607776972855859813e-07f,
1.81413056941151359868e-07f, 1.82219473056520464354e-07f,
1.83027097158612474240e-07f, 1.83836001542687613069e-07f,
1.84646259006759307383e-07f, 1.85457942899367347876e-07f,
1.86271127168064649331e-07f, 1.87085886408701333260e-07f,
1.87902295915592424729e-07f, 1.88720431732658022414e-07f,
1.89540370705627262627e-07f, 1.90362190535400839128e-07f,
1.91185969832669990437e-07f, 1.92011788173893651535e-07f,
1.92839726158739913768e-07f, 1.93669865469102145482e-07f,
1.94502288929804890433e-07f, 1.95337080571120616772e-07f,
1.96174325693223683314e-07f, 1.97014110932714374919e-07f,
1.97856524331352952716e-07f, 1.98701655407150388211e-07f,
1.99549595227971635348e-07f, 2.00400436487814600236e-07f,
2.01254273585938820883e-07f, 2.02111202709026498408e-07f,
2.02971321916571014951e-07f, 2.03834731229698846698e-07f,
2.04701532723644121196e-07f, 2.05571830624108885378e-07f,
2.06445731407757185541e-07f, 2.07323343907107312957e-07f,
2.08204779420104330037e-07f, 2.09090151824673600213e-07f,
2.09979577698577670508e-07f, 2.10873176444920111011e-07f,
2.11771070423665379388e-07f, 2.12673385089569268965e-07f,
2.13580249136944118603e-07f, 2.14491794651713402832e-07f,
2.15408157271244625533e-07f, 2.16329476352486921685e-07f,
2.17255895148978920488e-07f, 2.18187560997337924713e-07f,
2.19124625513888206785e-07f, 2.20067244802139479285e-07f,
2.21015579671883851683e-07f, 2.21969795870742159701e-07f,
2.22930064329060010376e-07f, 2.23896561419128954210e-07f,
2.24869469229791575583e-07f, 2.25848975857580322189e-07f,
2.26835275715640744118e-07f, 2.27828569861799901001e-07f,
2.28829066347263833069e-07f, 2.29836980587561823183e-07f,
2.30852535757505260518e-07f, 2.31875963212094114516e-07f,
2.32907502935486642699e-07f, 2.33947404020352726160e-07f,
2.34995925180156140289e-07f, 2.36053335297164516378e-07f,
2.37119914009265667728e-07f, 2.38195952338983970691e-07f,
# 以下是一个包含多个浮点数的数组
2.39281753368440712742e-07f, 2.40377632964396957621e-07f,
2.41483920557958384709e-07f, 2.42600959984018662258e-07f,
2.43729110386077326413e-07f, 2.44868747192698939290e-07f,
2.46020263172594533433e-07f, 2.47184069576113545901e-07f,
2.48360597371852893654e-07f, 2.49550298588131851232e-07f,
2.50753647770270890721e-07f, 2.51971143565970967140e-07f,
2.53203310452642767375e-07f, 2.54450700622322097890e-07f,
2.55713896041856770961e-07f, 2.56993510708419870887e-07f,
2.58290193123138874550e-07f, 2.59604629008804833146e-07f,
2.60937544301314385690e-07f, 2.62289708448800566945e-07f,
2.63661938057441759882e-07f, 2.65055100928844238758e-07f,
2.66470120540847889467e-07f, 2.67907981031821866252e-07f,
2.69369732758258246335e-07f, 2.70856498507068313229e-07f,
2.72369480457841388042e-07f, 2.73909968006952220135e-07f,
2.75479346585437289399e-07f, 2.77079107626811561009e-07f,
2.78710859870496796972e-07f, 2.80376342222588603820e-07f,
2.82077438439999912690e-07f, 2.83816193958769527230e-07f,
2.85594835255375795814e-07f, 2.87415792215003905739e-07f,
2.89281724087851835900e-07f, 2.91195549750371467233e-07f,
2.93160483161771875581e-07f, 2.95180075129332912389e-07f,
2.97258262785797916083e-07f, 2.99399428561531794298e-07f,
3.01608470935804138388e-07f, 3.03890889921758510417e-07f,
3.06252891144972267537e-07f, 3.08701513613258141075e-07f,
3.11244787989714509378e-07f, 3.13891934589336184321e-07f,
3.16653613755314681314e-07f, 3.19542246256559459667e-07f,
3.22572428717978242099e-07f, 3.25761480217458181578e-07f,
3.29130173358915628534e-07f, 3.32703730345002116955e-07f,
3.36513208964639108346e-07f, 3.40597478255417943913e-07f,
3.45006114675213401550e-07f, 3.49803789521323211592e-07f,
3.55077180848341416206e-07f, 3.60946392031859609868e-07f,
3.67584959507244041831e-07f, 3.75257645787954431030e-07f,
3.84399301057791926300e-07f, 3.95804015855768440983e-07f,
4.11186015434435801956e-07f, 4.35608969373823260746e-07f
# 这是一个包含一系列特定精度的浮点数的数组,精度为 e-07f
// 定义静态常量数组,包含一系列单精度浮点数
static const float fi_float[] = {
1.00000000000000000000e+00f, // 第一个元素,值为 1.0
9.77101701267671596263e-01f, // 第二个元素,接近于 0.977
9.59879091800106665211e-01f, // 第三个元素,接近于 0.960
9.45198953442299649730e-01f, // 第四个元素,接近于 0.945
9.32060075959230460718e-01f, // 第五个元素,接近于 0.932
9.19991505039347012840e-01f, // 第六个元素,接近于 0.920
9.08726440052130879366e-01f, // 第七个元素,接近于 0.909
8.98095921898343418910e-01f, // 第八个元素,接近于 0.898
8.87984660755833377088e-01f, // 第九个元素,接近于 0.888
8.78309655808917399966e-01f, // 第十个元素,接近于 0.878
8.69008688036857046555e-01f, // 第十一个元素,接近于 0.869
8.60033621196331532488e-01f, // 第十二个元素,接近于 0.860
8.51346258458677951353e-01f, // 第十三个元素,接近于 0.851
8.42915653112204177333e-01f, // 第十四个元素,接近于 0.843
8.34716292986883434679e-01f, // 第十五个元素,接近于 0.827
8.26726833946221373317e-01f, // 第十六个元素,接近于 0.821
8.18929191603702366642e-01f, // 第十七个元素,接近于 0.819
8.11307874312656274185e-01f, // 第十八个元素,接近于 0.804
8.03849483170964274059e-01f, // 第十九个元素,接近于 0.794
7.96542330422958966274e-01f, // 第二十个元素,接近于 0.789
7.89376143566024590648e-01f, // 第二十一个元素,接近于 0.782
7.82341832654802504798e-01f, // 第二十二个元素,接近于 0.775
7.75431304981187174974e-01f, // 第二十三个元素,接近于 0.769
7.68637315798486264740e-01f, // 第二十四个元素,接近于 0.765
7.61953346836795386565e-01f, // 第二十五个元素,接近于 0.762
7.55373506507096115214e-01f, // 第二十六个元素,接近于 0.755
7.48892447219156820459e-01f, // 第二十七个元素,接近于 0.749
7.42505296340151055290e-01f, // 第二十八个元素,接近于 0.742
7.36207598126862650112e-01f, // 第二十九个元素,接近于 0.736
7.29995264561476231435e-01f, // 第三十个元素,接近于 0.730
7.23864533468630222401e-01f, // 第三十一个元素,接近于 0.724
7.17811932630721960535e-01f, // 第三十二个元素,接近于 0.718
7.11834248878248421200e-01f, // 第三十三个元素,接近于 0.712
7.05928501332754310127e-01f, // 第三十四个元素,接近于 0.706
7.00091918136511615067e-01f, // 第三十五个元素,接近于 0.700
6.94321916126116711609e-01f, // 第三十六个元素,接近于 0.694
6.88616083004671808432e-01f, // 第三十七个元素,接近于 0.688
6.82972161644994857355e-01f, // 第三十八个元素,接近于 0.682
6.77388036218773526009e-01f, // 第三十九个元素,接近于 0.677
6.71861719897082099173e-01f, // 第四十个元素,接近于 0.671
6.66391343908750100056e-01f, // 第四十一个元素,接近于 0.666
6.60975147776663107813e-01f, // 第四十二个元素,接近于 0.661
6.55611470579697264149e-01f, // 第四十三个元素,接近于 0.656
6.50298743110816701574e-01f, // 第四十四个元素,接近于 0.650
6.45035480820822293424e-01f, // 第四十五个元素,接近于 0.645
6.39820277453056585060e-01f, // 第四十六个元素,接近于 0.640
6.34651799287623608059e-01f, // 第四十七个元素,接近于 0.635
6.29528779924836690007e-01f, // 第四十八个元素,接近于 0.630
6.24450015547026504592e-01f, // 第四十九个元素,接近于 0.625
6.19414360605834324325e-01f, // 第五十个元素,接近于 0.619
6.14420723888913888899e-01f, // 第五十一个元素,接近于 0.614
6.09468064925773433949e-01f, // 第五十二个元素,接近于 0.609
6.04555390697467776029e-01f, // 第五十三个元素,接近于 0.605
5.99681752619125263415e-01f, // 第五十四个元素,接近于 0.600
5.94846243767987448159e-01f, // 第五十五个元素,接近于 0.595
5.90047996332826008015e-01f, // 第五十六个元素,接近于 0.590
5.85286179263371453274e-01f, // 第五十七个元素,接近于 0.585
5.80559996100790898232e-01f, // 第五十八个元素,接近于 0.580
5.75868682972353718164e-01f, // 第五十九个元素,接近于 0.576
5.71211506735253227163e-01f, // 第六
# 定义一个包含多个浮点数的数组,这些浮点数看起来是按照特定模式排列的
4.25583931338021970170e-01f, 4.21832709229495894654e-01f,
4.18100649837848226120e-01f, 4.14387534040891125642e-01f,
4.10693148270188157500e-01f, 4.07017284329473372217e-01f,
4.03359739221114510510e-01f, 3.99720314980197222177e-01f,
3.96098818515832451492e-01f, 3.92495061459315619512e-01f,
3.88908860018788715696e-01f, 3.85340034840077283462e-01f,
3.81788410873393657674e-01f, 3.78253817245619183840e-01f,
3.74736087137891138443e-01f, 3.71235057668239498696e-01f,
3.67750569779032587814e-01f, 3.64282468129004055601e-01f,
3.60830600989648031529e-01f, 3.57394820145780500731e-01f,
3.53974980800076777232e-01f, 3.50570941481406106455e-01f,
3.47182563956793643900e-01f, 3.43809713146850715049e-01f,
3.40452257044521866547e-01f, 3.37110066637006045021e-01f,
3.33783015830718454708e-01f, 3.30470981379163586400e-01f,
3.27173842813601400970e-01f, 3.23891482376391093290e-01f,
3.20623784956905355514e-01f, 3.17370638029913609834e-01f,
3.14131931596337177215e-01f, 3.10907558126286509559e-01f,
3.07697412504292056035e-01f, 3.04501391976649993243e-01f,
3.01319396100803049698e-01f, 2.98151326696685481377e-01f,
2.94997087799961810184e-01f, 2.91856585617095209972e-01f,
2.88729728482182923521e-01f, 2.85616426815501756042e-01f,
2.82516593083707578948e-01f, 2.79430141761637940157e-01f,
2.76356989295668320494e-01f, 2.73297054068577072172e-01f,
2.70250256365875463072e-01f, 2.67216518343561471038e-01f,
2.64195763997261190426e-01f, 2.61187919132721213522e-01f,
2.58192911337619235290e-01f, 2.55210669954661961700e-01f,
2.52241126055942177508e-01f, 2.49284212418528522415e-01f,
2.46339863501263828249e-01f, 2.43408015422750312329e-01f,
2.40488605940500588254e-01f, 2.37581574431238090606e-01f,
2.34686861872330010392e-01f, 2.31804410824338724684e-01f,
2.28934165414680340644e-01f, 2.26076071322380278694e-01f,
2.23230075763917484855e-01f, 2.20396127480151998723e-01f,
2.17574176724331130872e-01f, 2.14764175251173583536e-01f,
2.11966076307030182324e-01f, 2.09179834621125076977e-01f,
2.06405406397880797353e-01f, 2.03642749310334908452e-01f,
2.00891822494656591136e-01f, 1.98152586545775138971e-01f,
1.95425003514134304483e-01f, 1.92709036903589175926e-01f,
1.90004651670464985713e-01f, 1.87311814223800304768e-01f,
1.84630492426799269756e-01f, 1.81960655599522513892e-01f,
1.79302274522847582272e-01f, 1.76655321443734858455e-01f,
1.74019770081838553999e-01f, 1.71395595637505754327e-01f,
1.68782774801211288285e-01f, 1.66181285764481906364e-01f,
1.63591108232365584074e-01f, 1.61012223437511009516e-01f,
1.58444614155924284882e-01f, 1.55888264724479197465e-01f,
1.53343161060262855866e-01f, 1.50809290681845675763e-01f,
1.48286642732574552861e-01f, 1.45775208005994028060e-01f,
1.43274978973513461566e-01f, 1.40785949814444699690e-01f,
1.38308116448550733057e-01f, 1.35841476571253755301e-01f,
# 以下是一个包含多个浮点数的数组
1.33386029691669155683e-01f, 1.30941777173644358090e-01f,
1.28508722279999570981e-01f, 1.26086870220185887081e-01f,
1.23676228201596571932e-01f, 1.21276805484790306533e-01f,
1.18888613442910059947e-01f, 1.16511665625610869035e-01f,
1.14145977827838487895e-01f, 1.11791568163838089811e-01f,
1.09448457146811797824e-01f, 1.07116667774683801961e-01f,
1.04796225622487068629e-01f, 1.02487158941935246892e-01f,
1.00189498768810017482e-01f, 9.79032790388624646338e-02f,
9.56285367130089991594e-02f, 9.33653119126910124859e-02f,
9.11136480663737591268e-02f, 8.88735920682758862021e-02f,
8.66451944505580717859e-02f, 8.44285095703534715916e-02f,
8.22235958132029043366e-02f, 8.00305158146630696292e-02f,
7.78493367020961224423e-02f, 7.56801303589271778804e-02f,
7.35229737139813238622e-02f, 7.13779490588904025339e-02f,
6.92451443970067553879e-02f, 6.71246538277884968737e-02f,
6.50165779712428976156e-02f, 6.29210244377581412456e-02f,
6.08381083495398780614e-02f, 5.87679529209337372930e-02f,
5.67106901062029017391e-02f, 5.46664613248889208474e-02f,
5.26354182767921896513e-02f, 5.06177238609477817000e-02f,
4.86135532158685421122e-02f, 4.66230949019303814174e-02f,
4.46465522512944634759e-02f, 4.26841449164744590750e-02f,
4.07361106559409394401e-02f, 3.88027074045261474722e-02f,
3.68842156885673053135e-02f, 3.49809414617161251737e-02f,
3.30932194585785779961e-02f, 3.12214171919203004046e-02f,
2.93659397581333588001e-02f, 2.75272356696031131329e-02f,
2.57058040085489103443e-02f, 2.39022033057958785407e-02f,
2.21170627073088502113e-02f, 2.03510962300445102935e-02f,
1.86051212757246224594e-02f, 1.68800831525431419000e-02f,
1.51770883079353092332e-02f, 1.34974506017398673818e-02f,
1.18427578579078790488e-02f, 1.02149714397014590439e-02f,
8.61658276939872638800e-03f, 7.05087547137322242369e-03f,
5.52240329925099155545e-03f, 4.03797259336302356153e-03f,
2.60907274610215926189e-03f, 1.26028593049859797236e-03f
// 定义一个静态常量数组 ke_double,包含多个 64 位无符号整数
static const uint64_t ke_double[] = {
// 下面是数组的元素,每个元素都是一个 64 位的十六进制数
0x001C5214272497C6, // 元素1
0x0000000000000000, // 元素2
0x00137D5BD79C317E, // 元素3
0x00186EF58E3F3C10, // 元素4
0x001A9BB7320EB0AE, // 元素5
0x001BD127F719447C, // 元素6
0x001C951D0F88651A, // 元素7
0x001D1BFE2D5C3972, // 元素8
0x001D7E5BD56B18B2, // 元素9
0x001DC934DD172C70, // 元素10
0x001E0409DFAC9DC8, // 元素11
0x001E337B71D47836, // 元素12
0x001E5A8B177CB7A2, // 元素13
0x001E7B42096F046C, // 元素14
0x001E970DAF08AE3E, // 元素15
0x001EAEF5B14EF09E, // 元素16
0x001EC3BD07B46556, // 元素17
0x001ED5F6F08799CE, // 元素18
0x001EE614AE6E5688, // 元素19
0x001EF46ECA361CD0, // 元素20
0x001F014B76DDD4A4, // 元素21
0x001F0CE313A796B6, // 元素22
0x001F176369F1F77A, // 元素23
0x001F20F20C452570, // 元素24
0x001F29AE1951A874, // 元素25
0x001F31B18FB95532, // 元素26
0x001F39125157C106, // 元素27
0x001F3FE2EB6E694C, // 元素28
0x001F463332D788FA, // 元素29
0x001F4C10BF1D3A0E, // 元素30
0x001F51874C5C3322, // 元素31
0x001F56A109C3ECC0, // 元素32
0x001F5B66D9099996, // 元素33
0x001F5FE08210D08C, // 元素34
0x001F6414DD445772, // 元素35
0x001F6809F6859678, // 元素36
0x001F6BC52A2B02E6, // 元素37
0x001F6F4B3D32E4F4, // 元素38
0x001F72A07190F13A, // 元素39
0x001F75C8974D09D6, // 元素40
0x001F78C71B045CC0, // 元素41
0x001F7B9F12413FF4, // 元素42
0x001F7E5346079F8A, // 元素43
0x001F80E63BE21138, // 元素44
0x001F835A3DAD9162, // 元素45
0x001F85B16056B912, // 元素46
0x001F87ED89B24262, // 元素47
0x001F8A10759374FA, // 元素48
0x001F8C1BBA3D39AC, // 元素49
0x001F8E10CC45D04A, // 元素50
0x001F8FF102013E16, // 元素51
0x001F91BD968358E0, // 元素52
0x001F9377AC47AFD8, // 元素53
0x001F95204F8B64DA, // 元素54
0x001F96B878633892, // 元素55
0x001F98410C968892, // 元素56
0x001F99BAE146BA80, // 元素57
0x001F9B26BC697F00, // 元素58
0x001F9C85561B717A, // 元素59
0x001F9DD759CFD802, // 元素60
0x001F9F1D6761A1CE, // 元素61
0x001FA058140936C0, // 元素62
0x001FA187EB3A3338, // 元素63
0x001FA2AD6F6BC4FC, // 元素64
0x001FA3C91ACE0682, // 元素65
0x001FA4DB5FEE6AA2, // 元素66
0x001FA5E4AA4D097C, // 元素67
0x001FA6E55EE46782, // 元素68
0x001FA7DDDCA51EC4, // 元素69
0x001FA8CE7CE6A874, // 元素70
0x001FA9B793CE5FEE, // 元素71
0x001FAA9970ADB858, // 元素72
0x001FAB745E588232, // 元素73
0x001FAC48A3740584, // 元素74
0x001FAD1682BF9FE8, // 元素75
0x001FADDE3B5782C0, // 元素76
0x001FAEA008F21D6C, // 元素77
0x001FAF5C2418B07E, // 元素78
0x001FB012C25B7A12, // 元素79
0x001FB0C41681DFF4, // 元素80
0x001FB17050B6F1FA, // 元素81
0x001FB2179EB2963A, // 元素82
0x001FB2BA2BDFA84A, // 元素83
0x001FB358217F4E18, // 元素84
0x001FB3F1A6C9BE0C, // 元素85
0x001FB486E10CACD6, // 元素86
0x001FB517F3C793FC, // 元素87
0x001FB5A500C5FDAA, // 元素88
0x001FB62E2837FE58, // 元素89
0x001FB6B388C9010A, // 元素90
0x001FB7353FB50798, // 元素91
0x001FB7B368DC7DA8, // 元素92
0x001FB82E1ED6BA08, // 元素93
0x001FB8A57B0347F6, // 元素94
0x001FB919959A0F74, // 元素95
0x001FB98A85BA7204, // 元素96
0x001FB9F861796F26, // 元素97
0x001FBA633DEEE286, // 元素98
0x001FBACB2F41EC16, // 元素99
0x001FBB3048B49144, // 元素100
# 定义一个包含大量十六进制整数的数组
0x001FC3F9F78E4DA8, 0x001FC4107DB85060, 0x001FC4257877FD68,
0x001FC438E8B5BFC6, 0x001FC44ACF15112A, 0x001FC45B2BF447E8,
0x001FC469FF6C4504, 0x001FC477495001B2, 0x001FC483092BFBB8,
0x001FC48D3E457FF6, 0x001FC495E799D21A, 0x001FC49D03DD30B0,
0x001FC4A29179B432, 0x001FC4A68E8E07FC, 0x001FC4A8F8EBFB8C,
0x001FC4A9CE16EA9E, 0x001FC4A90B41FA34, 0x001FC4A6AD4E28A0,
0x001FC4A2B0C82E74, 0x001FC49D11E62DE2, 0x001FC495CC852DF4,
0x001FC48CDC265EC0, 0x001FC4823BEC237A, 0x001FC475E696DEE6,
0x001FC467D6817E82, 0x001FC458059DC036, 0x001FC4466D702E20,
0x001FC433070BCB98, 0x001FC41DCB0D6E0E, 0x001FC406B196BBF6,
0x001FC3EDB248CB62, 0x001FC3D2C43E593C, 0x001FC3B5DE0591B4,
0x001FC396F599614C, 0x001FC376005A4592, 0x001FC352F3069370,
0x001FC32DC1B22818, 0x001FC3065FBD7888, 0x001FC2DCBFCBF262,
0x001FC2B0D3B99F9E, 0x001FC2828C8FFCF0, 0x001FC251DA79F164,
0x001FC21EACB6D39E, 0x001FC1E8F18C6756, 0x001FC1B09637BB3C,
0x001FC17586DCCD10, 0x001FC137AE74D6B6, 0x001FC0F6F6BB2414,
0x001FC0B348184DA4, 0x001FC06C898BAFF0, 0x001FC022A092F364,
0x001FBFD5710F72B8, 0x001FBF84DD29488E, 0x001FBF30C52FC60A,
0x001FBED907770CC6, 0x001FBE7D80327DDA, 0x001FBE1E094BA614,
0x001FBDBA7A354408, 0x001FBD52A7B9F826, 0x001FBCE663C6201A,
0x001FBC757D2C4DE4, 0x001FBBFFBF63B7AA, 0x001FBB84F23FE6A2,
0x001FBB04D9A0D18C, 0x001FBA7F351A70AC, 0x001FB9F3BF92B618,
0x001FB9622ED4ABFC, 0x001FB8CA33174A16, 0x001FB82B76765B54,
0x001FB7859C5B895C, 0x001FB6D840D55594, 0x001FB622F7D96942,
0x001FB5654C6F37E0, 0x001FB49EBFBF69D2, 0x001FB3CEC803E746,
0x001FB2F4CF539C3E, 0x001FB21032442852, 0x001FB1203E5A9604,
0x001FB0243042E1C2, 0x001FAF1B31C479A6, 0x001FAE045767E104,
0x001FACDE9DBF2D72, 0x001FABA8E640060A, 0x001FAA61F399FF28,
0x001FA908656F66A2, 0x001FA79AB3508D3C, 0x001FA61726D1F214,
0x001FA47BD48BEA00, 0x001FA2C693C5C094, 0x001FA0F4F47DF314,
0x001F9F04336BBE0A, 0x001F9CF12B79F9BC, 0x001F9AB84415ABC4,
0x001F98555B782FB8, 0x001F95C3ABD03F78, 0x001F92FDA9CEF1F2,
0x001F8FFCDA9AE41C, 0x001F8CB99E7385F8, 0x001F892AEC479606,
0x001F8545F904DB8E, 0x001F80FDC336039A, 0x001F7C427839E926,
0x001F7700A3582ACC, 0x001F71200F1A241C, 0x001F6A8234B7352A,
0x001F630000A8E266, 0x001F5A66904FE3C4, 0x001F50724ECE1172,
0x001F44C7665C6FDA, 0x001F36E5A38A59A2, 0x001F26143450340A,
0x001F113E047B0414, 0x001EF6AEFA57CBE6, 0x001ED38CA188151E,
0x001EA2A61E122DB0, 0x001E5961C78B267C, 0x001DDDF62BAC0BB0,
0x001CDB4DD9E4E8C0};
// 定义一个静态常量双精度浮点数组,存储了一系列非常小的双精度浮点数
static const double we_double[] = {
9.655740063209182975e-16, // 第一个元素
7.089014243955414331e-18, // 第二个元素
1.163941249669122378e-17, // 第三个元素
1.524391512353216015e-17, // 第四个元素
1.833284885723743916e-17, // 第五个元素
2.108965109464486630e-17, // 第六个元素
2.361128077843138196e-17, // 第七个元素
2.595595772310893952e-17, // 第八个元素
2.816173554197752338e-17, // 第九个元素
3.025504130321382330e-17, // 第十个元素
3.225508254836375280e-17, // 第十一个元素
3.417632340185027033e-17, // 第十二个元素
3.602996978734452488e-17, // 第十三个元素
3.782490776869649048e-17, // 第十四个元素
3.956832198097553231e-17, // 第十五个元素
4.126611778175946428e-17, // 第十六个元素
4.292321808442525631e-17, // 第十七个元素
4.454377743282371417e-17, // 第十八个元素
4.613133981483185932e-17, // 第十九个元素
4.768895725264635940e-17, // 第二十个元素
4.921928043727962847e-17, // 第二十一个元素
5.072462904503147014e-17, // 第二十二个元素
5.220704702792671737e-17, // 第二十三个元素
5.366834661718192181e-17, // 第二十四个元素
5.511014372835094717e-17, // 第二十五个元素
5.653388673239667134e-17, // 第二十六个元素
5.794088004852766616e-17, // 第二十七个元素
5.933230365208943081e-17, // 第二十八个元素
6.070922932847179572e-17, // 第二十九个元素
6.207263431163193485e-17, // 第三十个元素
6.342341280303076511e-17, // 第三十一个元素
6.476238575956142121e-17, // 第三十二个元素
6.609030925769405241e-17, // 第三十三个元素
6.740788167872722244e-17, // 第三十四个元素
6.871574991183812442e-17, // 第三十五个元素
7.001451473403929616e-17, // 第三十六个元素
7.130473549660643409e-17, // 第三十七个元素
7.258693422414648352e-17, // 第三十八个元素
7.386159921381791997e-17, // 第三十九个元素
7.512918820723728089e-17, // 第四十个元素
7.639013119550825792e-17, // 第四十一个元素
7.764483290797848102e-17, // 第四十二个元素
7.889367502729790548e-17, // 第四十三个元素
8.013701816675454434e-17, // 第四十四个元素
8.137520364041762206e-17, // 第四十五个元素
8.260855505210038174e-17, // 第四十六个元素
8.383737972539139383e-17, // 第四十七个元素
8.506196999385323132e-17, // 第四十八个元素
8.628260436784112996e-17, // 第四十九个元素
8.749954859216182511e-17, // 第五十个元素
8.871305660690252281e-17, // 第五十一个元素
8.992337142215357066e-17, // 第五十二个元素
9.113072591597909173e-17, // 第五十三个元素
9.233534356381788123e-17, // 第五十四个元素
9.353743910649128938e-17, // 第五十五个元素
9.473721916312949566e-17, // 第五十六个元素
9.593488279457997317e-17, // 第五十七个元素
9.713062202221521206e-17, // 第五十八个元素
9.832462230649511362e-17, // 第五十九个元素
9.951706298915071878e-17, // 第六十个元素
1.007081177024294931e-16, // 第六十一个元素
1.018979547484694078e-16, // 第六十二个元素
1.030867374515421954e-16, // 第六十三个元素
1.042746244856188556e-16, // 第六十四个元素
1.054617701794576406e-16, // 第六十五个元素
1.066483248011914702e-16, // 第六十六个元素
1.078344348241948498e-16, // 第六十七个元素
1.090202431758350473e-16, // 第六十八个元素
1.102058894705578110e-16, // 第六十九个元素
1.113915102286197502e-16, // 第七十个元素
1.125772390816567488e-16, // 第七十一个元素
1.137632069661684705e-16, // 第七十二个元素
1.149495423059009298e-16, // 第七十三个元素
1.161363711840218308e-16, // 第七十四个元素
1.173238175059045788e-16, // 第七十五个元素
1.185120031532669434e-16, // 第七十六个元素
1.197010481303465158e-16, // 第七十七个元素
1.208910707027385520e-16, // 第七十八个元素
1.220821875294706151e-16, // 第七十九个元素
1.232745137888415193e-16, // 第八十个元素
1.244681632985112523e-16, // 第八十一个元素
1.256632486302898513e-16, // 第八十二个元素
1.268598812200397542e-16, // 第
# 定义一个长列表,包含一系列浮点数
1.537945957544996933e-16, 1.550563253257577148e-16,
1.563221653865837505e-16, 1.575922262431176140e-16,
1.588666190753684151e-16, 1.601454560042916733e-16,
1.614288501593278662e-16, 1.627169157465130500e-16,
1.640097681172717950e-16, 1.653075238380036909e-16,
1.666103007605742067e-16, 1.679182180938228863e-16,
1.692313964762022267e-16, 1.705499580496629830e-16,
1.718740265349031656e-16, 1.732037273081008369e-16,
1.745391874792533975e-16, 1.758805359722491379e-16,
1.772279036068006489e-16, 1.785814231823732619e-16,
1.799412295642463721e-16, 1.813074597718501559e-16,
1.826802530695252266e-16, 1.840597510598587828e-16,
1.854460977797569461e-16, 1.868394397994192684e-16,
1.882399263243892051e-16, 1.896477093008616722e-16,
1.910629435244376536e-16, 1.924857867525243818e-16,
1.939163998205899420e-16, 1.953549467624909132e-16,
1.968015949351037382e-16, 1.982565151475019047e-16,
1.997198817949342081e-16, 2.011918729978734671e-16,
2.026726707464198289e-16, 2.041624610503588774e-16,
2.056614340951917875e-16, 2.071697844044737034e-16,
2.086877110088159721e-16, 2.102154176219292789e-16,
2.117531128241075913e-16, 2.133010102535779087e-16,
2.148593288061663316e-16, 2.164282928437604723e-16,
2.180081324120784027e-16, 2.195990834682870728e-16,
2.212013881190495942e-16, 2.228152948696180545e-16,
2.244410588846308588e-16, 2.260789422613173739e-16,
2.277292143158621037e-16, 2.293921518837311354e-16,
2.310680396348213318e-16, 2.327571704043534613e-16,
2.344598455404957859e-16, 2.361763752697773994e-16,
2.379070790814276700e-16, 2.396522861318623520e-16,
2.414123356706293277e-16, 2.431875774892255956e-16,
2.449783723943070217e-16, 2.467850927069288738e-16,
2.486081227895851719e-16, 2.504478596029557040e-16,
2.523047132944217013e-16, 2.541791078205812227e-16,
2.560714816061770759e-16, 2.579822882420530896e-16,
2.599119972249746917e-16, 2.618610947423924219e-16,
2.638300845054942823e-16, 2.658194886341845120e-16,
2.678298485979525166e-16, 2.698617262169488933e-16,
2.719157047279818500e-16, 2.739923899205814823e-16,
2.760924113487617126e-16, 2.782164236246436081e-16,
2.803651078006983464e-16, 2.825391728480253184e-16,
2.847393572388174091e-16, 2.869664306419817679e-16,
2.892211957417995598e-16, 2.915044901905293183e-16,
2.938171887070028633e-16, 2.961602053345465687e-16,
2.985344958730045276e-16, 3.009410605012618141e-16,
3.033809466085003416e-16, 3.058552518544860874e-16,
3.083651274815310004e-16, 3.109117819034266344e-16,
3.134964845996663118e-16, 3.161205703467105734e-16,
3.187854438219713117e-16, 3.214925846206797361e-16,
3.242435527309451638e-16, 3.270399945182240440e-16,
3.298836492772283149e-16, 3.327763564171671408e-16,
3.357200633553244075e-16, 3.387168342045505162e-16,
3.417688593525636996e-16, 3.448784660453423890e-16,
# 以下是一个包含浮点数的数组
3.480481301037442286e-16, 3.512804889222979418e-16,
3.545783559224791863e-16, 3.579447366604276541e-16,
3.613828468219060593e-16, 3.648961323764542545e-16,
3.684882922095621322e-16, 3.721633036080207290e-16,
3.759254510416256532e-16, 3.797793587668874387e-16,
3.837300278789213687e-16, 3.877828785607895292e-16,
3.919437984311428867e-16, 3.962191980786774996e-16,
4.006160751056541688e-16, 4.051420882956573177e-16,
4.098056438903062509e-16, 4.146159964290904582e-16,
4.195833672073398926e-16, 4.247190841824385048e-16,
4.300357481667470702e-16, 4.355474314693952008e-16,
4.412699169036069903e-16, 4.472209874259932285e-16,
4.534207798565834480e-16, 4.598922204905932469e-16,
4.666615664711475780e-16, 4.737590853262492027e-16,
4.812199172829237933e-16, 4.890851827392209900e-16,
4.974034236191939753e-16, 5.062325072144159699e-16,
5.156421828878082953e-16, 5.257175802022274839e-16,
5.365640977112021618e-16, 5.483144034258703912e-16,
5.611387454675159622e-16, 5.752606481503331688e-16,
5.909817641652102998e-16, 6.087231416180907671e-16,
6.290979034877557049e-16, 6.530492053564040799e-16,
6.821393079028928626e-16, 7.192444966089361564e-16,
7.706095350032096755e-16, 8.545517038584027421e-16;
// 定义一个静态常量数组,存储双精度浮点数
static const double fe_double[] = {
// 数组元素1
1.000000000000000000e+00,
// 数组元素2
9.381436808621747003e-01,
// 数组元素3
9.004699299257464817e-01,
// 数组元素4
8.717043323812035949e-01,
// 数组元素5
8.477855006239896074e-01,
// 数组元素6
8.269932966430503241e-01,
// 数组元素7
8.084216515230083777e-01,
// 数组元素8
7.915276369724956185e-01,
// 数组元素9
7.759568520401155522e-01,
// 数组元素10
7.614633888498962833e-01,
// 数组元素11
7.478686219851951034e-01,
// 数组元素12
7.350380924314234843e-01,
// 数组元素13
7.228676595935720206e-01,
// 数组元素14
7.112747608050760117e-01,
// 数组元素15
7.001926550827881623e-01,
// 数组元素16
6.895664961170779872e-01,
// 数组元素17
6.793505722647653622e-01,
// 数组元素18
6.695063167319247333e-01,
// 数组元素19
6.600008410789997004e-01,
// 数组元素20
6.508058334145710999e-01,
// 数组元素21
6.418967164272660897e-01,
// 数组元素22
6.332519942143660652e-01,
// 数组元素23
6.248527387036659775e-01,
// 数组元素24
6.166821809152076561e-01,
// 数组元素25
6.087253820796220127e-01,
// 数组元素26
6.009689663652322267e-01,
// 数组元素27
5.934009016917334289e-01,
// 数组元素28
5.860103184772680329e-01,
// 数组元素29
5.787873586028450257e-01,
// 数组元素30
5.717230486648258170e-01,
// 数组元素31
5.648091929124001709e-01,
// 数组元素32
5.580382822625874484e-01,
// 数组元素33
5.514034165406412891e-01,
// 数组元素34
5.448982376724396115e-01,
// 数组元素35
5.385168720028619127e-01,
// 数组元素36
5.322538802630433219e-01,
// 数组元素37
5.261042139836197284e-01,
// 数组元素38
5.200631773682335979e-01,
// 数组元素39
5.141263938147485613e-01,
// 数组元素40
5.082897764106428795e-01,
// 数组元素41
5.025495018413477233e-01,
// 数组元素42
4.969019872415495476e-01,
// 数组元素43
4.913438695940325340e-01,
// 数组元素44
4.858719873418849144e-01,
// 数组元素45
4.804833639304542103e-01,
// 数组元素46
4.751751930373773747e-01,
// 数组元素47
4.699448252839599771e-01,
// 数组元素48
4.647897562504261781e-01,
// 数组元素49
4.597076156421376902e-01,
// 数组元素50
4.546961574746155033e-01,
// 数组元素51
4.497532511627549967e-01,
// 数组元素52
4.448768734145485126e-01,
// 数组元素53
4.400651008423538957e-01,
// 数组元素54
4.353161032156365740e-01,
// 数组元素55
4.306281372884588343e-01,
// 数组元素56
4.259995411430343437e-01,
// 数组元素57
4.214287289976165751e-01,
// 数组元素58
4.169141864330028757e-01,
// 数组元素59
4.124544659971611793e-01,
// 数组元素60
4.080481831520323954e-01,
// 数组元素61
4.036940125305302773e-01,
// 数组元素62
3.993906844752310725e-01,
// 数组元素63
3.951369818332901573e-01,
// 数组元素64
3.909317369847971069e-01,
// 数组元素65
3.867738290841376547e-01,
// 数组元素66
3.826621814960098344e-01,
// 数组元素67
3.785957594095807899e-01,
// 数组元素68
3.745735676159021588e-01,
// 数组元素69
3.705946484351460013e-01,
// 数组元素70
3.666580797815141568e-01,
// 数组元素71
3.627629733548177748e-01,
// 数组元素72
3.589084729487497794e-01,
// 数组元素73
3.550937528667874599e-01,
// 数组元素74
3.513180164374833381e-01,
// 数组元素75
3.475804946216369817e-01,
// 数组元素76
3.438804447045024082e-01,
// 数组元素77
3.402171490667800224e-01,
// 数组元素78
3.365899140286776059e-01,
// 数组元素79
3.329980687618089852e-01,
// 数组元素80
3.294409642641363267e-01,
// 数组元素81
3.259179723935561879e-01,
// 数组元素82
3.224284849560891675e-01,
// 数组元素83
3.189719128449572394e-01,
// 数组元素84
3.155476852271289490e-01,
// 数组元素85
3.121552487741795501e-01,
// 数组元素86
3.087940669345601852e-01,
// 数组元素87
3.054636192445902565e-01,
// 数组元素88
3.021634006756935276e-01,
// 数组元素89
2.988929210155817917e-01,
// 数组元素90
2.956517042812611962e-01,
// 数组元素91
2.924392881618925744e-01,
// 数组元素92
# 下面是一系列的浮点数,每个数表示一个数据点的数值
2.502590823688622956e-01, 2.474310756653276266e-01,
2.446259691318921070e-01, 2.418434693988772144e-01,
2.390832902624491774e-01, 2.363451524570596429e-01,
2.336287834374333461e-01, 2.309339171696274118e-01,
2.282602939307167011e-01, 2.256076601166840667e-01,
2.229757680581201940e-01, 2.203643758433594946e-01,
2.177732471487005272e-01, 2.152021510753786837e-01,
2.126508619929782795e-01, 2.101191593889882581e-01,
2.076068277242220372e-01, 2.051136562938377095e-01,
2.026394390937090173e-01, 2.001839746919112650e-01,
1.977470661050988732e-01, 1.953285206795632167e-01,
1.929281499767713515e-01, 1.905457696631953912e-01,
1.881811994042543179e-01, 1.858342627621971110e-01,
1.835047870977674633e-01, 1.811926034754962889e-01,
1.788975465724783054e-01, 1.766194545904948843e-01,
1.743581691713534942e-01, 1.721135353153200598e-01,
1.698854013025276610e-01, 1.676736186172501919e-01,
1.654780418749360049e-01, 1.632985287519018169e-01,
1.611349399175920349e-01, 1.589871389693142123e-01,
1.568549923693652315e-01, 1.547383693844680830e-01,
1.526371420274428570e-01, 1.505511850010398944e-01,
1.484803756438667910e-01, 1.464245938783449441e-01,
1.443837221606347754e-01, 1.423576454324722018e-01,
1.403462510748624548e-01, 1.383494288635802039e-01,
1.363670709264288572e-01, 1.343990717022136294e-01,
1.324453279013875218e-01, 1.305057384683307731e-01,
1.285802045452281717e-01, 1.266686294375106714e-01,
1.247709185808309612e-01, 1.228869795095451356e-01,
1.210167218266748335e-01, 1.191600571753276827e-01,
1.173168992115555670e-01, 1.154871635786335338e-01,
1.136707678827443141e-01, 1.118676316700562973e-01,
1.100776764051853845e-01, 1.083008254510337970e-01,
1.065370040500016602e-01, 1.047861393065701724e-01,
1.030481601712577161e-01, 1.013229974259536315e-01,
9.961058367063713170e-02, 9.791085331149219917e-02,
9.622374255043279756e-02, 9.454918937605585882e-02,
9.288713355604354127e-02, 9.123751663104015530e-02,
8.960028191003285847e-02, 8.797537446727021759e-02,
8.636274114075691288e-02, 8.476233053236811865e-02,
8.317409300963238272e-02, 8.159798070923741931e-02,
8.003394754231990538e-02, 7.848194920160642130e-02,
7.694194317048050347e-02, 7.541388873405840965e-02,
7.389774699236474620e-02, 7.239348087570873780e-02,
7.090105516237182881e-02, 6.942043649872875477e-02,
6.795159342193660135e-02, 6.649449638533977414e-02,
6.504911778675374900e-02, 6.361543199980733421e-02,
6.219341540854099459e-02, 6.078304644547963265e-02,
5.938430563342026597e-02, 5.799717563120065922e-02,
5.662164128374287675e-02, 5.525768967669703741e-02,
5.390531019604608703e-02, 5.256449459307169225e-02,
5.123523705512628146e-02, 4.991753428270637172e-02,
4.861138557337949667e-02, 4.731679291318154762e-02,
4.603376107617516977e-02, 4.476229773294328196e-02,
# 定义一个包含浮点数的数组
4.350241356888818328e-02, 4.225412241331623353e-02,
4.101744138041481941e-02, 3.979239102337412542e-02,
3.857899550307485742e-02, 3.737728277295936097e-02,
3.618728478193142251e-02, 3.500903769739741045e-02,
3.384258215087432992e-02, 3.268796350895953468e-02,
3.154523217289360859e-02, 3.041444391046660423e-02,
2.929566022463739317e-02, 2.818894876397863569e-02,
2.709438378095579969e-02, 2.601204664513421735e-02,
2.494202641973178314e-02, 2.388442051155817078e-02,
2.283933540638524023e-02, 2.180688750428358066e-02,
2.078720407257811723e-02, 1.978042433800974303e-02,
1.878670074469603046e-02, 1.780620041091136169e-02,
1.683910682603994777e-02, 1.588562183997316302e-02,
1.494596801169114850e-02, 1.402039140318193759e-02,
1.310916493125499106e-02, 1.221259242625538123e-02,
1.133101359783459695e-02, 1.046481018102997894e-02,
9.614413642502209895e-03, 8.780314985808975251e-03,
7.963077438017040002e-03, 7.163353183634983863e-03,
6.381905937319179087e-03, 5.619642207205483020e-03,
4.877655983542392333e-03, 4.157295120833795314e-03,
3.460264777836904049e-03, 2.788798793574076128e-03,
2.145967743718906265e-03, 1.536299780301572356e-03,
9.672692823271745359e-04, 4.541343538414967652e-04};
// 定义一个名为 ke_float 的静态常量数组,存储的是十六进制形式的无符号整数
static const uint32_t ke_float[] = {
0x00714851UL, 0x00000000UL, 0x004DF56FUL, 0x0061BBD6UL, 0x006A6EDDUL,
0x006F44A0UL, 0x00725474UL, 0x00746FF9UL, 0x0075F96FUL, 0x007724D3UL,
0x00781027UL, 0x0078CDEEUL, 0x00796A2CUL, 0x0079Ed08UL, 0x007A5C37UL,
0x007ABBD7UL, 0x007B0EF4UL, 0x007B57DCUL, 0x007B9853UL, 0x007BD1BBUL,
0x007C052EUL, 0x007C338CUL, 0x007C5D8EUL, 0x007C83C8UL, 0x007CA6B8UL,
0x007CC6C6UL, 0x007CE449UL, 0x007CFF8CUL, 0x007D18CDUL, 0x007D3043UL,
0x007D461DUL, 0x007D5A84UL, 0x007D6D9BUL, 0x007D7F82UL, 0x007D9053UL,
0x007DA028UL, 0x007DAF15UL, 0x007DBD2DUL, 0x007DCA82UL, 0x007DD722UL,
0x007DE31CUL, 0x007DEE7CUL, 0x007DF94DUL, 0x007E0399UL, 0x007E0D69UL,
0x007E16C6UL, 0x007E1FB6UL, 0x007E2842UL, 0x007E306FUL, 0x007E3843UL,
0x007E3FC4UL, 0x007E46F6UL, 0x007E4DDFUL, 0x007E5481UL, 0x007E5AE2UL,
0x007E6104UL, 0x007E66ECUL, 0x007E6C9BUL, 0x007E7215UL, 0x007E775DUL,
0x007E7C76UL, 0x007E8160UL, 0x007E8620UL, 0x007E8AB6UL, 0x007E8F24UL,
0x007E936DUL, 0x007E9793UL, 0x007E9B95UL, 0x007E9F77UL, 0x007EA33AUL,
0x007EA6DEUL, 0x007EAA66UL, 0x007EADD1UL, 0x007EB123UL, 0x007EB45AUL,
0x007EB779UL, 0x007EBA80UL, 0x007EBD71UL, 0x007EC04BUL, 0x007EC310UL,
0x007EC5C1UL, 0x007EC85EUL, 0x007ECAE9UL, 0x007ECD61UL, 0x007ECFC7UL,
0x007ED21CUL, 0x007ED460UL, 0x007ED694UL, 0x007ED8B9UL, 0x007EDACEUL,
0x007EDCD5UL, 0x007EDECEUL, 0x007EE0B8UL, 0x007EE296UL, 0x007EE466UL,
0x007EE62AUL, 0x007EE7E2UL, 0x007EE98DUL, 0x007EEB2DUL, 0x007EECC1UL,
0x007EEE4AUL, 0x007EEFC9UL, 0x007EF13DUL, 0x007EF2A7UL, 0x007EF406UL,
0x007EF55CUL, 0x007EF6A8UL, 0x007EF7EBUL, 0x007EF924UL, 0x007EFA55UL,
0x007EFB7DUL, 0x007EFC9CUL, 0x007EFDB2UL, 0x007EFEC1UL, 0x007EFFC7UL,
0x007F00C5UL, 0x007F01BBUL, 0x007F02AAUL, 0x007F0391UL, 0x007F0470UL,
0x007F0548UL, 0x007F0618UL, 0x007F06E2UL, 0x007F07A4UL, 0x007F0860UL,
0x007F0914UL, 0x007F09C2UL, 0x007F0A69UL, 0x007F0B09UL, 0x007F0BA3UL,
0x007F0C36UL, 0x007F0CC2UL, 0x007F0D48UL, 0x007F0DC8UL, 0x007F0E41UL,
0x007F0EB4UL, 0x007F0F21UL, 0x007F0F88UL, 0x007F0FE8UL, 0x007F1042UL,
0x007F1096UL, 0x007F10E4UL, 0x007F112BUL, 0x007F116DUL, 0x007F11A8UL,
0x007F11DDUL, 0x007F120CUL, 0x007F1235UL, 0x007F1258UL, 0x007F1274UL,
0x007F128AUL, 0x007F129AUL, 0x007F12A4UL, 0x007F12A7UL, 0x007F12A4UL,
0x007F129BUL, 0x007F128BUL, 0x007F1274UL, 0x007F1257UL, 0x007F1233UL,
0x007F1209UL, 0x007F11D8UL, 0x007F119FUL, 0x007F1160UL, 0x007F111AUL,
0x007F10CCUL, 0x007F1077UL, 0x007F101BUL, 0x007F0FB7UL, 0x007F0F4BUL,
0x007F0ED7UL, 0x007F0E5CUL, 0x007F0DD8UL, 0x007F0D4CUL, 0x007F0CB7UL,
0x007F0C19UL, 0x007F0B73UL, 0x007F0AC3UL, 0x007F0A0AUL, 0x007F0947UL,
0x007F087BUL, 0x007F07A4UL, 0x007F06C2UL, 0x007F05D6UL, 0x007F04DFUL,
0x007F03DCUL, 0x007F02CDUL, 0x007F01B2UL, 0x007F008BUL, 0x007EFF56UL,
0x007EFE13UL, 0x007EFCC3UL, 0x007EFB64UL, 0x007EF9F6UL, 0x007EF878UL,
0x007EF6EAUL, 0x007EF54BUL, 0x007EF39AUL, 0x007EF1D6UL, 0x007EEFFFUL,
};
# 定义一个长整型数组,包含多个十六进制表示的地址值
[
0x007EEE14UL, 0x007EEC13UL, 0x007EE9FDUL, 0x007EE7CFUL, 0x007EE589UL,
0x007EE329UL, 0x007EE0AEUL, 0x007EDE16UL, 0x007EDB61UL, 0x007ED88CUL,
0x007ED595UL, 0x007ED27BUL, 0x007ECF3BUL, 0x007ECBD3UL, 0x007EC841UL,
0x007EC481UL, 0x007EC091UL, 0x007EBC6DUL, 0x007EB811UL, 0x007EB37AUL,
0x007EAEA4UL, 0x007EA988UL, 0x007EA422UL, 0x007E9E6BUL, 0x007E985DUL,
0x007E91EFUL, 0x007E8B1AUL, 0x007E83D4UL, 0x007E7C11UL, 0x007E73C5UL,
0x007E6AE1UL, 0x007E6155UL, 0x007E570FUL, 0x007E4BF7UL, 0x007E3FF3UL,
0x007E32E6UL, 0x007E24ACUL, 0x007E1518UL, 0x007E03F7UL, 0x007DF10AUL,
0x007DDC03UL, 0x007DC480UL, 0x007DAA09UL, 0x007D8C00UL, 0x007D699AUL,
0x007D41C9UL, 0x007D131EUL, 0x007CDB97UL, 0x007C9851UL, 0x007C44F8UL,
0x007BDABCUL, 0x007B4E33UL, 0x007A8A98UL, 0x00796587UL, 0x007777D9UL,
0x00736D37UL,
]
// 定义名为 we_float 的静态常量浮点数组,包含一系列浮点数
static const float we_float[] = {
// 下面是具体的浮点数数值
1.03677719e-06F, 7.61177108e-09F, 1.24977240e-08F, 1.63680292e-08F,
1.96847466e-08F, 2.26448404e-08F, 2.53524197e-08F, 2.78699974e-08F,
3.02384333e-08F, 3.24861032e-08F, 3.46336312e-08F, 3.66965478e-08F,
3.86868855e-08F, 4.06141855e-08F, 4.24861622e-08F, 4.43091566e-08F,
4.60884545e-08F, 4.78285168e-08F, 4.95331490e-08F, 5.12056279e-08F,
5.28488000e-08F, 5.44651557e-08F, 5.60568899e-08F, 5.76259484e-08F,
5.91740662e-08F, 6.07027987e-08F, 6.22135462e-08F, 6.37075759e-08F,
6.51860386e-08F, 6.66499836e-08F, 6.81003709e-08F, 6.95380822e-08F,
7.09639292e-08F, 7.23786618e-08F, 7.37829746e-08F, 7.51775128e-08F,
7.65628768e-08F, 7.79396272e-08F, 7.93082883e-08F, 8.06693516e-08F,
8.20232788e-08F, 8.33705045e-08F, 8.47114385e-08F, 8.60464681e-08F,
8.73759596e-08F, 8.87002606e-08F, 9.00197010e-08F, 9.13345948e-08F,
9.26452410e-08F, 9.39519249e-08F, 9.52549192e-08F, 9.65544849e-08F,
9.78508719e-08F, 9.91443202e-08F, 1.00435060e-07F, 1.01723315e-07F,
1.03009296e-07F, 1.04293211e-07F, 1.05575259e-07F, 1.06855633e-07F,
1.08134518e-07F, 1.09412096e-07F, 1.10688542e-07F, 1.11964025e-07F,
1.13238713e-07F, 1.14512767e-07F, 1.15786343e-07F, 1.17059595e-07F,
1.18332673e-07F, 1.19605723e-07F, 1.20878890e-07F, 1.22152313e-07F,
1.23426131e-07F, 1.24700479e-07F, 1.25975490e-07F, 1.27251294e-07F,
1.28528022e-07F, 1.29805799e-07F, 1.31084751e-07F, 1.32365001e-07F,
1.33646673e-07F, 1.34929886e-07F, 1.36214760e-07F, 1.37501415e-07F,
1.38789966e-07F, 1.40080532e-07F, 1.41373228e-07F, 1.42668169e-07F,
1.43965470e-07F, 1.45265245e-07F, 1.46567606e-07F, 1.47872669e-07F,
1.49180545e-07F, 1.50491348e-07F, 1.51805191e-07F, 1.53122186e-07F,
1.54442445e-07F, 1.55766083e-07F, 1.57093212e-07F, 1.58423946e-07F,
1.59758399e-07F, 1.61096684e-07F, 1.62438917e-07F, 1.63785214e-07F,
1.65135690e-07F, 1.66490462e-07F, 1.67849647e-07F, 1.69213364e-07F,
1.70581733e-07F, 1.71954874e-07F, 1.73332908e-07F, 1.74715958e-07F,
1.76104148e-07F, 1.77497602e-07F, 1.78896448e-07F, 1.80300814e-07F,
1.81710828e-07F, 1.83126623e-07F, 1.84548331e-07F, 1.85976086e-07F,
1.87410026e-07F, 1.88850288e-07F, 1.90297012e-07F, 1.91750343e-07F,
1.93210424e-07F, 1.94677403e-07F, 1.96151428e-07F, 1.97632653e-07F,
1.99121231e-07F, 2.00617321e-07F, 2.02121082e-07F, 2.03632677e-07F,
2.05152273e-07F, 2.06680040e-07F, 2.08216149e-07F, 2.09760777e-07F,
2.11314104e-07F, 2.12876312e-07F, 2.14447590e-07F, 2.16028129e-07F,
2.17618123e-07F, 2.19217773e-07F, 2.20827283e-07F, 2.22446862e-07F,
2.24076723e-07F, 2.25717086e-07F, 2.27368174e-07F, 2.29030216e-07F,
2.30703448e-07F, 2.32388110e-07F, 2.34084450e-07F, 2.35792720e-07F,
2.37513182e-07F, 2.39246101e-07F, 2.40991752e-07F, 2.42750416e-07F,
2.44522382e-07F, 2.46307948e-07F, 2.48107418e-07F, 2.49921109e-07F,
2.51749342e-07F, 2.53592452e-07F, 2.55450781e-07F, 2.57324683e-07F
};
# 下面是一系列的浮点数值,表示在科学计数法中的小数,后缀 'F' 表示这是单精度浮点数
2.59214522e-07F, 2.61120673e-07F, 2.63043524e-07F, 2.64983476e-07F,
2.66940939e-07F, 2.68916342e-07F, 2.70910123e-07F, 2.72922739e-07F,
2.74954660e-07F, 2.77006373e-07F, 2.79078382e-07F, 2.81171210e-07F,
2.83285396e-07F, 2.85421503e-07F, 2.87580110e-07F, 2.89761822e-07F,
2.91967265e-07F, 2.94197089e-07F, 2.96451969e-07F, 2.98732610e-07F,
3.01039742e-07F, 3.03374127e-07F, 3.05736557e-07F, 3.08127859e-07F,
3.10548894e-07F, 3.13000563e-07F, 3.15483804e-07F, 3.17999599e-07F,
3.20548974e-07F, 3.23133003e-07F, 3.25752811e-07F, 3.28409576e-07F,
3.31104534e-07F, 3.33838984e-07F, 3.36614287e-07F, 3.39431878e-07F,
3.42293264e-07F, 3.45200034e-07F, 3.48153864e-07F, 3.51156520e-07F,
3.54209871e-07F, 3.57315892e-07F, 3.60476673e-07F, 3.63694431e-07F,
3.66971518e-07F, 3.70310433e-07F, 3.73713834e-07F, 3.77184553e-07F,
3.80725611e-07F, 3.84340234e-07F, 3.88031877e-07F, 3.91804239e-07F,
3.95661291e-07F, 3.99607304e-07F, 4.03646879e-07F, 4.07784981e-07F,
4.12026980e-07F, 4.16378695e-07F, 4.20846449e-07F, 4.25437124e-07F,
4.30158235e-07F, 4.35018005e-07F, 4.40025460e-07F, 4.45190536e-07F,
4.50524210e-07F, 4.56038644e-07F, 4.61747369e-07F, 4.67665494e-07F,
4.73809965e-07F, 4.80199879e-07F, 4.86856855e-07F, 4.93805512e-07F,
5.01074042e-07F, 5.08694944e-07F, 5.16705952e-07F, 5.25151216e-07F,
5.34082859e-07F, 5.43563016e-07F, 5.53666578e-07F, 5.64484953e-07F,
5.76131313e-07F, 5.88748108e-07F, 6.02518140e-07F, 6.17681418e-07F,
6.34561837e-07F, 6.53611496e-07F, 6.75488730e-07F, 7.01206245e-07F,
7.32441505e-07F, 7.72282898e-07F, 8.27435688e-07F, 9.17567905e-07F,
// 定义静态常量数组 fe_float,存储了一组浮点数值
static const float fe_float[] = {
// 下面是具体的浮点数值列表
1.00000000e+00F, 9.38143681e-01F, 9.00469930e-01F, 8.71704332e-01F,
8.47785501e-01F, 8.26993297e-01F, 8.08421652e-01F, 7.91527637e-01F,
7.75956852e-01F, 7.61463389e-01F, 7.47868622e-01F, 7.35038092e-01F,
7.22867660e-01F, 7.11274761e-01F, 7.00192655e-01F, 6.89566496e-01F,
6.79350572e-01F, 6.69506317e-01F, 6.60000841e-01F, 6.50805833e-01F,
6.41896716e-01F, 6.33251994e-01F, 6.24852739e-01F, 6.16682181e-01F,
6.08725382e-01F, 6.00968966e-01F, 5.93400902e-01F, 5.86010318e-01F,
5.78787359e-01F, 5.71723049e-01F, 5.64809193e-01F, 5.58038282e-01F,
5.51403417e-01F, 5.44898238e-01F, 5.38516872e-01F, 5.32253880e-01F,
5.26104214e-01F, 5.20063177e-01F, 5.14126394e-01F, 5.08289776e-01F,
5.02549502e-01F, 4.96901987e-01F, 4.91343870e-01F, 4.85871987e-01F,
4.80483364e-01F, 4.75175193e-01F, 4.69944825e-01F, 4.64789756e-01F,
4.59707616e-01F, 4.54696157e-01F, 4.49753251e-01F, 4.44876873e-01F,
4.40065101e-01F, 4.35316103e-01F, 4.30628137e-01F, 4.25999541e-01F,
4.21428729e-01F, 4.16914186e-01F, 4.12454466e-01F, 4.08048183e-01F,
4.03694013e-01F, 3.99390684e-01F, 3.95136982e-01F, 3.90931737e-01F,
3.86773829e-01F, 3.82662181e-01F, 3.78595759e-01F, 3.74573568e-01F,
3.70594648e-01F, 3.66658080e-01F, 3.62762973e-01F, 3.58908473e-01F,
3.55093753e-01F, 3.51318016e-01F, 3.47580495e-01F, 3.43880445e-01F,
3.40217149e-01F, 3.36589914e-01F, 3.32998069e-01F, 3.29440964e-01F,
3.25917972e-01F, 3.22428485e-01F, 3.18971913e-01F, 3.15547685e-01F,
3.12155249e-01F, 3.08794067e-01F, 3.05463619e-01F, 3.02163401e-01F,
2.98892921e-01F, 2.95651704e-01F, 2.92439288e-01F, 2.89255223e-01F,
2.86099074e-01F, 2.82970415e-01F, 2.79868833e-01F, 2.76793928e-01F,
2.73745310e-01F, 2.70722597e-01F, 2.67725420e-01F, 2.64753419e-01F,
2.61806243e-01F, 2.58883550e-01F, 2.55985007e-01F, 2.53110290e-01F,
2.50259082e-01F, 2.47431076e-01F, 2.44625969e-01F, 2.41843469e-01F,
2.39083290e-01F, 2.36345152e-01F, 2.33628783e-01F, 2.30933917e-01F,
2.28260294e-01F, 2.25607660e-01F, 2.22975768e-01F, 2.20364376e-01F,
2.17773247e-01F, 2.15202151e-01F, 2.12650862e-01F, 2.10119159e-01F,
2.07606828e-01F, 2.05113656e-01F, 2.02639439e-01F, 2.00183975e-01F,
1.97747066e-01F, 1.95328521e-01F, 1.92928150e-01F, 1.90545770e-01F,
1.88181199e-01F, 1.85834263e-01F, 1.83504787e-01F, 1.81192603e-01F,
1.78897547e-01F, 1.76619455e-01F, 1.74358169e-01F, 1.72113535e-01F,
1.69885401e-01F, 1.67673619e-01F, 1.65478042e-01F, 1.63298529e-01F,
1.61134940e-01F, 1.58987139e-01F, 1.56854992e-01F, 1.54738369e-01F,
1.52637142e-01F, 1.50551185e-01F, 1.48480376e-01F, 1.46424594e-01F,
1.44383722e-01F, 1.42357645e-01F, 1.40346251e-01F, 1.38349429e-01F,
1.36367071e-01F, 1.34399072e-01F, 1.32445328e-01F, 1.30505738e-01F,
1.28580205e-01F, 1.26668629e-01F, 1.24770919e-01F, 1.22886980e-01F,
1.21016722e-01F, 1.19160057e-01F, 1.17316899e-01F, 1.15487164e-01F,
};
# 下面是一个包含浮点数的长列表
1.13670768e-01F, 1.11867632e-01F, 1.10077676e-01F, 1.08300825e-01F,
1.06537004e-01F, 1.04786139e-01F, 1.03048160e-01F, 1.01322997e-01F,
9.96105837e-02F, 9.79108533e-02F, 9.62237426e-02F, 9.45491894e-02F,
9.28871336e-02F, 9.12375166e-02F, 8.96002819e-02F, 8.79753745e-02F,
8.63627411e-02F, 8.47623305e-02F, 8.31740930e-02F, 8.15979807e-02F,
8.00339475e-02F, 7.84819492e-02F, 7.69419432e-02F, 7.54138887e-02F,
7.38977470e-02F, 7.23934809e-02F, 7.09010552e-02F, 6.94204365e-02F,
6.79515934e-02F, 6.64944964e-02F, 6.50491178e-02F, 6.36154320e-02F,
6.21934154e-02F, 6.07830464e-02F, 5.93843056e-02F, 5.79971756e-02F,
5.66216413e-02F, 5.52576897e-02F, 5.39053102e-02F, 5.25644946e-02F,
5.12352371e-02F, 4.99175343e-02F, 4.86113856e-02F, 4.73167929e-02F,
4.60337611e-02F, 4.47622977e-02F, 4.35024136e-02F, 4.22541224e-02F,
4.10174414e-02F, 3.97923910e-02F, 3.85789955e-02F, 3.73772828e-02F,
3.61872848e-02F, 3.50090377e-02F, 3.38425822e-02F, 3.26879635e-02F,
3.15452322e-02F, 3.04144439e-02F, 2.92956602e-02F, 2.81889488e-02F,
2.70943838e-02F, 2.60120466e-02F, 2.49420264e-02F, 2.38844205e-02F,
2.28393354e-02F, 2.18068875e-02F, 2.07872041e-02F, 1.97804243e-02F,
1.87867007e-02F, 1.78062004e-02F, 1.68391068e-02F, 1.58856218e-02F,
1.49459680e-02F, 1.40203914e-02F, 1.31091649e-02F, 1.22125924e-02F,
1.13310136e-02F, 1.04648102e-02F, 9.61441364e-03F, 8.78031499e-03F,
7.96307744e-03F, 7.16335318e-03F, 6.38190594e-03F, 5.61964221e-03F,
4.87765598e-03F, 4.15729512e-03F, 3.46026478e-03F, 2.78879879e-03F,
2.14596774e-03F, 1.53629978e-03F, 9.67269282e-04F, 4.54134354e-04F,
};
这段代码没有需要注释的内容,它是一个静态变量声明的结尾,不需要额外的解释。
.\numpy\numpy\random\src\legacy\legacy-distributions.c
/*
* This file contains generation code for distribution that have been modified
* since Generator was introduced. These are preserved using identical code
* to what was in NumPy 1.16 so that the stream of values generated by
* RandomState is not changed when there are changes that affect Generator.
*
* These functions should not be changed except if they contain code that
* cannot be compiled. They should not be changed for bug fixes, performance
* improvements that can change the values produced, or enhancements to precision.
*/
#include "include/legacy-distributions.h"
// 返回一个在 [0, 1) 范围内的随机双精度浮点数
static inline double legacy_double(aug_bitgen_t *aug_state) {
return aug_state->bit_generator->next_double(aug_state->bit_generator->state);
}
// 生成服从标准正态分布的随机数
double legacy_gauss(aug_bitgen_t *aug_state) {
if (aug_state->has_gauss) {
const double temp = aug_state->gauss;
aug_state->has_gauss = false;
aug_state->gauss = 0.0;
return temp;
} else {
double f, x1, x2, r2;
do {
x1 = 2.0 * legacy_double(aug_state) - 1.0;
x2 = 2.0 * legacy_double(aug_state) - 1.0;
r2 = x1 * x1 + x2 * x2;
} while (r2 >= 1.0 || r2 == 0.0);
/* Polar method, a more efficient version of the Box-Muller approach. */
f = sqrt(-2.0 * log(r2) / r2);
/* Keep for next call */
aug_state->gauss = f * x1;
aug_state->has_gauss = true;
return f * x2;
}
}
// 生成服从标准指数分布的随机数
double legacy_standard_exponential(aug_bitgen_t *aug_state) {
/* We use -log(1-U) since U is [0, 1) */
return -log(1.0 - legacy_double(aug_state));
}
// 生成服从标准 Gamma 分布的随机数
double legacy_standard_gamma(aug_bitgen_t *aug_state, double shape) {
double b, c;
double U, V, X, Y;
if (shape == 1.0) {
return legacy_standard_exponential(aug_state);
}
else if (shape == 0.0) {
return 0.0;
} else if (shape < 1.0) {
for (;;) {
U = legacy_double(aug_state);
V = legacy_standard_exponential(aug_state);
if (U <= 1.0 - shape) {
X = pow(U, 1. / shape);
if (X <= V) {
return X;
}
} else {
Y = -log((1 - U) / shape);
X = pow(1.0 - shape + shape * Y, 1. / shape);
if (X <= (V + Y)) {
return X;
}
}
}
} else {
b = shape - 1. / 3.;
c = 1. / sqrt(9 * b);
for (;;) {
do {
X = legacy_gauss(aug_state);
V = 1.0 + c * X;
} while (V <= 0.0);
V = V * V * V;
U = legacy_double(aug_state);
if (U < 1.0 - 0.0331 * (X * X) * (X * X))
return (b * V);
if (log(U) < 0.5 * X * X + b * (1. - V + log(V)))
return (b * V);
}
}
}
// 生成服从 Gamma 分布的随机数
double legacy_gamma(aug_bitgen_t *aug_state, double shape, double scale) {
return scale * legacy_standard_gamma(aug_state, shape);
}
// 生成服从 Pareto 分布的随机数
double legacy_pareto(aug_bitgen_t *aug_state, double a) {
return exp(legacy_standard_exponential(aug_state) / a) - 1;
}
// 生成服从 Weibull 分布的随机数
double legacy_weibull(aug_bitgen_t *aug_state, double a) {
if (a == 0.0) {
return 0.0;
}
return pow(legacy_standard_exponential(aug_state), 1. / a);
}
double legacy_power(aug_bitgen_t *aug_state, double a) {
// 使用 legacy_standard_exponential 函数计算指数分布,并结合 a 计算幂次方
return pow(1 - exp(-legacy_standard_exponential(aug_state)), 1. / a);
}
double legacy_chisquare(aug_bitgen_t *aug_state, double df) {
// 使用 legacy_standard_gamma 函数计算 Gamma 分布的值,并返回卡方分布的结果
return 2.0 * legacy_standard_gamma(aug_state, df / 2.0);
}
double legacy_rayleigh(bitgen_t *bitgen_state, double mode) {
// 使用 next_double 函数生成随机数,并计算 Rayleigh 分布的结果
return mode * sqrt(-2.0 * npy_log1p(-next_double(bitgen_state)));
}
double legacy_noncentral_chisquare(aug_bitgen_t *aug_state, double df,
double nonc) {
double out;
if (nonc == 0) {
// 如果非中心参数 nonc 为零,返回标准卡方分布的结果
return legacy_chisquare(aug_state, df);
}
if (1 < df) {
// 使用 legacy_gauss 函数生成高斯分布随机数,并计算非中心卡方分布的结果
const double Chi2 = legacy_chisquare(aug_state, df - 1);
const double n = legacy_gauss(aug_state) + sqrt(nonc);
return Chi2 + n * n;
} else {
const long i = random_poisson(aug_state->bit_generator, nonc / 2.0);
out = legacy_chisquare(aug_state, df + 2 * i);
/* 在此处插入 NaN 保护,以避免更改数据流 */
if (npy_isnan(nonc)){
// 如果 nonc 是 NaN,则返回 NaN
return NPY_NAN;
} else {
return out;
}
}
}
double legacy_noncentral_f(aug_bitgen_t *aug_state, double dfnum, double dfden,
double nonc) {
// 计算非中心 F 分布的结果
double t = legacy_noncentral_chisquare(aug_state, dfnum, nonc) * dfden;
return t / (legacy_chisquare(aug_state, dfden) * dfnum);
}
double legacy_wald(aug_bitgen_t *aug_state, double mean, double scale) {
double U, X, Y;
double mu_2l;
mu_2l = mean / (2 * scale);
Y = legacy_gauss(aug_state);
Y = mean * Y * Y;
X = mean + mu_2l * (Y - sqrt(4 * scale * Y + Y * Y));
U = legacy_double(aug_state);
if (U <= mean / (mean + X)) {
return X;
} else {
return mean * mean / X;
}
}
double legacy_normal(aug_bitgen_t *aug_state, double loc, double scale) {
// 返回正态分布的结果
return loc + scale * legacy_gauss(aug_state);
}
double legacy_lognormal(aug_bitgen_t *aug_state, double mean, double sigma) {
// 返回对数正态分布的结果
return exp(legacy_normal(aug_state, mean, sigma));
}
double legacy_standard_t(aug_bitgen_t *aug_state, double df) {
double num, denom;
num = legacy_gauss(aug_state);
denom = legacy_standard_gamma(aug_state, df / 2);
return sqrt(df / 2) * num / sqrt(denom);
}
int64_t legacy_negative_binomial(aug_bitgen_t *aug_state, double n, double p) {
// 使用 legacy_gamma 函数计算 Gamma 分布的值,并返回负二项分布的结果
double Y = legacy_gamma(aug_state, n, (1 - p) / p);
return (int64_t)random_poisson(aug_state->bit_generator, Y);
}
double legacy_standard_cauchy(aug_bitgen_t *aug_state) {
// 返回标准 Cauchy 分布的结果
return legacy_gauss(aug_state) / legacy_gauss(aug_state);
}
double legacy_beta(aug_bitgen_t *aug_state, double a, double b) {
double Ga, Gb;
if ((a <= 1.0) && (b <= 1.0)) {
double U, V, X, Y;
/* 使用 Johnk's 算法 */
// TODO: 补充 Johnk's 算法的详细解释
}
// 返回 Beta 分布的结果
return Ga / (Ga + Gb);
}
// 进入循环,使用 legacy_double 函数生成两个均匀分布的随机数 U 和 V
while (1) {
// 从增强状态生成一个双精度浮点数 U
U = legacy_double(aug_state);
// 从增强状态生成一个双精度浮点数 V
V = legacy_double(aug_state);
// 计算 X 和 Y,分别为 U 的 a 次方根和 V 的 b 次方根
X = pow(U, 1.0 / a);
Y = pow(V, 1.0 / b);
// 如果 X + Y 小于或等于 1.0
if ((X + Y) <= 1.0) {
// 如果 X + Y 大于 0
if (X + Y > 0) {
// 返回 X / (X + Y)
return X / (X + Y);
} else {
// 计算对数值 logX 和 logY,分别为 log(U) / a 和 log(V) / b
double logX = log(U) / a;
double logY = log(V) / b;
// 计算最大的 logX 和 logY
double logM = logX > logY ? logX : logY;
// 对 logX 和 logY 进行调整
logX -= logM;
logY -= logM;
// 返回 exp(logX - log(exp(logX) + exp(logY)))
return exp(logX - log(exp(logX) + exp(logY)));
}
}
}
} else {
// 使用增强状态和参数 a 生成 Ga
Ga = legacy_standard_gamma(aug_state, a);
// 使用增强状态和参数 b 生成 Gb
Gb = legacy_standard_gamma(aug_state, b);
// 返回 Ga / (Ga + Gb)
return Ga / (Ga + Gb);
}
}
// 计算自由度为 dfnum 和 dfden 的卡方分布变量比值
double legacy_f(aug_bitgen_t *aug_state, double dfnum, double dfden) {
return ((legacy_chisquare(aug_state, dfnum) * dfden) /
(legacy_chisquare(aug_state, dfden) * dfnum));
}
// 计算指数分布变量,使用给定的比例尺度
double legacy_exponential(aug_bitgen_t *aug_state, double scale) {
return scale * legacy_standard_exponential(aug_state);
}
// 原始的随机二项分布生成器
static RAND_INT_TYPE legacy_random_binomial_original(bitgen_t *bitgen_state,
double p,
RAND_INT_TYPE n,
binomial_t *binomial) {
double q;
if (p <= 0.5) {
// 当成功概率 p*n 小于等于 30 时使用反转法生成二项分布
if (p * n <= 30.0) {
return random_binomial_inversion(bitgen_state, n, p, binomial);
} else {
// 否则使用 BTPE 算法生成二项分布
return random_binomial_btpe(bitgen_state, n, p, binomial);
}
} else {
q = 1.0 - p;
// 当失败概率 q*n 小于等于 30 时使用反转法生成二项分布
if (q * n <= 30.0) {
return n - random_binomial_inversion(bitgen_state, n, q, binomial);
} else {
// 否则使用 BTPE 算法生成二项分布
return n - random_binomial_btpe(bitgen_state, n, q, binomial);
}
}
}
// 随机生成二项分布,返回整数类型结果
int64_t legacy_random_binomial(bitgen_t *bitgen_state, double p,
int64_t n, binomial_t *binomial) {
return (int64_t) legacy_random_binomial_original(bitgen_state, p,
(RAND_INT_TYPE) n,
binomial);
}
// HYP 算法生成超几何分布的随机变量
static RAND_INT_TYPE random_hypergeometric_hyp(bitgen_t *bitgen_state,
RAND_INT_TYPE good,
RAND_INT_TYPE bad,
RAND_INT_TYPE sample) {
RAND_INT_TYPE d1, k, z;
double d2, u, y;
d1 = bad + good - sample;
d2 = (double) MIN(bad, good);
y = d2;
k = sample;
while (y > 0.0) {
u = next_double(bitgen_state);
y -= (RAND_INT_TYPE) floor(u + y / (d1 + k));
k--;
if (k == 0)
break;
}
z = (RAND_INT_TYPE) (d2 - y);
if (good > bad)
z = sample - z;
return z;
}
// 常量定义,用于计算超几何分布的变量
/* D1 = 2*sqrt(2/e) */
/* D2 = 3 - 2*sqrt(3/e) */
#define D1 1.7155277699214135
#define D2 0.8989161620588988
/*
* 使用 HRUA 算法生成超几何分布的随机数
* 使用指定的随机数生成器状态、好品质物品数、坏品质物品数和样本数
*/
static RAND_INT_TYPE random_hypergeometric_hrua(bitgen_t *bitgen_state,
RAND_INT_TYPE good,
RAND_INT_TYPE bad,
RAND_INT_TYPE sample) {
// 计算好品质和坏品质中的最小值
RAND_INT_TYPE mingoodbad, maxgoodbad, popsize, m, d9;
double d4, d5, d6, d7, d8, d10, d11;
RAND_INT_TYPE Z;
double T, W, X, Y;
mingoodbad = MIN(good, bad); // 计算最小的好品质和坏品质数量
popsize = good + bad; // 总物品数量
maxgoodbad = MAX(good, bad); // 计算最大的好品质和坏品质数量
m = MIN(sample, popsize - sample); // 样本数和剩余物品数中较小的值
d4 = ((double)mingoodbad) / popsize; // 好品质物品在总物品中所占比例
d5 = 1.0 - d4; // 坏品质物品在总物品中所占比例
d6 = m * d4 + 0.5; // 期望值的修正
d7 = sqrt((double)(popsize - m) * sample * d4 * d5 / (popsize - 1) + 0.5); // 标准差的估算
d8 = D1 * d7 + D2; // 修正因子
d9 = (RAND_INT_TYPE)floor((double)(m + 1) * (mingoodbad + 1) / (popsize + 2)); // 计算随机数
d10 = (random_loggam(d9 + 1) + random_loggam(mingoodbad - d9 + 1) +
random_loggam(m - d9 + 1) + random_loggam(maxgoodbad - m + d9 + 1)); // 计算随机数
d11 = MIN(MIN(m, mingoodbad) + 1.0, floor(d6 + 16 * d7)); // 计算阈值
/* 16 for 16-decimal-digit precision in D1 and D2 */
/* 16 用于 D1 和 D2 的 16 位小数精度 */
while (1) {
X = next_double(bitgen_state); // 生成随机数
Y = next_double(bitgen_state); // 生成随机数
W = d6 + d8 * (Y - 0.5) / X; // 计算 W 值
/* fast rejection: */
/* 快速拒绝: */
if ((W < 0.0) || (W >= d11))
continue; // 如果 W 超出范围则重新生成
Z = (RAND_INT_TYPE)floor(W); // 转换 W 为整数
T = d10 - (random_loggam(Z + 1) + random_loggam(mingoodbad - Z + 1) +
random_loggam(m - Z + 1) + random_loggam(maxgoodbad - m + Z + 1)); // 计算 T 值
/* fast acceptance: */
/* 快速接受: */
if ((X * (4.0 - X) - 3.0) <= T)
break; // 如果接受条件成立则接受
/* fast rejection: */
/* 快速拒绝: */
if (X * (X - T) >= 1)
continue; // 如果拒绝条件成立则重新生成
/* log(0.0) is ok here, since always accept */
/* 这里使用 log(0.0) 是可以的,因为总是接受 */
if (2.0 * log(X) <= T)
break; /* acceptance */ // 如果接受条件成立则接受
}
/* this is a correction to HRUA* by Ivan Frohne in rv.py */
/* 这是在 rv.py 中对 HRUA* 的修正 */
if (good > bad)
Z = m - Z; // 根据好品质和坏品质的比较修正 Z 值
/* another fix from rv.py to allow sample to exceed popsize/2 */
/* 另一个修复,允许样本数超过总物品数量的一半 */
if (m < sample)
Z = good - Z; // 根据样本数和好品质修正 Z 值
return Z; // 返回生成的随机数
}
#undef D1
#undef D2
/*
* 使用原始超几何分布生成器
* 根据样本数的大小选择使用 HRUA 算法或者其他方法
*/
static RAND_INT_TYPE random_hypergeometric_original(bitgen_t *bitgen_state,
RAND_INT_TYPE good,
RAND_INT_TYPE bad,
RAND_INT_TYPE sample)
{
if (sample > 10) {
return random_hypergeometric_hrua(bitgen_state, good, bad, sample); // 如果样本数大于 10 使用 HRUA 算法
} else if (sample > 0) {
return random_hypergeometric_hyp(bitgen_state, good, bad, sample); // 如果样本数大于 0 使用其他算法
} else {
return 0; // 否则返回 0
}
}
/*
* 这是一个包装函数,与预期的模板匹配。
* 在旧生成器中,所有 int 类型都是 long,因此这里接受 int64 然后将其转换为 long。
* 这些值必须在 long 的范围内,并且这在函数外部检查。
*
* 其余部分仅包括返回类型
*/
// 使用给定的随机数生成器和参数,调用原始超几何分布生成函数来生成一个随机数
int64_t legacy_random_hypergeometric(bitgen_t *bitgen_state, int64_t good,
int64_t bad, int64_t sample) {
return (int64_t)random_hypergeometric_original(bitgen_state,
(RAND_INT_TYPE)good,
(RAND_INT_TYPE)bad,
(RAND_INT_TYPE)sample);
}
// 使用给定的随机数生成器和参数,调用原始泊松分布生成函数来生成一个随机数
int64_t legacy_random_poisson(bitgen_t *bitgen_state, double lam) {
return (int64_t)random_poisson(bitgen_state, lam);
}
// 使用给定的随机数生成器和参数,调用原始 Zipf 分布生成函数来生成一个随机数
int64_t legacy_random_zipf(bitgen_t *bitgen_state, double a) {
return (int64_t)random_zipf(bitgen_state, a);
}
// 使用给定的随机数生成器和参数,通过几何分布的反函数方法来生成一个随机数
static long legacy_geometric_inversion(bitgen_t *bitgen_state, double p) {
return (long)ceil(npy_log1p(-next_double(bitgen_state)) / log(1 - p));
}
// 使用给定的随机数生成器和参数,根据概率 p 的大小选择使用不同的几何分布生成方法来生成一个随机数
int64_t legacy_random_geometric(bitgen_t *bitgen_state, double p) {
if (p >= 0.333333333333333333333333) {
return (int64_t)random_geometric_search(bitgen_state, p);
} else {
return (int64_t)legacy_geometric_inversion(bitgen_state, p);
}
}
// 使用给定的随机数生成器和参数,调用原始多项分布生成函数来生成多项式分布的随机数
void legacy_random_multinomial(bitgen_t *bitgen_state, RAND_INT_TYPE n,
RAND_INT_TYPE *mnix, double *pix, npy_intp d,
binomial_t *binomial) {
return random_multinomial(bitgen_state, n, mnix, pix, d, binomial);
}
// 使用给定的随机数生成器和参数,根据 von Mises 分布的参数 mu 和 kappa 生成一个随机数
double legacy_vonmises(bitgen_t *bitgen_state, double mu, double kappa) {
double s;
double U, V, W, Y, Z;
double result, mod;
int neg;
if (npy_isnan(kappa)) {
return NPY_NAN;
}
if (kappa < 1e-8) {
return M_PI * (2 * next_double(bitgen_state) - 1);
} else {
/* with double precision rho is zero until 1.4e-8 */
if (kappa < 1e-5) {
/*
* second order taylor expansion around kappa = 0
* precise until relatively large kappas as second order is 0
*/
s = (1. / kappa + kappa);
} else {
/* Path for 1e-5 <= kappa <= 1e6 */
double r = 1 + sqrt(1 + 4 * kappa * kappa);
double rho = (r - sqrt(2 * r)) / (2 * kappa);
s = (1 + rho * rho) / (2 * rho);
}
while (1) {
U = next_double(bitgen_state);
Z = cos(M_PI * U);
W = (1 + s * Z) / (s + Z);
Y = kappa * (s - W);
V = next_double(bitgen_state);
/*
* V==0.0 is ok here since Y >= 0 always leads
* to accept, while Y < 0 always rejects
*/
if ((Y * (2 - Y) - V >= 0) || (log(Y / V) + 1 - Y >= 0)) {
break;
}
}
U = next_double(bitgen_state);
result = acos(W);
if (U < 0.5) {
result = -result;
}
result += mu;
neg = (result < 0);
mod = fabs(result);
mod = (fmod(mod + M_PI, 2 * M_PI) - M_PI);
if (neg) {
mod *= -1;
}
return mod;
}
}
// 使用给定的随机数生成器和参数,调用原始对数系列分布生成函数来生成一个随机数
int64_t legacy_logseries(bitgen_t *bitgen_state, double p) {
double q, r, U, V;
long result;
r = log(1.0 - p);
while (1) {
V = next_double(bitgen_state);
if (V >= p) {
return 1;
}
# 生成一个均匀分布的双精度随机数 U
U = next_double(bitgen_state);
# 计算概率 q,用于判断返回结果
q = 1.0 - exp(r * U);
# 如果 V 小于等于 q 的平方,则返回一个数值 result
if (V <= q * q) {
# 计算 result,即 log(V) / log(q) 的向下取整再加一
result = (long)floor(1 + log(V) / log(q));
# 如果 result 小于 1 或者 V 等于 0.0,则继续循环
if ((result < 1) || (V == 0.0)) {
continue;
} else {
# 否则返回 result 的整数类型值
return (int64_t)result;
}
}
# 如果 V 大于等于 q,则返回 1
if (V >= q) {
return 1;
}
# 其他情况返回 2
return 2;
}
注释:
# 这行代码表示一个代码块的结束,通常与一个以关键字开始(如if、for、def等)的代码块配对。
MT19937
Copyright (c) 2003-2005, Jean-Sebastien Roy (js@jeannot.org)
The rk_random and rk_seed functions algorithms and the original design of the Mersenne Twister RNG:
Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
-
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
-
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
-
The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Original algorithm for the implementation of rk_interval function from Richard J. Wagner's implementation of the Mersenne Twister RNG, optimised by Magnus Jonsson.
Constants used in the rk_double implementation by Isaku Wada.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
.\numpy\numpy\random\src\mt19937\mt19937-benchmark.c
/*
* 编译命令行示例(Windows下):
* cl mt19937-benchmark.c mt19937.c /Ox
* 测量执行时间命令(Windows下):
* Measure-Command { .\mt19937-benchmark.exe }
*
* 编译命令行示例(Linux下):
* gcc mt19937-benchmark.c mt19937.c -O3 -o mt19937-benchmark
* 运行可执行文件时间(Linux下):
* time ./mt19937-benchmark
*/
#include "mt19937.h" // 引入自定义的Mersenne Twister实现头文件
#include <inttypes.h> // 提供符合C99标准的整数类型
#include <stdio.h> // 标准输入输出库
#include <time.h> // 时间库,用于计时
#define Q 1000000000 // 定义循环次数
int main() {
int i;
uint32_t seed = 0x0; // 设置随机数生成器的种子
uint64_t sum = 0, count = 0; // 初始化总和与计数变量
mt19937_state state; // 定义Mersenne Twister状态变量
mt19937_seed(&state, seed); // 初始化Mersenne Twister随机数生成器
clock_t begin = clock(); // 记录开始时间
for (i = 0; i < Q; i++) {
sum += mt19937_next64(&state); // 生成64位随机数并累加到总和
count++; // 计数器加一
}
clock_t end = clock(); // 记录结束时间
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC; // 计算运行时间
printf("0x%" PRIx64 "\ncount: %" PRIu64 "\n", sum, count); // 输出总和和计数
printf("%" PRIu64 " randoms per second\n",
(uint64_t)(Q / time_spent) / 1000000 * 1000000); // 输出每秒生成的随机数数量
}
.\numpy\numpy\random\src\mt19937\mt19937-jump.c
/* 包含头文件 "mt19937-jump.h" 和 "mt19937.h" */
#include "mt19937-jump.h"
#include "mt19937.h"
/* 定义一个函数:获取多项式 pf 的第 i 个系数 */
unsigned long get_coef(unsigned long *pf, unsigned int deg) {
/* 检查多项式中第 i 个系数是否为 1 */
if ((pf[deg >> 5] & (LSB << (deg & 0x1ful))) != 0)
return (1);
else
return (0);
}
/* 定义一个函数:复制状态 */
void copy_state(mt19937_state *target_state, mt19937_state *state) {
int i;
/* 循环复制状态的关键数组和位置 */
for (i = 0; i < N; i++)
target_state->key[i] = state->key[i];
target_state->pos = state->pos;
}
/* 定义一个函数:生成下一个状态 */
void gen_next(mt19937_state *state) {
int num;
unsigned long y;
static unsigned long mag02[2] = {0x0ul, MATRIX_A};
num = state->pos;
if (num < N - M) {
/* 计算下一个状态的关键数组中的值 */
y = (state->key[num] & UPPER_MASK) | (state->key[num + 1] & LOWER_MASK);
state->key[num] = state->key[num + M] ^ (y >> 1) ^ mag02[y % 2];
state->pos++;
} else if (num < N - 1) {
y = (state->key[num] & UPPER_MASK) | (state->key[num + 1] & LOWER_MASK);
state->key[num] = state->key[num + (M - N)] ^ (y >> 1) ^ mag02[y % 2];
state->pos++;
} else if (num == N - 1) {
y = (state->key[N - 1] & UPPER_MASK) | (state->key[0] & LOWER_MASK);
state->key[N - 1] = state->key[M - 1] ^ (y >> 1) ^ mag02[y % 2];
state->pos = 0;
}
}
/* 定义一个函数:添加状态 */
void add_state(mt19937_state *state1, mt19937_state *state2) {
int i, pt1 = state1->pos, pt2 = state2->pos;
if (pt2 - pt1 >= 0) {
/* 将状态 state2 添加到状态 state1 */
for (i = 0; i < N - pt2; i++)
state1->key[i + pt1] ^= state2->key[i + pt2];
for (; i < N - pt1; i++)
state1->key[i + pt1] ^= state2->key[i + (pt2 - N)];
for (; i < N; i++)
state1->key[i + (pt1 - N)] ^= state2->key[i + (pt2 - N)];
} else {
for (i = 0; i < N - pt1; i++)
state1->key[i + pt1] ^= state2->key[i + pt2];
for (; i < N - pt2; i++)
state1->key[i + (pt1 - N)] ^= state2->key[i + pt2];
for (; i < N; i++)
state1->key[i + (pt1 - N)] ^= state2->key[i + (pt2 - N)];
}
}
/* 定义一个函数:使用标准 Horner 方法计算 pf(ss) */
void horner1(unsigned long *pf, mt19937_state *state) {
int i = MEXP - 1;
mt19937_state *temp;
/* 分配内存并初始化 temp 为 mt19937_state 类型的指针 */
temp = (mt19937_state *)calloc(1, sizeof(mt19937_state));
/* 通过 Horner 方法计算多项式的值 */
while (get_coef(pf, i) == 0)
i--;
if (i > 0) {
copy_state(temp, state);
gen_next(temp);
i--;
for (; i > 0; i--) {
if (get_coef(pf, i) != 0)
add_state(temp, state);
else
;
gen_next(temp);
}
if (get_coef(pf, 0) != 0)
add_state(temp, state);
else
;
} else if (i == 0)
copy_state(temp, state);
else
;
/* 将 temp 的状态复制到 state,释放 temp 的内存 */
copy_state(state, temp);
free(temp);
}
/* 定义一个函数:MT19937 跳跃状态 */
void mt19937_jump_state(mt19937_state *state) {
unsigned long *pf;
int i;
/* 分配内存并初始化 pf 数组 */
pf = (unsigned long *)calloc(P_SIZE, sizeof(unsigned long));
for (i = 0; i<P_SIZE; i++) {
pf[i] = poly_coef[i];
}
/* 如果状态的位置超过数组长度 N,则将位置设置为 0 */
if (state->pos >= N) {
state->pos = 0;
}
/* 使用 Horner 方法计算 pf(ss),更新状态 */
horner1(pf, state);
/* 释放 pf 数组的内存 */
free(pf);
}
.\numpy\numpy\random\src\mt19937\mt19937-jump.h
#pragma once
#include "mt19937.h"
#include <stdlib.h>
/* parameters for computing Jump */
#define W_SIZE 32 /* size of unsigned long */
#define MEXP 19937
#define P_SIZE ((MEXP / W_SIZE) + 1)
#define LSB 0x00000001UL
#define QQ 7
#define LL 128 /* LL = 2^(QQ) */
// 声明函数,用于在 Mersenne Twister 的状态上进行跳跃
void mt19937_jump_state(mt19937_state *state);
// 设置多项式系数的函数声明
void set_coef(unsigned long *pf, unsigned int deg, unsigned long v);
/*
* 2**128 step polynomial produced using the file mt19937-generate-jump-poly.c
* (randomgen) which is a modified version of minipoly_mt19937.c as distributed
* in
* http://www.math.sci.hiroshima-u.ac.jp/m-mat/MT/JUMP/jump_ahead_1.02.tar.gz
*
* These files are not part of NumPy.
*/
// 定义 624 个无符号长整型数组,用于表示 2^128 步长多项式的系数
static const unsigned long poly_coef[624] = {
1927166307UL, 3044056772UL, 2284297142UL, 2820929765UL, 651705945UL,
69149273UL, 3892165397UL, 2337412983UL, 1219880790UL, 3207074517UL,
3836784057UL, 189286826UL, 1049791363UL, 3916249550UL, 2942382547UL,
166392552UL, 861176918UL, 3246476411UL, 2302311555UL, 4273801148UL,
29196903UL, 1363664063UL, 3802562022UL, 2600400244UL, 3090369801UL,
4040416970UL, 1432485208UL, 3632558139UL, 4015816763UL, 3013316418UL,
551532385UL, 3592224467UL, 3479125595UL, 1195467127UL, 2391032553UL,
2393493419UL, 1482493632UL, 1625159565UL, 748389672UL, 4042774030UL,
2998615036UL, 3393119101UL, 2177492569UL, 2265897321UL, 2507383006UL,
3461498961UL, 2003319700UL, 1942857197UL, 1455226044UL, 4097545580UL,
529653268UL, 3204756480UL, 2486748289UL, 495294513UL, 3396001954UL,
2643963605UL, 2655404568UL, 3881604377UL, 624710790UL, 3443737948UL,
1941294296UL, 2139259604UL, 3368734020UL, 422436761UL, 3602810182UL,
1384691081UL, 3035786407UL, 2551797119UL, 537227499UL, 65486120UL,
642436100UL, 2023822537UL, 2515598203UL, 1122953367UL, 2882306242UL,
1743213032UL, 321965189UL, 336496623UL, 2436602518UL, 3556266590UL,
1055117829UL, 463541647UL, 743234441UL, 527083645UL, 2606668346UL,
2274046499UL, 2761475053UL, 2760669048UL, 2538258534UL, 487125077UL,
3365962306UL, 3604906217UL, 2714700608UL, 680709708UL, 2217161159UL,
1614899374UL, 3710119533UL, 3201300658UL, 3752620679UL, 2755041105UL,
3129723037UL, 1247297753UL, 2812642690UL, 4114340845UL, 3485092247UL,
2752814364UL, 3586551747UL, 4073138437UL, 3462966585UL, 2924318358UL,
4061374901UL, 3314086806UL, 2640385723UL, 744590670UL, 3007586513UL,
3959120371UL, 997207767UL, 3420235506UL, 2092400998UL, 3190305685UL,
60965738UL, 549507222UL, 3784354415UL, 3209279509UL, 1238863299UL,
2605037827UL, 178570440UL, 1743491299UL, 4079686640UL, 2136795825UL,
3435430548UL, 1679732443UL, 1835708342UL, 2159367000UL, 1924487218UL,
4059723674UL, 996192116UL, 2308091645UL, 1336281586UL, 674600050UL,
1642572529UL, 1383973289UL, 2202960007UL, 3165481279UL, 3385474038UL,
2501318550UL, 2671842890UL, 3084085109UL, 3475033915UL, 1551329147UL,
};
# 以下是一系列的无符号长整型数值,每个数值用UL后缀表示
4101397249UL, 1205851807UL, 3641536021UL, 3607635071UL, 1609126163UL,
2910426664UL, 3324508658UL, 4244311266UL, 254034382UL, 1258304384UL,
1914048768UL, 1358592011UL, 527610138UL, 3072108727UL, 4289413885UL,
1417001678UL, 2445445945UL, 896462712UL, 339855811UL, 3699378285UL,
2529457297UL, 3049459401UL, 2723472429UL, 2838633181UL, 2520397330UL,
3272339035UL, 1667003847UL, 3742634787UL, 942706520UL, 2301027215UL,
1907791250UL, 2306299096UL, 1021173342UL, 1539334516UL, 2907834628UL,
3199959207UL, 1556251860UL, 3642580275UL, 2355865416UL, 285806145UL,
867932457UL, 1177354172UL, 3291107470UL, 4022765061UL, 1613380116UL,
588147929UL, 650574324UL, 1236855601UL, 1371354511UL, 2085218212UL,
1203081931UL, 420526905UL, 1022192219UL, 2903287064UL, 2470845899UL,
3649873273UL, 2502333582UL, 3972385637UL, 4246356763UL, 199084157UL,
1567178788UL, 2107121836UL, 4293612856UL, 1902910177UL, 332397359UL,
83422598UL, 3614961721UL, 456321943UL, 2277615967UL, 2302518510UL,
3258315116UL, 2521897172UL, 3900282042UL, 4186973154UL, 3146532165UL,
2299685029UL, 3889120948UL, 1293301857UL, 187455105UL, 3395849230UL,
913321567UL, 3093513909UL, 1440944571UL, 1923481911UL, 338680924UL,
1204882963UL, 2739724491UL, 2886241328UL, 2408907774UL, 1299817192UL,
2474012871UL, 45400213UL, 553186784UL, 134558656UL, 2180943666UL,
2870807589UL, 76511085UL, 3053566760UL, 2516601415UL, 4172865902UL,
1751297915UL, 1251975234UL, 2964780642UL, 1412975316UL, 2739978478UL,
2171013719UL, 637935041UL, 975972384UL, 3044407449UL, 3111425639UL,
1938684970UL, 2860857400UL, 13419586UL, 2772079268UL, 3484375614UL,
3184054178UL, 159924837UL, 1386213021UL, 2765617231UL, 2523689118UL,
1283505218UL, 3510789588UL, 4125878259UL, 2990287597UL, 2152014833UL,
3084155970UL, 2815101609UL, 1932985704UL, 114887365UL, 1712687646UL,
2550515629UL, 3299051916UL, 2022747614UL, 2143630992UL, 2244188960UL,
3309469192UL, 3234358520UL, 800720365UL, 3278176634UL, 554357439UL,
2415629802UL, 1620877315UL, 2389462898UL, 2229691332UL, 1007748450UL,
1966873768UL, 2264971043UL, 1214524156UL, 346854700UL, 3471905342UL,
3984889660UL, 4034246840UL, 216712649UL, 4027196762UL, 3754772604UL,
2121785562UL, 2347070732UL, 7457687UL, 1443375102UL, 683948143UL,
2940226032UL, 3211475670UL, 2836507357UL, 774899409UL, 1588968308UL,
780438009UL, 3278878781UL, 2217181540UL, 2184194887UL, 1642129086UL,
69346830UL, 297114710UL, 3841068188UL, 2631265450UL, 4167492314UL,
2613519651UL, 1388582503UL, 2171556668UL, 1201873758UL, 2698772382UL,
207791958UL, 3936134563UL, 3725025702UL, 3306317801UL, 1055730422UL,
4069230694UL, 1767821343UL, 4252407395UL, 2422583118UL, 3158834399UL,
3754582617UL, 1112422556UL, 376187931UL, 3137549150UL, 712221089UL,
3300799453UL, 3868250200UL, 1165257666UL, 2494837767UL, 131304831UL,
1619349427UL, 1958236644UL, 3678218946UL, 3651007751UL, 2261987899UL,
1567368524UL, 2193599522UL, 3034394674UL, 2994602555UL, 3072727647UL,
889094521UL, 1089692095UL, 1822324824UL, 3876999182UL, 1703361286UL,
902229515UL, 4213728487UL, 3838170364UL, 672727494UL, 2240733828UL,
3858539469UL, 1149254245UL, 4166055926UL, 4193525313UL, 1709921593UL,
2278290377UL, 3190784116UL, 2919588882UL, 1012709717UL, 3640562031UL,
2931984863UL, 3515665246UL, 250577343UL, 1147230194UL, 1183856202UL,
3734511989UL, 3243867808UL, 3499383067UL, 2985115159UL, 2036821626UL,
3298159553UL, 2726542838UL, 1686910320UL, 1778823772UL, 965412224UL,
233509772UL, 3843098861UL, 1312622954UL, 500855830UL, 2950562091UL,
1915683607UL, 3405781138UL, 596073719UL, 2195150546UL, 3381728478UL,
546426436UL, 3527890868UL, 2324975353UL, 2241074266UL, 3992514859UL,
2576108287UL, 4077653225UL, 2632319392UL, 3127212632UL, 917000669UL,
2498161805UL, 3980835128UL, 2259526768UL, 1083920509UL, 1187452089UL,
97018536UL, 3056075838UL, 2059706760UL, 2373335692UL, 182196406UL,
2136713111UL, 1762080153UL, 1572125803UL, 1145919955UL, 1023966754UL,
3921694345UL, 1632005969UL, 1418372326UL, 354407429UL, 2438288265UL,
1620072033UL, 1586320921UL, 1044153697UL, 969324572UL, 613487980UL,
4230993062UL, 397726764UL, 2194259193UL, 735511759UL, 2066049260UL,
88093248UL, 1562536153UL, 2114157419UL, 3630951546UL, 589238503UL,
3120654384UL, 2521793793UL, 2746692127UL, 2557723425UL, 889897693UL,
2778878177UL, 643269509UL, 3342389831UL, 19218890UL, 3442706236UL,
3314581273UL, 3503147052UL, 1546343434UL, 1448529060UL, 529038801UL,
2748942264UL, 2213019208UL, 111314040UL, 2488697563UL, 1180642808UL,
2605272289UL, 4207476668UL, 1502558669UL, 2972370981UL, 4204339995UL,
1046225278UL, 992840610UL, 3847290298UL, 2387673094UL, 2221565747UL,
1045901716UL, 3997739302UL, 1556952765UL, 1103336648UL, 279418400UL,
2711316466UL, 2336215718UL, 2317900806UL, 974624729UL, 909575434UL,
1675610631UL, 1922393214UL, 2054896570UL, 3197007361UL, 3932554569UL,
1008619802UL, 3349254938UL, 113511461UL, 932630384UL, 2098759268UL,
3436837432UL, 3119972401UL, 1612590197UL, 2281609013UL, 4174211248UL,
4016332246UL, 2097525539UL, 1398632760UL, 1543697535UL, 2419227174UL,
1676465074UL, 2882923045UL, 23216933UL, 808195649UL, 3690720147UL,
484419260UL, 2254772642UL, 2975434733UL, 288528113UL, 204598404UL,
589968818UL, 3021152400UL, 2463155141UL, 1397846755UL, 157285579UL,
4230258857UL, 2469135246UL, 625357422UL, 3435224647UL, 465239124UL,
1022535736UL, 2823317040UL, 274194469UL, 2214966446UL, 3661001613UL,
518802547UL, 2293436304UL, 1335881988UL, 2247010176UL, 1856732584UL,
1088028094UL, 1877563709UL, 1015352636UL, 1700817932UL, 2960695857UL,
1882229300UL, 1666906557UL, 1838841022UL, 3983797810UL, 1667630361UL,
注释:这段代码是一系列的无符号长整数(unsigned long),用于某种数据处理或计算中。
# 以下是一个长整型数组的定义,存储了多个无符号长整型数值
385998221UL, 241341791UL, 403550441UL, 2629200403UL, 3552759102UL,
2029750442UL, 2247999048UL, 2726665298UL, 2507798776UL, 2419064129UL,
1266444923UL, 526255242UL, 2384866697UL, 1886200981UL, 3954956408UL,
2171436866UL, 2295200753UL, 1047315850UL, 1967809707UL, 2860382973UL,
3918334466UL, 3057439479UL, 952682588UL, 1925559679UL, 3112119050UL,
3833190964UL, 1430139895UL, 2089165610UL, 3009202424UL, 3989186157UL,
3395807230UL, 347600520UL, 120428923UL, 3017004655UL, 1384933954UL,
303039929UL, 234010146UL, 2278760249UL, 315514836UL, 3987659575UL,
1239335668UL, 2387869477UL, 3885908826UL, 1983922602UL, 698609264UL,
3009002846UL, 1520611399UL, 809159940UL, 3089771783UL, 374838722UL,
2789914419UL, 2500831937UL, 3751970335UL, 4279852547UL, 2362894437UL,
1588814060UL, 1671213155UL, 434218829UL, 2126587176UL, 2002526422UL,
2756464095UL, 141700479UL, 2965974322UL, 2211530172UL, 992085992UL,
1943691492UL, 2705131817UL, 2519208889UL, 1938768395UL, 3949294294UL,
354046666UL, 2158272751UL, 602858583UL, 0UL};
# 数组末尾的 0UL 表示数组的结束标志,通常用于指示数组的结束或者空指针
.\numpy\numpy\random\src\mt19937\mt19937-test-data-gen.c
/*
* 生成测试用的 CSV 文件
*
* cl mt19937-test-data-gen.c randomkit.c
* -IC:\Anaconda\Lib\site-packages\numpy\_core\include -IC:\Anaconda\include
* Advapi32.lib Kernel32.lib C:\Anaconda\libs\python36.lib -DRK_NO_WINCRYPT=1
*
*/
#include "randomkit.h" // 包含随机数生成器的头文件
#include <inttypes.h> // 提供整数格式转换宏
#include <stdio.h> // 标准输入输出库
#define N 1000 // 定义生成随机数的数量
int main() {
uint64_t sum = 0; // 用于计算随机数总和的变量
uint32_t seed = 0xDEADBEAF; // 随机数种子
int i; // 循环变量
rk_state state; // 随机数生成器状态结构体
rk_seed(seed, &state); // 初始化随机数生成器状态
uint64_t store[N]; // 存储生成的随机数数组
// 生成随机数并存储到数组中
for (i = 0; i < N; i++) {
store[i] = (uint64_t)rk_random(&state);
}
FILE *fp;
fp = fopen("mt19937-testset-1.csv", "w"); // 打开第一个 CSV 文件进行写操作
if (fp == NULL) {
printf("Couldn't open file\n"); // 打开文件失败时输出错误信息
return -1; // 返回错误状态码
}
fprintf(fp, "seed, 0x%" PRIx32 "\n", seed); // 将种子写入文件
// 将随机数和其对应的索引写入文件
for (i = 0; i < N; i++) {
fprintf(fp, "%d, 0x%" PRIx64 "\n", i, store[i]);
if (i == 999) {
printf("%d, 0x%" PRIx64 "\n", i, store[i]); // 打印最后一个随机数的值
}
}
fclose(fp); // 关闭文件
seed = 0; // 重置种子
rk_seed(seed, &state); // 使用新种子重新初始化随机数生成器状态
// 重新生成随机数并存储到数组中
for (i = 0; i < N; i++) {
store[i] = (uint64_t)rk_random(&state);
}
fp = fopen("mt19937-testset-2.csv", "w"); // 打开第二个 CSV 文件进行写操作
if (fp == NULL) {
printf("Couldn't open file\n"); // 打开文件失败时输出错误信息
return -1; // 返回错误状态码
}
fprintf(fp, "seed, 0x%" PRIx32 "\n", seed); // 将新种子写入文件
// 将新生成的随机数和其对应的索引写入文件
for (i = 0; i < N; i++) {
fprintf(fp, "%d, 0x%" PRIx64 "\n", i, store[i]);
if (i == 999) {
printf("%d, 0x%" PRIx64 "\n", i, store[i]); // 打印最后一个随机数的值
}
}
fclose(fp); // 关闭文件
}
.\numpy\numpy\random\src\mt19937\mt19937.c
/*
* Seed the mt19937 PRNG state with a given seed value.
* The function initializes the state's key array using Knuth's PRNG method.
* It iterates over the state array length (RK_STATE_LEN) to populate each element.
*/
void mt19937_seed(mt19937_state *state, uint32_t seed) {
int pos;
seed &= 0xffffffffUL;
/* Knuth's PRNG as used in the Mersenne Twister reference implementation */
for (pos = 0; pos < RK_STATE_LEN; pos++) {
state->key[pos] = seed;
seed = (1812433253UL * (seed ^ (seed >> 30)) + pos + 1) & 0xffffffffUL;
}
state->pos = RK_STATE_LEN;
}
/*
* Initialize the mt19937 PRNG state using a single seed value.
* This function initializes the state's key array (mt) based on the provided seed value.
* It uses a modified version of Knuth's PRNG algorithm to fill the key array.
*/
static void init_genrand(mt19937_state *state, uint32_t s) {
int mti;
uint32_t *mt = state->key;
mt[0] = s & 0xffffffffUL;
for (mti = 1; mti < RK_STATE_LEN; mti++) {
/*
* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier.
* In the previous versions, MSBs of the seed affect
* only MSBs of the array mt[].
* 2002/01/09 modified by Makoto Matsumoto
*/
mt[mti] = (1812433253UL * (mt[mti - 1] ^ (mt[mti - 1] >> 30)) + mti);
/* for > 32 bit machines */
mt[mti] &= 0xffffffffUL;
}
state->pos = mti;
return;
}
/*
* Initialize the mt19937 PRNG state using an array of keys.
* This function initializes the state's key array (mt) using an array of keys.
* It first initializes the state with a default seed value.
* Then it mixes the provided keys with the initialized state using a non-linear mixing function.
*/
void mt19937_init_by_array(mt19937_state *state, uint32_t *init_key,
int key_length) {
/* was signed in the original code. RDH 12/16/2002 */
int i = 1;
int j = 0;
uint32_t *mt = state->key;
int k;
init_genrand(state, 19650218UL);
k = (RK_STATE_LEN > key_length ? RK_STATE_LEN : key_length);
for (; k; k--) {
/* non linear */
mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 30)) * 1664525UL)) +
init_key[j] + j;
/* for > 32 bit machines */
mt[i] &= 0xffffffffUL;
i++;
j++;
if (i >= RK_STATE_LEN) {
mt[0] = mt[RK_STATE_LEN - 1];
i = 1;
}
if (j >= key_length) {
j = 0;
}
}
for (k = RK_STATE_LEN - 1; k; k--) {
mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 30)) * 1566083941UL)) -
i; /* non linear */
mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
i++;
if (i >= RK_STATE_LEN) {
mt[0] = mt[RK_STATE_LEN - 1];
i = 1;
}
}
mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
}
/*
* Generate the next batch of random numbers using mt19937 PRNG state.
* This function generates the next set of random numbers using the mt19937 PRNG algorithm.
* It advances the state's key array (mt) using a specific mixing function.
*/
void mt19937_gen(mt19937_state *state) {
uint32_t y;
int i;
for (i = 0; i < N - M; i++) {
y = (state->key[i] & UPPER_MASK) | (state->key[i + 1] & LOWER_MASK);
state->key[i] = state->key[i + M] ^ (y >> 1) ^ (-(y & 1) & MATRIX_A);
}
for (; i < N - 1; i++) {
y = (state->key[i] & UPPER_MASK) | (state->key[i + 1] & LOWER_MASK);
state->key[i] = state->key[i + (M - N)] ^ (y >> 1) ^ (-(y & 1) & MATRIX_A);
}
y = (state->key[N - 1] & UPPER_MASK) | (state->key[0] & LOWER_MASK);
state->key[N - 1] = state->key[M - 1] ^ (y >> 1) ^ (-(y & 1) & MATRIX_A);
state->pos = 0;
}
/* Declaration of inline functions for mt19937 PRNG */
extern inline uint64_t mt19937_next64(mt19937_state *state);
extern inline uint32_t mt19937_next32(mt19937_state *state);
extern inline double mt19937_next_double(mt19937_state *state);
# 调用 mt19937_jump_state 函数,使得 mt19937_state 对象跳转到下一个状态
void mt19937_jump(mt19937_state *state) { mt19937_jump_state(state); }
.\numpy\numpy\random\src\mt19937\mt19937.h
#pragma once
// 防止头文件重复包含
#include <math.h>
// 包含数学库函数
#include <stdint.h>
// 包含标准整数类型定义
#if defined(_WIN32) && !defined (__MINGW32__)
#define inline __forceinline
#endif
// 如果编译环境为 Windows,并且非 MinGW,定义 inline 为 __forceinline
#define RK_STATE_LEN 624
// 定义 Mersenne Twister 状态数组的长度为 624
#define N 624
#define M 397
#define MATRIX_A 0x9908b0dfUL
#define UPPER_MASK 0x80000000UL
#define LOWER_MASK 0x7fffffffUL
// 定义 Mersenne Twister 算法中使用的常量
typedef struct s_mt19937_state {
uint32_t key[RK_STATE_LEN];
// 用于存储 Mersenne Twister 状态的数组
int pos;
// 当前状态数组中的位置指针
} mt19937_state;
// 定义 Mersenne Twister 状态结构体
extern void mt19937_seed(mt19937_state *state, uint32_t seed);
// 外部函数声明:初始化 Mersenne Twister 状态数组
extern void mt19937_gen(mt19937_state *state);
// 外部函数声明:生成 Mersenne Twister 的下一个状态
/* Slightly optimized reference implementation of the Mersenne Twister */
// 稍微优化的 Mersenne Twister 参考实现
static inline uint32_t mt19937_next(mt19937_state *state) {
uint32_t y;
if (state->pos == RK_STATE_LEN) {
// 如果当前位置指针达到状态数组长度
mt19937_gen(state);
// 生成下一个 Mersenne Twister 状态数组
}
y = state->key[state->pos++];
/* Tempering */
// 状态数据的调整
y ^= (y >> 11);
y ^= (y << 7) & 0x9d2c5680UL;
y ^= (y << 15) & 0xefc60000UL;
y ^= (y >> 18);
return y;
// 返回生成的随机数
}
extern void mt19937_init_by_array(mt19937_state *state, uint32_t *init_key,
int key_length);
// 外部函数声明:通过数组初始化 Mersenne Twister 状态数组
static inline uint64_t mt19937_next64(mt19937_state *state) {
return (uint64_t)mt19937_next(state) << 32 | mt19937_next(state);
// 返回生成的 64 位随机数
}
static inline uint32_t mt19937_next32(mt19937_state *state) {
return mt19937_next(state);
// 返回生成的 32 位随机数
}
static inline double mt19937_next_double(mt19937_state *state) {
int32_t a = mt19937_next(state) >> 5, b = mt19937_next(state) >> 6;
// 使用生成的随机数生成双精度浮点数
return (a * 67108864.0 + b) / 9007199254740992.0;
}
void mt19937_jump(mt19937_state *state);
// 外部函数声明:进行 Mersenne Twister 的跳跃操作
.\numpy\numpy\random\src\mt19937\randomkit.c
/* Random kit 1.3 */
/* static char const rcsid[] =
"@(#) $Jeannot: randomkit.c,v 1.28 2005/07/21 22:14:09 js Exp $"; */
#ifdef _WIN32
/*
* Windows
* XXX: we have to use this ugly defined(__GNUC__) because it is not easy to
* detect the compiler used in distutils itself
*/
#if (defined(__GNUC__) && defined(NPY_NEEDS_MINGW_TIME_WORKAROUND))
/*
* FIXME: ideally, we should set this to the real version of MSVCRT. We need
* something higher than 0x601 to enable _ftime64 and co
*/
#define __MSVCRT_VERSION__ 0x0700
#include <sys/timeb.h>
#include <time.h>
/*
* mingw msvcr lib import wrongly export _ftime, which does not exist in the
* actual msvc runtime for version >= 8; we make it an alias to _ftime64, which
* is available in those versions of the runtime
*/
#define _FTIME(x) _ftime64((x))
#else
#include <sys/timeb.h>
#include <time.h>
#define _FTIME(x) _ftime((x))
#endif
#ifndef RK_NO_WINCRYPT
/* Windows crypto */
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0400
#endif
#include <wincrypt.h>
#include <windows.h>
#endif
/*
* Do not move this include. randomkit.h must be included
* after windows timeb.h is included.
*/
#include "randomkit.h"
#else
/* Unix */
#include "randomkit.h"
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#endif
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <math.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef RK_DEV_URANDOM
#define RK_DEV_URANDOM "/dev/urandom"
#endif
#ifndef RK_DEV_RANDOM
#define RK_DEV_RANDOM "/dev/random"
#endif
char *rk_strerror[RK_ERR_MAX] = {"no error", "random device unvavailable"};
/* static functions */
/*
* 哈希函数,使用Thomas Wang的32位整数哈希算法
*/
static unsigned long rk_hash(unsigned long key);
/*
* 初始化随机数发生器状态,使用Knuth的PRNG算法
*/
void rk_seed(unsigned long seed, rk_state *state) {
int pos;
seed &= 0xffffffffUL;
/* Knuth's PRNG as used in the Mersenne Twister reference implementation */
for (pos = 0; pos < RK_STATE_LEN; pos++) {
state->key[pos] = seed;
seed = (1812433253UL * (seed ^ (seed >> 30)) + pos + 1) & 0xffffffffUL;
}
state->pos = RK_STATE_LEN;
state->gauss = 0;
state->has_gauss = 0;
state->has_binomial = 0;
}
/*
* Thomas Wang的32位整数哈希函数
*/
unsigned long rk_hash(unsigned long key) {
key += ~(key << 15);
key ^= (key >> 10);
key += (key << 3);
key ^= (key >> 6);
key += ~(key << 11);
key ^= (key >> 16);
return key;
}
/*
* 从系统随机源填充种子数据,确保生成的种子不为零
*/
rk_error rk_randomseed(rk_state *state) {
#ifndef _WIN32
struct timeval tv;
#else
struct _timeb tv;
#endif
int i;
if (rk_devfill(state->key, sizeof(state->key), 0) == RK_NOERR) {
/* ensures non-zero key */
state->key[0] |= 0x80000000UL;
state->pos = RK_STATE_LEN;
state->gauss = 0;
state->has_gauss = 0;
state->has_binomial = 0;
for (i = 0; i < 624; i++) {
state->key[i] &= 0xffffffffUL;
}
return RK_NOERR;
}
#ifndef _WIN32
gettimeofday(&tv, NULL);
rk_seed(rk_hash(getpid()) ^ rk_hash(tv.tv_sec) ^ rk_hash(tv.tv_usec) ^
rk_hash(clock()),
state);
#else
_FTIME(&tv); // 获取当前时间到结构体 tv 中
rk_seed(rk_hash(tv.time) ^ rk_hash(tv.millitm) ^ rk_hash(clock()), state); // 使用当前时间、毫秒部分和系统时钟的哈希值作为种子初始化随机数发生器
#endif
return RK_ENODEV; // 返回设备不存在错误码
}
/* Magic Mersenne Twister constants */
#define N 624 // MT 算法中使用的数组长度
#define M 397 // MT 算法中用于生成随机数的参数
#define MATRIX_A 0x9908b0dfUL // MT 算法中的矩阵 A 常数
#define UPPER_MASK 0x80000000UL // MT 算法中的掩码,用于获取上半部分位
#define LOWER_MASK 0x7fffffffUL // MT 算法中的掩码,用于获取下半部分位
/*
* Slightly optimised reference implementation of the Mersenne Twister
* Note that regardless of the precision of long, only 32 bit random
* integers are produced
*/
unsigned long rk_random(rk_state *state) {
unsigned long y;
if (state->pos == RK_STATE_LEN) {
int i;
for (i = 0; i < N - M; i++) {
y = (state->key[i] & UPPER_MASK) | (state->key[i + 1] & LOWER_MASK);
state->key[i] = state->key[i + M] ^ (y >> 1) ^ (-(y & 1) & MATRIX_A);
}
for (; i < N - 1; i++) {
y = (state->key[i] & UPPER_MASK) | (state->key[i + 1] & LOWER_MASK);
state->key[i] =
state->key[i + (M - N)] ^ (y >> 1) ^ (-(y & 1) & MATRIX_A);
}
y = (state->key[N - 1] & UPPER_MASK) | (state->key[0] & LOWER_MASK);
state->key[N - 1] = state->key[M - 1] ^ (y >> 1) ^ (-(y & 1) & MATRIX_A);
state->pos = 0;
}
y = state->key[state->pos++];
/* Tempering */
y ^= (y >> 11); // 随机数的一种混淆操作
y ^= (y << 7) & 0x9d2c5680UL; // 随机数的一种混淆操作
y ^= (y << 15) & 0xefc60000UL; // 随机数的一种混淆操作
y ^= (y >> 18); // 随机数的一种混淆操作
return y; // 返回生成的随机数
}
/*
* Returns an unsigned 64 bit random integer.
*/
static inline npy_uint64 rk_uint64(rk_state *state) {
npy_uint64 upper = (npy_uint64)rk_random(state) << 32; // 生成一个 64 位的随机数,取高 32 位
npy_uint64 lower = (npy_uint64)rk_random(state); // 生成一个 64 位的随机数,取低 32 位
return upper | lower; // 合并高位和低位得到完整的 64 位随机数
}
/*
* Returns an unsigned 32 bit random integer.
*/
static inline npy_uint32 rk_uint32(rk_state *state) {
return (npy_uint32)rk_random(state); // 生成一个 32 位的随机数
}
/*
* Fills an array with cnt random npy_uint64 between off and off + rng
* inclusive. The numbers wrap if rng is sufficiently large.
*/
void rk_random_uint64(npy_uint64 off, npy_uint64 rng, npy_intp cnt,
npy_uint64 *out, rk_state *state) {
npy_uint64 val, mask = rng;
npy_intp i;
if (rng == 0) {
for (i = 0; i < cnt; i++) {
out[i] = off;
}
return;
}
/* Smallest bit mask >= max */
mask |= mask >> 1;
mask |= mask >> 2;
mask |= mask >> 4;
mask |= mask >> 8;
mask |= mask >> 16;
mask |= mask >> 32;
for (i = 0; i < cnt; i++) {
if (rng <= 0xffffffffUL) {
while ((val = (rk_uint32(state) & mask)) > rng)
;
} else {
while ((val = (rk_uint64(state) & mask)) > rng)
;
}
out[i] = off + val; // 将生成的随机数放入数组中
}
}
/*
* Fills an array with cnt random npy_uint32 between off and off + rng
* inclusive. The numbers wrap if rng is sufficiently large.
*/
void rk_random_uint32(npy_uint32 off, npy_uint32 rng, npy_intp cnt,
npy_uint32 *out, rk_state *state) {
npy_uint32 val, mask = rng;
npy_intp i;
if (rng == 0) {
for (i = 0; i < cnt; i++) {
out[i] = off;
}
return;
}
return;
}
/* Smallest bit mask >= max */
// 初始化掩码为最小的大于等于 max 的 2 的幂次掩码
mask |= mask >> 1;
mask |= mask >> 2;
mask |= mask >> 4;
mask |= mask >> 8;
mask |= mask >> 16;
// 对于每个要生成的随机数
for (i = 0; i < cnt; i++) {
// 生成在指定范围内的随机数,确保其不大于 rng
while ((val = (rk_uint32(state) & mask)) > rng)
; // 循环直到生成的随机数满足要求
// 将生成的随机数加上偏移 off,存入输出数组
out[i] = off + val;
}
/*
* Fills an array with cnt random npy_uint16 between off and off + rng
* inclusive. The numbers wrap if rng is sufficiently large.
*/
void rk_random_uint16(npy_uint16 off, npy_uint16 rng, npy_intp cnt,
npy_uint16 *out, rk_state *state) {
npy_uint16 val, mask = rng;
npy_intp i;
npy_uint32 buf;
int bcnt = 0;
if (rng == 0) {
for (i = 0; i < cnt; i++) {
out[i] = off;
}
return;
}
/* Smallest bit mask >= max */
mask |= mask >> 1;
mask |= mask >> 2;
mask |= mask >> 4;
mask |= mask >> 8;
for (i = 0; i < cnt; i++) {
do {
if (!bcnt) {
buf = rk_uint32(state); // 从状态结构中获取一个32位随机数
bcnt = 1; // 设置位计数器为1
} else {
buf >>= 16; // 右移16位,准备获取下一个16位的随机数
bcnt--;
}
val = (npy_uint16)buf & mask; // 将buf与掩码进行按位与运算,获取符合范围的随机数
} while (val > rng); // 如果随机数超出范围则重新生成
out[i] = off + val; // 将生成的随机数加上偏移量存入数组
}
}
/*
* Fills an array with cnt random npy_uint8 between off and off + rng
* inclusive. The numbers wrap if rng is sufficiently large.
*/
void rk_random_uint8(npy_uint8 off, npy_uint8 rng, npy_intp cnt, npy_uint8 *out,
rk_state *state) {
npy_uint8 val, mask = rng;
npy_intp i;
npy_uint32 buf;
int bcnt = 0;
if (rng == 0) {
for (i = 0; i < cnt; i++) {
out[i] = off;
}
return;
}
/* Smallest bit mask >= max */
mask |= mask >> 1;
mask |= mask >> 2;
mask |= mask >> 4;
for (i = 0; i < cnt; i++) {
do {
if (!bcnt) {
buf = rk_uint32(state); // 从状态结构中获取一个32位随机数
bcnt = 3; // 设置位计数器为3
} else {
buf >>= 8; // 右移8位,准备获取下一个8位的随机数
bcnt--;
}
val = (npy_uint8)buf & mask; // 将buf与掩码进行按位与运算,获取符合范围的随机数
} while (val > rng); // 如果随机数超出范围则重新生成
out[i] = off + val; // 将生成的随机数加上偏移量存入数组
}
}
/*
* Fills an array with cnt random npy_bool between off and off + rng
* inclusive.
*/
void rk_random_bool(npy_bool off, npy_bool rng, npy_intp cnt, npy_bool *out,
rk_state *state) {
npy_intp i;
npy_uint32 buf;
int bcnt = 0;
if (rng == 0) {
for (i = 0; i < cnt; i++) {
out[i] = off;
}
return;
}
/* If we reach here rng and mask are one and off is zero */
assert(rng == 1 && off == 0); // 断言rng为1且off为0
for (i = 0; i < cnt; i++) {
if (!bcnt) {
buf = rk_uint32(state); // 从状态结构中获取一个32位随机数
bcnt = 31; // 设置位计数器为31
} else {
buf >>= 1; // 右移1位,准备获取下一个1位的随机数
bcnt--;
}
out[i] = (buf & 0x00000001) != 0; // 将buf的最低位作为布尔值存入数组
}
}
long rk_long(rk_state *state) { return rk_ulong(state) >> 1; } // 返回一个长整型随机数
unsigned long rk_ulong(rk_state *state) {
#if ULONG_MAX <= 0xffffffffUL
return rk_random(state); // 如果unsigned long的最大值小于等于0xffffffffUL,则返回一个随机数
#else
return (rk_random(state) << 32) | (rk_random(state)); // 否则返回一个64位随机数
#endif
}
unsigned long rk_interval(unsigned long max, rk_state *state) {
unsigned long mask = max, value;
if (max == 0) {
return 0;
}
/* Smallest bit mask >= max */
mask |= mask >> 1;
mask |= mask >> 2;
mask |= mask >> 4;
mask |= mask >> 8;
mask |= mask >> 16;
#if ULONG_MAX > 0xffffffffUL
mask |= mask >> 32;
#endif
/* Search a random value in [0..mask] <= max */
#if ULONG_MAX > 0xffffffffUL
if (max <= 0xffffffffUL) {
while ((value = (rk_random(state) & mask)) > max)
;
} else {
while ((value = (rk_ulong(state) & mask)) > max)
;
}
#else
while ((value = (rk_ulong(state) & mask)) > max)
;
#endif
return value; // 返回找到的随机值
}
# 当条件 ((value = (rk_ulong(state) & mask)) > max) 满足时执行循环,直到条件不再满足。
while ((value = (rk_ulong(state) & mask)) > max)
;
#else
while ((value = (rk_ulong(state) & mask)) > max)
;
#endif
return value;
}
double rk_double(rk_state *state) {
/* shifts : 67108864 = 0x4000000, 9007199254740992 = 0x20000000000000 */
// 生成两个随机数并进行右移,得到a和b
long a = rk_random(state) >> 5, b = rk_random(state) >> 6;
// 返回双精度浮点数,使用预先定义的常数进行计算
return (a * 67108864.0 + b) / 9007199254740992.0;
}
void rk_fill(void *buffer, size_t size, rk_state *state) {
unsigned long r;
unsigned char *buf = buffer;
// 对于每个4字节的块,填充随机数据
for (; size >= 4; size -= 4) {
r = rk_random(state);
*(buf++) = r & 0xFF;
*(buf++) = (r >> 8) & 0xFF;
*(buf++) = (r >> 16) & 0xFF;
*(buf++) = (r >> 24) & 0xFF;
}
// 处理剩余的字节(少于4字节)
if (!size) {
return;
}
r = rk_random(state);
for (; size; r >>= 8, size--) {
*(buf++) = (unsigned char)(r & 0xFF);
}
}
rk_error rk_devfill(void *buffer, size_t size, int strong) {
#ifndef _WIN32
FILE *rfile;
int done;
// 根据 strong 参数选择不同的随机设备文件
if (strong) {
rfile = fopen(RK_DEV_RANDOM, "rb");
} else {
rfile = fopen(RK_DEV_URANDOM, "rb");
}
// 打开文件失败时返回错误码
if (rfile == NULL) {
return RK_ENODEV;
}
// 从设备文件读取随机数据到缓冲区
done = fread(buffer, size, 1, rfile);
fclose(rfile);
// 根据读取结果返回相应的错误码或成功码
if (done) {
return RK_NOERR;
}
#else
#ifndef RK_NO_WINCRYPT
HCRYPTPROV hCryptProv;
BOOL done;
// 在 Windows 下使用 CryptGenRandom 函数获取随机数据
if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT) ||
!hCryptProv) {
return RK_ENODEV;
}
done = CryptGenRandom(hCryptProv, size, (unsigned char *)buffer);
CryptReleaseContext(hCryptProv, 0);
// 根据操作结果返回相应的错误码或成功码
if (done) {
return RK_NOERR;
}
#endif
#endif
// 默认返回设备不可用的错误码
return RK_ENODEV;
}
rk_error rk_altfill(void *buffer, size_t size, int strong, rk_state *state) {
rk_error err;
// 调用 rk_devfill 函数填充缓冲区,根据返回值判断是否成功
err = rk_devfill(buffer, size, strong);
// 如果 rk_devfill 失败,则使用 rk_fill 函数填充缓冲区
if (err) {
rk_fill(buffer, size, state);
}
// 返回操作结果的错误码
return err;
}
double rk_gauss(rk_state *state) {
if (state->has_gauss) {
// 如果状态中有预生成的高斯值,则返回并清空状态
const double tmp = state->gauss;
state->gauss = 0;
state->has_gauss = 0;
return tmp;
} else {
double f, x1, x2, r2;
do {
// 使用 Box-Muller 方法的极坐标法生成高斯分布的随机数对
x1 = 2.0 * rk_double(state) - 1.0;
x2 = 2.0 * rk_double(state) - 1.0;
r2 = x1 * x1 + x2 * x2;
} while (r2 >= 1.0 || r2 == 0.0);
// 计算正态分布的随机数并保留一个用于下一次调用
f = sqrt(-2.0 * log(r2) / r2);
state->gauss = f * x1;
state->has_gauss = 1;
return f * x2;
}
}
.\numpy\numpy\random\src\mt19937\randomkit.h
/*
* Random kit 1.3
*/
/*
* Copyright (c) 2003-2005, Jean-Sebastien Roy (js@jeannot.org)
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* @(#) $Jeannot: randomkit.h,v 1.24 2005/07/21 22:14:09 js Exp $ */
/*
* Typical use:
*
* {
* rk_state state;
* unsigned long seed = 1, random_value;
*
* rk_seed(seed, &state); // Initialize the RNG
* ...
* random_value = rk_random(&state); // Generate random values in [0..RK_MAX]
* }
*
* Instead of rk_seed, you can use rk_randomseed which will get a random seed
* from /dev/urandom (or the clock, if /dev/urandom is unavailable):
*
* {
* rk_state state;
* unsigned long random_value;
*
* rk_randomseed(&state); // Initialize the RNG with a random seed
* ...
* random_value = rk_random(&state); // Generate random values in [0..RK_MAX]
* }
*/
/*
* Useful macro:
* RK_DEV_RANDOM: the device used for random seeding.
* defaults to "/dev/urandom"
*/
#ifndef _RANDOMKIT_
#define _RANDOMKIT_
#include <numpy/npy_common.h>
#include <stddef.h>
#define RK_STATE_LEN 624
typedef struct rk_state_ {
unsigned long key[RK_STATE_LEN];
int pos;
int has_gauss; /* !=0: gauss contains a gaussian deviate */
double gauss;
/* The rk_state structure has been extended to store the following
* information for the binomial generator. If the input values of n or p
* are different than nsave and psave, then the other parameters will be
* recomputed. RTK 2005-09-02 */
int has_binomial; /* !=0: following parameters initialized for
binomial */
double psave;
long nsave;
double r;
double q;
double fm;
long m;
double p1;
double xm;
double xl;
double xr;
double c;
double laml;
double lamr;
double p2;
double p3;
double p4;
} rk_state;
typedef enum {
RK_NOERR = 0, /* no error */
RK_ENODEV = 1, /* no RK_DEV_RANDOM device */
RK_ERR_MAX = 2
} rk_error;
/* error strings */
#endif /* _RANDOMKIT_ */
/*
* Array of error messages corresponding to error codes.
*/
extern char *rk_strerror[RK_ERR_MAX];
/*
* Maximum generated random value.
*/
#define RK_MAX 0xFFFFFFFFUL
#ifdef __cplusplus
extern "C" {
#endif
/*
* Initialize the RNG state using the given seed.
*/
extern void rk_seed(unsigned long seed, rk_state *state);
/*
* Initialize the RNG state using a random seed.
* Uses /dev/random or, when unavailable, the clock (see randomkit.c).
* Returns RK_NOERR when no errors occur.
* Returns RK_ENODEV when the use of RK_DEV_RANDOM failed (for example because
* there is no such device). In this case, the RNG was initialized using the
* clock.
*/
extern rk_error rk_randomseed(rk_state *state);
/*
* Returns a random unsigned long between 0 and RK_MAX inclusive.
*/
extern unsigned long rk_random(rk_state *state);
/*
* Returns a random long between 0 and LONG_MAX inclusive.
*/
extern long rk_long(rk_state *state);
/*
* Returns a random unsigned long between 0 and ULONG_MAX inclusive.
*/
extern unsigned long rk_ulong(rk_state *state);
/*
* Returns a random unsigned long between 0 and max inclusive.
*/
extern unsigned long rk_interval(unsigned long max, rk_state *state);
/*
* Fills an array with cnt random npy_uint64 between off and off + rng
* inclusive. The numbers wrap if rng is sufficiently large.
*/
extern void rk_random_uint64(npy_uint64 off, npy_uint64 rng, npy_intp cnt,
npy_uint64 *out, rk_state *state);
/*
* Fills an array with cnt random npy_uint32 between off and off + rng
* inclusive. The numbers wrap if rng is sufficiently large.
*/
extern void rk_random_uint32(npy_uint32 off, npy_uint32 rng, npy_intp cnt,
npy_uint32 *out, rk_state *state);
/*
* Fills an array with cnt random npy_uint16 between off and off + rng
* inclusive. The numbers wrap if rng is sufficiently large.
*/
extern void rk_random_uint16(npy_uint16 off, npy_uint16 rng, npy_intp cnt,
npy_uint16 *out, rk_state *state);
/*
* Fills an array with cnt random npy_uint8 between off and off + rng
* inclusive. The numbers wrap if rng is sufficiently large.
*/
extern void rk_random_uint8(npy_uint8 off, npy_uint8 rng, npy_intp cnt,
npy_uint8 *out, rk_state *state);
/*
* Fills an array with cnt random npy_bool between off and off + rng
* inclusive. It is assumed that npy_bool is the same size as npy_uint8.
*/
extern void rk_random_bool(npy_bool off, npy_bool rng, npy_intp cnt,
npy_bool *out, rk_state *state);
/*
* Returns a random double between 0.0 and 1.0, 1.0 excluded.
*/
extern double rk_double(rk_state *state);
/*
* Fill the buffer with size random bytes.
*/
extern void rk_fill(void *buffer, size_t size, rk_state *state);
#ifdef __cplusplus
}
#endif
/*
* 填充缓冲区使用来自随机设备的随机字节
* 如果设备不可用,则返回RK_ENODEV,如果可用则返回RK_NOERR
* 在Unix系统上,如果定义了strong,则使用RK_DEV_RANDOM;如果未定义,则使用RK_DEV_URANDOM。
* 警告:在大多数Unix系统上,RK_DEV_RANDOM会等待足够的熵以产生随机数,这在安静系统上可能需要很长时间。
*/
extern rk_error rk_devfill(void *buffer, size_t size, int strong);
/*
* 使用rk_devfill填充缓冲区(如果随机设备可用),否则使用rk_fill
* 参数与rk_fill和rk_devfill相同
* 如果设备不可用,则返回RK_ENODEV,如果可用则返回RK_NOERR
*/
extern rk_error rk_altfill(void *buffer, size_t size, int strong,
rk_state *state);
/*
* 返回均值为零,方差为一的随机高斯变量
*/
extern double rk_gauss(rk_state *state);
#ifdef __cplusplus
}
#endif
#endif /* _RANDOMKIT_ */
PCG64
The MIT License
PCG Random Number Generation for C.
Copyright 2014 Melissa O'Neill oneill@pcg-random.org
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
.\numpy\numpy\random\src\pcg64\pcg64-benchmark.c
/*
* 以下是一个 C 语言程序,用于基准测试 PCG64 随机数生成器的性能。
* 在不同的编译器和系统上进行了两次编译测试,分别用了不同的编译选项。
*/
#include "../splitmix64/splitmix64.h" // 引入 splitmix64 随机数生成器的头文件
#include "pcg64.h" // 引入 pcg64 随机数生成器的头文件
#include <inttypes.h> // 包含了 int64_t 等整型的定义
#include <stdio.h> // 标准输入输出库
#include <time.h> // 包含了时钟函数 clock()
#define N 1000000000 // 循环迭代次数
int main() {
pcg64_random_t rng; // 定义 pcg64 随机数生成器结构体
uint64_t sum = 0, count = 0; // 初始化总和与计数
uint64_t seed = 0xDEADBEAF; // 种子值
int i;
#if __SIZEOF_INT128__ && !defined(PCG_FORCE_EMULATED_128BIT_MATH)
// 如果支持原生的 128 位整数并且没有强制启用模拟的 128 位数学运算
rng.state = (__uint128_t)splitmix64_next(&seed) << 64; // 使用 splitmix64 生成高位状态
rng.state |= splitmix64_next(&seed); // 生成低位状态
rng.inc = (__uint128_t)1; // 设置增量为 1
#else
// 否则使用分离的 64 位状态
rng.state.high = splitmix64_next(&seed); // 使用 splitmix64 生成高位状态
rng.state.low = splitmix64_next(&seed); // 生成低位状态
rng.inc.high = 0; // 设置增量高位为 0
rng.inc.low = 1; // 设置增量低位为 1
#endif
clock_t begin = clock(); // 记录开始时钟时间
for (i = 0; i < N; i++) {
sum += pcg64_random_r(&rng); // 调用 pcg64 生成随机数并累加到总和
count++; // 增加计数
}
clock_t end = clock(); // 记录结束时钟时间
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC; // 计算运行时间
printf("0x%" PRIx64 "\ncount: %" PRIu64 "\n", sum, count); // 输出总和与计数
printf("%" PRIu64 " randoms per second\n",
(uint64_t)(N / time_spent) / 1000000 * 1000000); // 输出每秒生成的随机数数量
}
.\numpy\numpy\random\src\pcg64\pcg64-test-data-gen.c
/*
* 生成测试用的 CSV 文件
*
* 仅适用于 GCC 编译器
*
* 编译命令:
* gcc pcg64-test-data-gen.c pcg64.orig.c ../splitmix64/splitmix64.c -o pgc64-test-data-gen
*/
#include "pcg64.orig.h"
#include <inttypes.h>
#include <stdio.h>
#define N 1000
int main() {
pcg64_random_t rng; // 定义 pcg64 随机数生成器结构体
uint64_t state, seed = 0xDEADBEAF; // 定义状态变量和种子值
state = seed; // 将种子值赋给状态变量
__uint128_t temp, s, inc; // 定义用于存储状态和增量的 128 位整数变量
int i; // 循环变量
uint64_t store[N]; // 存储生成随机数的数组
s = (__uint128_t)seed; // 将种子转换为 128 位整数类型
inc = (__uint128_t)0; // 设置增量为 0
pcg64_srandom_r(&rng, s, inc); // 初始化随机数生成器
printf("0x%" PRIx64, (uint64_t)(rng.state >> 64)); // 打印状态的高 64 位十六进制值
printf("%" PRIx64 "\n", (uint64_t)rng.state); // 打印状态的低 64 位十六进制值
printf("0x%" PRIx64, (uint64_t)(rng.inc >> 64)); // 打印增量的高 64 位十六进制值
printf("%" PRIx64 "\n", (uint64_t)rng.inc); // 打印增量的低 64 位十六进制值
for (i = 0; i < N; i++) {
store[i] = pcg64_random_r(&rng); // 生成随机数并存储到数组中
}
FILE *fp; // 文件指针
fp = fopen("pcg64-testset-1.csv", "w"); // 打开文件准备写入
if (fp == NULL) { // 检查文件是否成功打开
printf("Couldn't open file\n"); // 打开失败时输出错误信息
return -1; // 返回错误状态码
}
fprintf(fp, "seed, 0x%" PRIx64 "\n", seed); // 将种子值写入文件
for (i = 0; i < N; i++) {
fprintf(fp, "%d, 0x%" PRIx64 "\n", i, store[i]); // 将索引和对应的随机数写入文件
if (i == 999) {
printf("%d, 0x%" PRIx64 "\n", i, store[i]); // 打印第 1000 个数据的索引和随机数
}
}
fclose(fp); // 关闭文件
state = seed = 0; // 重置状态和种子
s = (__uint128_t)seed; // 将种子转换为 128 位整数类型
i = (__uint128_t)0; // 将循环变量转换为 128 位整数类型
pcg64_srandom_r(&rng, s, i); // 重新初始化随机数生成器
printf("0x%" PRIx64, (uint64_t)(rng.state >> 64)); // 打印状态的高 64 位十六进制值
printf("%" PRIx64 "\n", (uint64_t)rng.state); // 打印状态的低 64 位十六进制值
printf("0x%" PRIx64, (uint64_t)(rng.inc >> 64)); // 打印增量的高 64 位十六进制值
printf("%" PRIx64 "\n", (uint64_t)rng.inc); // 打印增量的低 64 位十六进制值
for (i = 0; i < N; i++) {
store[i] = pcg64_random_r(&rng); // 生成随机数并存储到数组中
}
fp = fopen("pcg64-testset-2.csv", "w"); // 打开文件准备写入
if (fp == NULL) { // 检查文件是否成功打开
printf("Couldn't open file\n"); // 打开失败时输出错误信息
return -1; // 返回错误状态码
}
fprintf(fp, "seed, 0x%" PRIx64 "\n", seed); // 将种子值写入文件
for (i = 0; i < N; i++) {
fprintf(fp, "%d, 0x%" PRIx64 "\n", i, store[i]); // 将索引和对应的随机数写入文件
if (i == 999) {
printf("%d, 0x%" PRIx64 "\n", i, store[i]); // 打印第 1000 个数据的索引和随机数
}
}
fclose(fp); // 关闭文件
}
.\numpy\numpy\random\src\pcg64\pcg64.c
/*
* PCG64 Random Number Generation for C.
*
* Copyright 2014 Melissa O'Neill <oneill@pcg-random.org>
* Copyright 2015 Robert Kern <robert.kern@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For additional information about the PCG random number generation scheme,
* including its license and other licensing options, visit
*
* https://www.pcg-random.org
*
* Relicensed MIT in May 2019
*
* The MIT License
*
* PCG Random Number Generation for C.
*
* Copyright 2014 Melissa O'Neill <oneill@pcg-random.org>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// 包含 PCG64 随机数生成器的头文件
#include "pcg64.h"
// 定义外部链接的内联函数,设置序列 128 步骤
extern inline void pcg_setseq_128_step_r(pcg_state_setseq_128 *rng);
// 定义外部链接的内联函数,输出 XSL-RR 128 到 64 位
extern inline uint64_t pcg_output_xsl_rr_128_64(pcg128_t state);
// 定义外部链接的内联函数,初始化序列 128 的状态和序列
extern inline void pcg_setseq_128_srandom_r(pcg_state_setseq_128 *rng,
pcg128_t initstate,
pcg128_t initseq);
// 定义外部链接的内联函数,生成 XSL-RR 64 位随机数
extern inline uint64_t pcg_setseq_128_xsl_rr_64_random_r(pcg_state_setseq_128 *rng);
// 定义外部链接的内联函数,生成 XSL-RR 64 位有界随机数
extern inline uint64_t pcg_setseq_128_xsl_rr_64_boundedrand_r(pcg_state_setseq_128 *rng,
uint64_t bound);
// 定义外部链接的内联函数,推进序列 128 的状态
extern inline void pcg_setseq_128_advance_r(pcg_state_setseq_128 *rng,
pcg128_t delta);
// 定义外部链接的内联函数,生成 CM 方法的随机数
extern inline uint64_t pcg_cm_random_r(pcg_state_setseq_128 *rng);
// 定义外部链接的内联函数,CM 方法的步骤
extern inline void pcg_cm_step_r(pcg_state_setseq_128 *rng);
/*
* 外部声明:内联函数,用于从 pcg128_t 状态中输出 uint64_t 类型数据
*/
extern inline uint64_t pcg_output_cm_128_64(pcg128_t state);
/*
* 外部声明:内联函数,用于初始化 pcg_state_setseq_128 结构中的状态
*/
extern inline void pcg_cm_srandom_r(pcg_state_setseq_128 *rng, pcg128_t initstate, pcg128_t initseq);
/*
* 多步进函数(跳跃前进、跳跃后退)
*
* 该方法基于 Brown 的论文 "Random Number Generation with Arbitrary Stride,"
* 算法类似于快速指数运算。
*
* 即使 delta 是无符号整数,我们也可以传递有符号整数以实现向后跳跃,只不过会"绕远路"。
*/
#ifndef PCG_EMULATED_128BIT_MATH
/*
* 函数:pcg_advance_lcg_128
* 功能:根据指定的 delta 值,以及当前的乘法和加法常数,对状态进行跳跃操作
*/
pcg128_t pcg_advance_lcg_128(pcg128_t state, pcg128_t delta, pcg128_t cur_mult,
pcg128_t cur_plus) {
pcg128_t acc_mult = 1u; // 累积乘法常数
pcg128_t acc_plus = 0u; // 累积加法常数
while (delta > 0) { // 当 delta 大于 0 时执行循环
if (delta & 1) { // 如果 delta 的最低位为1
acc_mult *= cur_mult; // 更新累积乘法常数
acc_plus = acc_plus * cur_mult + cur_plus; // 更新累积加法常数
}
cur_plus = (cur_mult + 1) * cur_plus; // 更新当前加法常数
cur_mult *= cur_mult; // 更新当前乘法常数
delta /= 2; // delta 右移一位(相当于除以2)
}
return acc_mult * state + acc_plus; // 返回跳跃后的状态值
}
#else
/*
* 函数:pcg_advance_lcg_128
* 功能:根据指定的 delta 值,以及当前的乘法和加法常数,对状态进行跳跃操作
* 注:使用了 PCG 128位常量来处理数值运算
*/
pcg128_t pcg_advance_lcg_128(pcg128_t state, pcg128_t delta, pcg128_t cur_mult,
pcg128_t cur_plus) {
pcg128_t acc_mult = PCG_128BIT_CONSTANT(0u, 1u); // 累积乘法常数初始化为 1
pcg128_t acc_plus = PCG_128BIT_CONSTANT(0u, 0u); // 累积加法常数初始化为 0
while ((delta.high > 0) || (delta.low > 0)) { // 当 delta 的高位或低位大于 0 时执行循环
if (delta.low & 1) { // 如果 delta 的最低位为1
acc_mult = pcg128_mult(acc_mult, cur_mult); // 更新累积乘法常数
acc_plus = pcg128_add(pcg128_mult(acc_plus, cur_mult), cur_plus); // 更新累积加法常数
}
cur_plus = pcg128_mult(pcg128_add(cur_mult, PCG_128BIT_CONSTANT(0u, 1u)),
cur_plus); // 更新当前加法常数
cur_mult = pcg128_mult(cur_mult, cur_mult); // 更新当前乘法常数
delta.low = (delta.low >> 1) | (delta.high << 63); // delta 右移一位,高位低位交替更新
delta.high >>= 1; // delta 高位右移一位
}
return pcg128_add(pcg128_mult(acc_mult, state), acc_plus); // 返回跳跃后的状态值
}
#endif
/*
* 外部声明:内联函数,用于从 pcg64_state 结构中获取下一个 uint64_t 类型数据
*/
extern inline uint64_t pcg64_next64(pcg64_state *state);
/*
* 外部声明:内联函数,用于从 pcg64_state 结构中获取下一个 uint32_t 类型数据
*/
extern inline uint32_t pcg64_next32(pcg64_state *state);
/*
* 外部声明:内联函数,用于从 pcg64_state 结构中获取下一个 uint64_t 类型数据
*/
extern inline uint64_t pcg64_cm_next64(pcg64_state *state);
/*
* 外部声明:内联函数,用于从 pcg64_state 结构中获取下一个 uint32_t 类型数据
*/
extern inline uint32_t pcg64_cm_next32(pcg64_state *state);
/*
* 函数:pcg64_advance
* 功能:根据给定的步长,以 pcg128_t 形式传递 delta 值,并调用 pcg64_advance_r 函数
*/
extern void pcg64_advance(pcg64_state *state, uint64_t *step) {
pcg128_t delta; // 定义 pcg128_t 类型的 delta 变量
#ifndef PCG_EMULATED_128BIT_MATH
delta = (((pcg128_t)step[0]) << 64) | step[1]; // 构造 delta 值(未模拟128位数学时)
#else
delta.high = step[0]; // 设置 delta 高位
delta.low = step[1]; // 设置 delta 低位
#endif
pcg64_advance_r(state->pcg_state, delta); // 调用 pcg64_advance_r 函数
}
/*
* 函数:pcg64_cm_advance
* 功能:根据给定的步长,以 pcg128_t 形式传递 delta 值,并调用 pcg_cm_advance_r 函数
*/
extern void pcg64_cm_advance(pcg64_state *state, uint64_t *step) {
pcg128_t delta; // 定义 pcg128_t 类型的 delta 变量
#ifndef PCG_EMULATED_128BIT_MATH
delta = (((pcg128_t)step[0]) << 64) | step[1]; // 构造 delta 值(未模拟128位数学时)
#else
delta.high = step[0]; // 设置 delta 高位
delta.low = step[1]; // 设置 delta 低位
#endif
pcg_cm_advance_r(state->pcg_state, delta); // 调用 pcg_cm_advance_r 函数
}
/*
* 函数:pcg64_set_seed
* 功能:根据给定的种子和增量,以 pcg128_t 形式传递 s 和 i 值,并调用 pcg64_srandom_r 函数
*/
extern void pcg64_set_seed(pcg64_state *state, uint64_t *seed, uint64_t *inc) {
pcg128_t s, i; // 定义 pcg128_t 类型的 s 和 i 变量
#ifndef PCG_EMULATED_128BIT_MATH
s = (((pcg128_t)seed[0]) << 64) | seed[1]; // 构造种子 s 值(未模拟128位数学时)
i = (((pcg128_t)inc[0]) << 64) | inc[1]; // 构造增量 i 值(未模拟128位数学时)
#else
s.high = seed[0]; // 设置种子 s 的高位
s.low = seed[1]; // 设置种子 s 的低位
i.high = inc[0]; // 设置增量 i 的高位
i.low = inc[1]; // 设置增量 i 的低位
#endif
pcg64_srandom_r(state->pcg_state, s, i); // 调用 pcg64_srandom_r 函数
}
extern void pcg64_get_state(pcg64_state *state, uint64_t *state_arr,
int *has_uint32, uint32_t *uinteger) {
/*
* state_arr contains state.high, state.low, inc.high, inc.low
* which are interpreted as the upper 64 bits (high) or lower
* 64 bits of a uint128_t variable
*
*/
#ifndef PCG_EMULATED_128BIT_MATH
// 将 PCG 状态的高 64 位存入 state_arr[0]
state_arr[0] = (uint64_t)(state->pcg_state->state >> 64);
// 将 PCG 状态的低 64 位存入 state_arr[1]
state_arr[1] = (uint64_t)(state->pcg_state->state & 0xFFFFFFFFFFFFFFFFULL);
// 将 PCG 增量的高 64 位存入 state_arr[2]
state_arr[2] = (uint64_t)(state->pcg_state->inc >> 64);
// 将 PCG 增量的低 64 位存入 state_arr[3]
state_arr[3] = (uint64_t)(state->pcg_state->inc & 0xFFFFFFFFFFFFFFFFULL);
#else
// 使用 emulated 128 位整数模式时,直接将状态和增量的高低位赋给 state_arr
state_arr[0] = (uint64_t)state->pcg_state->state.high;
state_arr[1] = (uint64_t)state->pcg_state->state.low;
state_arr[2] = (uint64_t)state->pcg_state->inc.high;
state_arr[3] = (uint64_t)state->pcg_state->inc.low;
#endif
// 将 state 的 has_uint32 值赋给 has_uint32 数组
has_uint32[0] = state->has_uint32;
// 将 state 的 uinteger 值赋给 uinteger 数组
uinteger[0] = state->uinteger;
}
extern void pcg64_set_state(pcg64_state *state, uint64_t *state_arr,
int has_uint32, uint32_t uinteger) {
/*
* state_arr contains state.high, state.low, inc.high, inc.low
* which are interpreted as the upper 64 bits (high) or lower
* 64 bits of a uint128_t variable
*
*/
#ifndef PCG_EMULATED_128BIT_MATH
// 根据 state_arr 的高低位设置 PCG 状态的整体值
state->pcg_state->state = (((pcg128_t)state_arr[0]) << 64) | state_arr[1];
// 根据 state_arr 的高低位设置 PCG 增量的整体值
state->pcg_state->inc = (((pcg128_t)state_arr[2]) << 64) | state_arr[3];
#else
// 使用 emulated 128 位整数模式时,直接赋值给状态和增量的高低位
state->pcg_state->state.high = state_arr[0];
state->pcg_state->state.low = state_arr[1];
state->pcg_state->inc.high = state_arr[2];
state->pcg_state->inc.low = state_arr[3];
#endif
// 将 has_uint32 的值赋给 state 的 has_uint32
state->has_uint32 = has_uint32;
// 将 uinteger 的值赋给 state 的 uinteger
state->uinteger = uinteger;
}
.\numpy\numpy\random\src\pcg64\pcg64.h
/*
* PCG64 Random Number Generation for C.
*
* Copyright 2014 Melissa O'Neill <oneill@pcg-random.org>
* Copyright 2015 Robert Kern <robert.kern@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For additional information about the PCG random number generation scheme,
* including its license and other licensing options, visit
*
* https://www.pcg-random.org
*
* Relicensed MIT in May 2019
*
* The MIT License
*
* PCG Random Number Generation for C.
*
* Copyright 2014 Melissa O'Neill <oneill@pcg-random.org>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef PCG64_H_INCLUDED
#define PCG64_H_INCLUDED 1
#include <inttypes.h> // 包含整数类型定义
#ifdef _WIN32
#include <stdlib.h> // 包含标准库定义
#endif
#if defined(_WIN32) && !defined (__MINGW32__)
#define inline __forceinline // 定义内联函数修饰符为 __forceinline
#endif
#if defined(__GNUC_GNU_INLINE__) && !defined(__cplusplus)
#error Nonstandard GNU inlining semantics. Compile with -std=c99 or better.
#endif
#ifdef __cplusplus
extern "C" {
#endif
#if defined(__SIZEOF_INT128__) && !defined(PCG_FORCE_EMULATED_128BIT_MATH)
typedef __uint128_t pcg128_t; // 定义 128 位无符号整数类型 pcg128_t
#define PCG_128BIT_CONSTANT(high, low) (((pcg128_t)(high) << 64) + low) // 定义生成 128 位常量的宏
#else
typedef struct {
uint64_t high; // 128 位结构体高位 64 位
uint64_t low; // 128 位结构体低位 64 位
} pcg128_t;
static inline pcg128_t PCG_128BIT_CONSTANT(uint64_t high, uint64_t low) {
pcg128_t result;
result.high = high; // 设置高位值
result.low = low; // 设置低位值
return result; // 返回设置好的结构体
}
#define PCG_EMULATED_128BIT_MATH 1 // 定义为使用模拟的 128 位数学运算
#endif
typedef struct { pcg128_t state; } pcg_state_128;
定义了一个结构体 `pcg_state_128`,包含一个名为 `state` 的成员变量,类型为 `pcg128_t`。
typedef struct {
pcg128_t state;
pcg128_t inc;
} pcg_state_setseq_128;
定义了一个结构体 `pcg_state_setseq_128`,包含两个成员变量 `state` 和 `inc`,都是类型为 `pcg128_t` 的变量。
#define PCG_DEFAULT_MULTIPLIER_HIGH 2549297995355413924ULL
#define PCG_DEFAULT_MULTIPLIER_LOW 4865540595714422341ULL
定义了两个宏 `PCG_DEFAULT_MULTIPLIER_HIGH` 和 `PCG_DEFAULT_MULTIPLIER_LOW`,分别表示一个128位乘法的高位和低位默认乘数。
#define PCG_DEFAULT_MULTIPLIER_128 \
PCG_128BIT_CONSTANT(PCG_DEFAULT_MULTIPLIER_HIGH, PCG_DEFAULT_MULTIPLIER_LOW)
#define PCG_DEFAULT_INCREMENT_128 \
PCG_128BIT_CONSTANT(6364136223846793005ULL, 1442695040888963407ULL)
定义了两个宏 `PCG_DEFAULT_MULTIPLIER_128` 和 `PCG_DEFAULT_INCREMENT_128`,分别表示默认的128位乘数和增量。`PCG_128BIT_CONSTANT` 是一个宏,用于将高位和低位常量合并成一个 `pcg128_t` 类型的常量。
#define PCG_STATE_SETSEQ_128_INITIALIZER \
{ \
PCG_128BIT_CONSTANT(0x979c9a98d8462005ULL, 0x7d3e9cb6cfe0549bULL) \
, PCG_128BIT_CONSTANT(0x0000000000000001ULL, 0xda3e39cb94b95bdbULL) \
}
定义了一个宏 `PCG_STATE_SETSEQ_128_INITIALIZER`,用于初始化 `pcg_state_setseq_128` 结构体的实例,其中包括 `state` 和 `inc` 的初始值。
#define PCG_CHEAP_MULTIPLIER_128 (0xda942042e4dd58b5ULL)
定义了一个宏 `PCG_CHEAP_MULTIPLIER_128`,表示一个较为简单的128位乘数。
static inline uint64_t pcg_rotr_64(uint64_t value, unsigned int rot) {
#ifdef _WIN32
return _rotr64(value, rot);
#else
return (value >> rot) | (value << ((-rot) & 63));
#endif
}
定义了一个静态内联函数 `pcg_rotr_64`,实现对 `value` 进行64位的循环右移操作。
#ifdef PCG_EMULATED_128BIT_MATH
static inline pcg128_t pcg128_add(pcg128_t a, pcg128_t b) {
pcg128_t result;
result.low = a.low + b.low;
result.high = a.high + b.high + (result.low < b.low);
return result;
}
如果定义了 `PCG_EMULATED_128BIT_MATH` 宏,则定义了一个静态内联函数 `pcg128_add`,实现对两个 `pcg128_t` 类型的数进行加法操作,并处理溢出。
static inline void _pcg_mult64(uint64_t x, uint64_t y, uint64_t *z1,
uint64_t *z0) {
#if defined _WIN32 && _M_AMD64
z0[0] = _umul128(x, y, z1);
#else
uint64_t x0, x1, y0, y1;
uint64_t w0, w1, w2, t;
*z0 = x * y;
x0 = x & 0xFFFFFFFFULL;
x1 = x >> 32;
y0 = y & 0xFFFFFFFFULL;
y1 = y >> 32;
w0 = x0 * y0;
t = x1 * y0 + (w0 >> 32);
w1 = t & 0xFFFFFFFFULL;
w2 = t >> 32;
w1 += x0 * y1;
*z1 = x1 * y1 + w2 + (w1 >> 32);
#endif
}
定义了一个静态内联函数 `_pcg_mult64`,用于对两个64位数 `x` 和 `y` 进行乘法操作,并将结果存储在 `z1` 和 `z0` 中。根据平台不同选择使用不同的乘法实现。
static inline pcg128_t pcg128_mult(pcg128_t a, pcg128_t b) {
uint64_t h1;
pcg128_t result;
h1 = a.high * b.low + a.low * b.high;
_pcg_mult64(a.low, b.low, &(result.high), &(result.low));
result.high += h1;
return result;
}
定义了一个静态内联函数 `pcg128_mult`,用于对两个 `pcg128_t` 类型的数进行乘法操作,返回乘积结果。
static inline void pcg_setseq_128_step_r(pcg_state_setseq_128 *rng) {
rng->state = pcg128_add(pcg128_mult(rng->state, PCG_DEFAULT_MULTIPLIER_128),
rng->inc);
}
定义了一个静态内联函数 `pcg_setseq_128_step_r`,实现了一步 PCG 128位序列生成器的状态更新。
static inline uint64_t pcg_output_xsl_rr_128_64(pcg128_t state) {
return pcg_rotr_64(state.high ^ state.low, state.high >> 58u);
}
定义了一个静态内联函数 `pcg_output_xsl_rr_128_64`,实现了 PCG 128位序列生成器的输出函数。
static inline void pcg_setseq_128_srandom_r(pcg_state_setseq_128 *rng,
pcg128_t initstate,
pcg128_t initseq) {
rng->state = PCG_128BIT_CONSTANT(0ULL, 0ULL);
rng->inc.high = initseq.high << 1u;
rng->inc.high |= initseq.low >> 63u;
rng->inc.low = (initseq.low << 1u) | 1u;
pcg_setseq_128_step_r(rng);
rng->state = pcg128_add(rng->state, initstate);
pcg_setseq_128_step_r(rng);
}
定义了一个静态内联函数 `pcg_setseq_128_srandom_r`,用于初始化 PCG 128位序列生成器的状态和增量,以及执行初始步骤。
static inline uint64_t
定义了一个静态内联函数的开始部分,但截至当前行没有提供具体实现。
static inline pcg128_t pcg128_mult_64(pcg128_t a, uint64_t b) {
// 定义变量 h1 存储高位乘积结果
uint64_t h1;
// 定义结果变量 result
pcg128_t result;
// 计算高位乘积
h1 = a.high * b;
// 调用 _pcg_mult64 函数计算低位乘积
_pcg_mult64(a.low, b, &(result.high), &(result.low));
// 将高位乘积加到 result 的高位
result.high += h1;
// 返回乘积结果
return result;
}
static inline void pcg_cm_step_r(pcg_state_setseq_128 *rng) {
#if defined _WIN32 && _M_AMD64
// 定义变量 h1 存储高位乘积结果
uint64_t h1;
// 定义结果变量 product
pcg128_t product;
// 手动内联使用内部函数进行乘法和加法运算
// 计算高位乘积
h1 = rng->state.high * PCG_CHEAP_MULTIPLIER_128;
// 调用 _umul128 函数计算低位乘积
product.low =
_umul128(rng->state.low, PCG_CHEAP_MULTIPLIER_128, &(product.high));
// 将高位乘积加到 product 的高位
product.high += h1;
// 使用 _addcarry_u64 函数更新 rng->state 中的值
_addcarry_u64(_addcarry_u64(0, product.low, rng->inc.low, &(rng->state.low)),
product.high, rng->inc.high, &(rng->state.high));
#else
// 在非 Windows 下,调用 pcg128_mult_64 和 pcg128_add 函数更新 rng->state
rng->state = pcg128_add(pcg128_mult_64(rng->state, PCG_CHEAP_MULTIPLIER_128),
rng->inc);
#endif
}
static inline void pcg_cm_srandom_r(pcg_state_setseq_128 *rng, pcg128_t initstate, pcg128_t initseq) {
// 初始化 rng->state 和 rng->inc
rng->state = PCG_128BIT_CONSTANT(0ULL, 0ULL);
rng->inc.high = initseq.high << 1u;
rng->inc.high |= initseq.low >> 63u;
rng->inc.low = (initseq.low << 1u) | 1u;
// 执行一次 pcg_cm_step_r
pcg_cm_step_r(rng);
// 更新 rng->state
rng->state = pcg128_add(rng->state, initstate);
// 再次执行 pcg_cm_step_r
pcg_cm_step_r(rng);
}
static inline uint64_t pcg_cm_random_r(pcg_state_setseq_128* rng)
{
// 高位和低位初始化为 rng->state 的值
uint64_t hi = rng->state.high;
uint64_t lo = rng->state.low;
// 执行 DXSM 输出函数在预迭代状态上
// 将低位设置为 1
lo |= 1;
// 高位按位右移 32 位后与自身按位异或
hi ^= hi >> 32;
// 高位乘以常数
hi *= 0xda942042e4dd58b5ULL;
// 再次高位按位右移 48 位后与自身按位异或
hi ^= hi >> 48;
// 高位乘以低位
hi *= lo;
// 执行 CM 步骤
#if defined _WIN32 && _M_AMD64
// 定义变量 h1 存储高位乘积结果
uint64_t h1;
// 定义结果变量 product
pcg128_t product;
// 手动内联使用内部函数进行乘法和加法运算
// 计算高位乘积
h1 = rng->state.high * PCG_CHEAP_MULTIPLIER_128;
// 调用 _umul128 函数计算低位乘积
product.low =
_umul128(rng->state.low, PCG_CHEAP_MULTIPLIER_128, &(product.high));
// 将高位乘积加到 product 的高位
product.high += h1;
// 使用 _addcarry_u64 函数更新 rng->state 中的值
_addcarry_u64(_addcarry_u64(0, product.low, rng->inc.low, &(rng->state.low)),
product.high, rng->inc.high, &(rng->state.high));
#else
// 在非 Windows 下,调用 pcg128_mult_64 和 pcg128_add 函数更新 rng->state
rng->state = pcg128_add(pcg128_mult_64(rng->state, PCG_CHEAP_MULTIPLIER_128),
rng->inc);
#endif
// 返回高位
return hi;
}
#else /* PCG_EMULATED_128BIT_MATH */
代码块开始,说明在不支持128位整数运算的情况下使用的条件编译。
static inline void pcg_setseq_128_step_r(pcg_state_setseq_128 *rng) {
rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_128 + rng->inc;
}
定义函数pcg_setseq_128_step_r,用于PCG状态结构体的状态更新。
static inline uint64_t pcg_output_xsl_rr_128_64(pcg128_t state) {
return pcg_rotr_64(((uint64_t)(state >> 64u)) ^ (uint64_t)state,
state >> 122u);
}
定义函数pcg_output_xsl_rr_128_64,用于生成64位随机数的输出函数,通过XSL-RR算法实现。
static inline void pcg_cm_step_r(pcg_state_setseq_128 *rng) {
rng-> state = rng->state * PCG_CHEAP_MULTIPLIER_128 + rng->inc;
}
定义函数pcg_cm_step_r,用于PCG状态结构体的状态更新,采用便宜乘法器。
static inline uint64_t pcg_output_cm_128_64(pcg128_t state) {
uint64_t hi = state >> 64;
uint64_t lo = state;
lo |= 1;
hi ^= hi >> 32;
hi *= 0xda942042e4dd58b5ULL;
hi ^= hi >> 48;
hi *= lo;
return hi;
}
定义函数pcg_output_cm_128_64,用于生成64位随机数的输出函数,通过CM算法实现。
static inline void pcg_cm_srandom_r(pcg_state_setseq_128 *rng, pcg128_t initstate, pcg128_t initseq) {
rng->state = 0U;
rng->inc = (initseq << 1u) | 1u;
pcg_cm_step_r(rng);
rng->state += initstate;
pcg_cm_step_r(rng);
}
定义函数pcg_cm_srandom_r,用于初始化PCG状态结构体,采用CM算法。
static inline uint64_t pcg_cm_random_r(pcg_state_setseq_128* rng)
{
uint64_t ret = pcg_output_cm_128_64(rng->state);
pcg_cm_step_r(rng);
return ret;
}
定义函数pcg_cm_random_r,用于生成64位随机数,基于已初始化的PCG状态结构体,采用CM算法。
static inline uint64_t
pcg_setseq_128_xsl_rr_64_random_r(pcg_state_setseq_128* rng)
{
pcg_setseq_128_step_r(rng);
return pcg_output_xsl_rr_128_64(rng->state);
}
定义函数pcg_setseq_128_xsl_rr_64_random_r,用于生成64位随机数,基于已初始化的PCG状态结构体,采用XSL-RR算法。
static inline void pcg_setseq_128_srandom_r(pcg_state_setseq_128 *rng,
pcg128_t initstate,
pcg128_t initseq) {
rng->state = 0U;
rng->inc = (initseq << 1u) | 1u;
pcg_setseq_128_step_r(rng);
rng->state += initstate;
pcg_setseq_128_step_r(rng);
}
定义函数pcg_setseq_128_srandom_r,用于初始化PCG状态结构体,采用setseq(序列设置)算法。
#endif /* PCG_EMULATED_128BIT_MATH */
代码块结束,结束条件编译段,对于不支持128位整数运算的情况。
static inline uint64_t
pcg_setseq_128_xsl_rr_64_boundedrand_r(pcg_state_setseq_128 *rng,
uint64_t bound) {
uint64_t threshold = -bound % bound;
for (;;) {
uint64_t r = pcg_setseq_128_xsl_rr_64_random_r(rng);
if (r >= threshold)
return r % bound;
}
}
定义函数pcg_setseq_128_xsl_rr_64_boundedrand_r,生成一个不超过bound的随机数,基于已初始化的PCG状态结构体,采用XSL-RR算法。
extern pcg128_t pcg_advance_lcg_128(pcg128_t state, pcg128_t delta,
pcg128_t cur_mult, pcg128_t cur_plus);
声明函数pcg_advance_lcg_128,用于LCG(线性同余生成器)的128位状态推进。
static inline void pcg_setseq_128_advance_r(pcg_state_setseq_128 *rng,
pcg128_t delta) {
rng->state = pcg_advance_lcg_128(rng->state, delta,
PCG_DEFAULT_MULTIPLIER_128, rng->inc);
}
定义函数pcg_setseq_128_advance_r,用于推进PCG状态结构体,采用setseq算法。
static inline void pcg_cm_advance_r(pcg_state_setseq_128 *rng, pcg128_t delta) {
rng->state = pcg_advance_lcg_128(rng->state, delta,
PCG_128BIT_CONSTANT(0, PCG_CHEAP_MULTIPLIER_128),
rng->inc);
}
定义函数pcg_cm_advance_r,用于推进PCG状态结构体,采用CM算法。
typedef pcg_state_setseq_128 pcg64_random_t;
声明pcg64_random_t类型,作为pcg_state_setseq_128的别名,用于64位随机数生成。
#define pcg64_random_r pcg_setseq_128_xsl_rr_64_random_r
#define pcg64_boundedrand_r pcg_setseq_128_xsl_rr_64_boundedrand_r
#define pcg64_srandom_r pcg_setseq_128_srandom_r
#define pcg64_advance_r pcg_setseq_128_advance_r
#define PCG64_INITIALIZER PCG_STATE_SETSEQ_128_INITIALIZER
定义宏,为pcg64_random_r、pcg64_boundedrand_r、pcg64_srandom_r、pcg64_advance_r提供别名,以及PCG64_INITIALIZER的定义。
#ifdef __cplusplus
}
#endif
结束C++的extern "C"声明,如果是C++环境则关闭extern "C"。
# 定义一个结构体 `s_pcg64_state`,用于封装 PCG64 随机数生成器的状态信息
typedef struct s_pcg64_state {
pcg64_random_t *pcg_state; // 指向 PCG64 随机数生成器状态的指针
int has_uint32; // 标志位,指示是否有未使用的 uint32_t 类型的随机数
uint32_t uinteger; // 存储未使用的 uint32_t 类型的随机数
} pcg64_state;
# 定义一个静态内联函数 `pcg64_next64`,用于生成下一个 64 位随机数
static inline uint64_t pcg64_next64(pcg64_state *state) {
return pcg64_random_r(state->pcg_state); // 调用 PCG64 随机数生成器生成下一个 64 位随机数
}
# 定义一个静态内联函数 `pcg64_next32`,用于生成下一个 32 位随机数
static inline uint32_t pcg64_next32(pcg64_state *state) {
uint64_t next;
if (state->has_uint32) { // 如果有未使用的 uint32_t 类型的随机数
state->has_uint32 = 0; // 标志位复位
return state->uinteger; // 返回未使用的 uint32_t 类型的随机数
}
next = pcg64_random_r(state->pcg_state); // 生成一个新的随机数
state->has_uint32 = 1; // 设置标志位,表示有未使用的 uint32_t 类型的随机数
state->uinteger = (uint32_t)(next >> 32); // 提取新生成的随机数的高 32 位作为 uint32_t 类型的随机数
return (uint32_t)(next & 0xffffffff); // 返回新生成的随机数的低 32 位
}
# 定义一个静态内联函数 `pcg64_cm_next64`,用于生成下一个 64 位随机数(带状态变更)
static inline uint64_t pcg64_cm_next64(pcg64_state *state) {
return pcg_cm_random_r(state->pcg_state); // 调用 PCG64-CM 随机数生成器生成下一个 64 位随机数
}
# 定义一个静态内联函数 `pcg64_cm_next32`,用于生成下一个 32 位随机数(带状态变更)
static inline uint32_t pcg64_cm_next32(pcg64_state *state) {
uint64_t next;
if (state->has_uint32) { // 如果有未使用的 uint32_t 类型的随机数
state->has_uint32 = 0; // 标志位复位
return state->uinteger; // 返回未使用的 uint32_t 类型的随机数
}
next = pcg_cm_random_r(state->pcg_state); // 生成一个新的随机数
state->has_uint32 = 1; // 设置标志位,表示有未使用的 uint32_t 类型的随机数
state->uinteger = (uint32_t)(next >> 32); // 提取新生成的随机数的高 32 位作为 uint32_t 类型的随机数
return (uint32_t)(next & 0xffffffff); // 返回新生成的随机数的低 32 位
}
# 声明函数 `pcg64_advance`,用于向前推进 PCG64 随机数生成器状态
void pcg64_advance(pcg64_state *state, uint64_t *step);
# 声明函数 `pcg64_cm_advance`,用于向前推进 PCG64-CM 随机数生成器状态
void pcg64_cm_advance(pcg64_state *state, uint64_t *step);
# 声明函数 `pcg64_set_seed`,用于设置 PCG64 随机数生成器的种子和增量
void pcg64_set_seed(pcg64_state *state, uint64_t *seed, uint64_t *inc);
# 声明函数 `pcg64_get_state`,用于获取 PCG64 随机数生成器的状态信息
void pcg64_get_state(pcg64_state *state, uint64_t *state_arr, int *has_uint32,
uint32_t *uinteger);
# 声明函数 `pcg64_set_state`,用于设置 PCG64 随机数生成器的状态信息
void pcg64_set_state(pcg64_state *state, uint64_t *state_arr, int has_uint32,
uint32_t uinteger);
# 结束条件编译指令,结束头文件 `pcg64.h` 的定义
#endif /* PCG64_H_INCLUDED */