统一的 int32 分布
Uniform int32 Distribution
有没有办法在没有警告的情况下获得统一的 int32_t
分布?
我在我的代码中使用了这个 uniform_int_distribution<int32_t>
但我收到警告:
54988961.cpp: In function ‘int main()’:
54988961.cpp:6:64: warning: overflow in conversion from ‘double’ to ‘int’ changes value from ‘1.0e+10’ to ‘2147483647’ [-Woverflow]
std::uniform_int_distribution<std::int32_t> unif(1,std::pow(10,10));
~~~~~~~~^~~~~~~
这正是我的代码:
#include <cmath>
#include <cstdint>
#include <random>
int main() {
std::uniform_int_distribution<std::int32_t> unif(1,std::pow(10,10));
}
pow(10, 10)
这是10000000000
,一个int32
只能装2147483647
(2^31 - 1
)。如果您希望能够存储 pow(10, 10)
.
,则应使用 int64_t
因为你的最小值是 1
你也可以选择它的未签名对应物。
使用大双精度值 (pow()
) for an integer argument causes this warning in the constructor of uniform_int_distribution
。
改为使用 int 常量。如果您需要一个不适合 int32_t
的范围,请使用 int64_t
模板参数。
有没有办法在没有警告的情况下获得统一的 int32_t
分布?
我在我的代码中使用了这个 uniform_int_distribution<int32_t>
但我收到警告:
54988961.cpp: In function ‘int main()’:
54988961.cpp:6:64: warning: overflow in conversion from ‘double’ to ‘int’ changes value from ‘1.0e+10’ to ‘2147483647’ [-Woverflow]
std::uniform_int_distribution<std::int32_t> unif(1,std::pow(10,10));
~~~~~~~~^~~~~~~
这正是我的代码:
#include <cmath>
#include <cstdint>
#include <random>
int main() {
std::uniform_int_distribution<std::int32_t> unif(1,std::pow(10,10));
}
pow(10, 10)
这是10000000000
,一个int32
只能装2147483647
(2^31 - 1
)。如果您希望能够存储 pow(10, 10)
.
int64_t
因为你的最小值是 1
你也可以选择它的未签名对应物。
使用大双精度值 (pow()
) for an integer argument causes this warning in the constructor of uniform_int_distribution
。
改为使用 int 常量。如果您需要一个不适合 int32_t
的范围,请使用 int64_t
模板参数。