我正在尝试使用oder中的boost来计算阶乘。我不知道为什么,但是VS2013给我看了一个编译错误。
有人有想法吗?
int nLgCombi = 12;
std::vector<std::string> NbrToPlay;
...
int ne = NbrToPlay.size();
int calcnc = boost::math::factorial<int>(ne + 1) / boost::math::factorial<int>(nLgCombi);
错误讯息:
Erreur 1错误C2338:!boost :: is_integral :: value d:\ users \ XXXXXX \ downloads \ boost_1_55_0b1 \ boost_1_55_0b1 \ boost \ math \ special_functions \ factorials.hpp 32 1 APPS
编辑:
代码用double替换int:
double dcalcnc = boost::math::factorial<double>(ne +1) / boost::math::factorial<double>(nLgCombi);
错误讯息:
Erreur 1错误C2039:'assert_not_arg':n'est pas membre de'boost :: mpl'd:\ users \ XXXXX \ downloads \ boost_1_55_0b1 \ boost_1_55_0b1 \ boost \ mpl \ aux_ \ preprocessed \ plain \ arg.hpp 45 1
Erreur 2错误C3861:'assert_not_arg':identifyicateur introuvable d:\ users \ XXXXX \ downloads \ boost_1_55_0b1 \ boost_1_55_0b1 \ boost \ mpl \ aux_ \ preprocessed \ plain \ arg.hpp 45 1
非常感谢,
致以最诚挚的问候,
Nixeus
答案 0 :(得分:4)
根据Boost documentation about factorial:
BOOST_STATIC_ASSERT(!boost::is_integral<T>::value);
// factorial<unsigned int>(n) is not implemented
// because it would overflow integral type T for too small n
// to be useful. Use instead a floating-point type,
// and convert to an unsigned type if essential, for example:
// unsigned int nfac = static_cast<unsigned int>(factorial<double>(n));
// See factorial documentation for more detail.
int
和unsigned int
的阶乘版尚未实现,因为即使是较小的值也会导致溢出。
例如,13的阶乘是6227020800,超过了unsigned int
的最大限制(如果整数长度为4个字节,则为4294967295)。
您必须使用double
或float
来计算阶乘。