[英]Why does Boost multiprecision not work with Boost rational number library?
我正在將Boost 1.55.0與clang 3.5.0和gcc 4.8.1一起使用。
現在,我想計算最多256個階乘(無精度損失):
#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
int main(){
using boost::multiprecision::uint128_t;
using boost::rational;
using std::cout;
using std::endl;
typedef unsigned long long unsigned_int;
// typedef uint128_t unsigned_int;
rational<unsigned_int> r((unsigned_int)1,
//((unsigned_int)1)<<127);
~(((unsigned_int)-1)>>1));
unsigned_int n_I = 1;
cout << "0!:\t\t" << r << endl;
cout << "1!:\t\t" << r << endl;
for(unsigned_int i=2; i<257; ++i){
r *= i;
cout << i << "!:\t\t" << r << endl;
}
return 0;
}
旁注:大階乘在二進制表示形式中有許多尾隨零,因此我從值為1 /(2 ^ 127)的有理變量開始。 這將自動使分子盡可能小。
我的問題:它不與工作uint128_t
從升壓多倍! 但它確實適用於unsigned long long
!
這是我的終端輸出:
~/ccpp_projects/facultiy $ clang++ -I /usr/local/include/boost-1_55 faculty.cpp -o faculty
In file included from faculty.cpp:51:
In file included from /usr/local/include/boost-1_55/boost/multiprecision/cpp_int.hpp:12:
In file included from /usr/local/include/boost-1_55/boost/multiprecision/number.hpp:22:
In file included from /usr/local/include/boost-1_55/boost/multiprecision/detail/generic_interconvert.hpp:9:
In file included from /usr/local/include/boost-1_55/boost/multiprecision/detail/default_ops.hpp:2073:
/usr/local/include/boost-1_55/boost/multiprecision/detail/no_et_ops.hpp:25:4: error: implicit instantiation of undefined template
'boost::STATIC_ASSERTION_FAILURE<false>'
BOOST_STATIC_ASSERT_MSG(is_signed_number<B>::value, "Negating an unsigned type results in ill-defined behavior.");
^
/usr/local/include/boost-1_55/boost/static_assert.hpp:36:48: note: expanded from macro 'BOOST_STATIC_ASSERT_MSG'
# define BOOST_STATIC_ASSERT_MSG( B, Msg ) BOOST_STATIC_ASSERT( B )
^
/usr/local/include/boost-1_55/boost/static_assert.hpp:169:13: note: expanded from macro 'BOOST_STATIC_ASSERT'
sizeof(::boost::STATIC_ASSERTION_FAILURE< BOOST_STATIC_ASSERT_BOOL_CAST( __VA_ARGS__ ) >)>\
^
/usr/local/include/boost-1_55/boost/rational.hpp:533:15: note: in instantiation of function template specialization
'boost::multiprecision::operator-<boost::multiprecision::backends::cpp_int_backend<128, 128, 0, 0, void> >' requested here
num = -num;
^
/usr/local/include/boost-1_55/boost/rational.hpp:139:61: note: in instantiation of member function
'boost::rational<boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<128, 128, 0, 0, void>, 0> >::normalize' requested here
rational(param_type n, param_type d) : num(n), den(d) { normalize(); }
^
faculty.cpp:63:28: note: in instantiation of member function 'boost::rational<boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<128, 128,
0, 0, void>, 0> >::rational' requested here
rational<unsigned_int> r((unsigned_int)1, ~(((unsigned_int)-1)>>1));
^
/usr/local/include/boost-1_55/boost/static_assert.hpp:87:26: note: template is declared here
template <bool x> struct STATIC_ASSERTION_FAILURE;
^
1 error generated.
附錄
我只是用g ++編譯了代碼,就可以在那工作了! 有什么方法可以禁用clang ++的BOOST STATIC ASSERT嗎?
normalize()
的實現假定翻轉基礎整數類型的符號( i = -i
)是已定義的操作。
unsigned long long
就是這種情況,而uint128_t
不是 。
Yould
使用cpp_rational
(請參閱Live On Coliru上的內容 )
手動分解出2的冪: Live On Coliru ,輸出:
0!: 1 1!: 1 2!: 1 x 2^1 3!: 3 x 2^1 4!: 3 x 2^3 ... 255!: 62542083004847430224885350954338565259 x 2^247 256!: 62542083004847430224885350954338565259 x 2^255
首先可能是您想要的嗎? 它將更加高效,並防止128位溢出。
#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>
int main(){
using boost::multiprecision::uint128_t;
uint128_t mantissa = 1;
unsigned int binary_exponent = 0;
std::cout << "0!:\t\t" << mantissa << std::endl;
std::cout << "1!:\t\t" << mantissa << std::endl;
for(unsigned i=2; i<257; ++i){
unsigned tmp = i;
while (tmp && ((tmp % 2) == 0))
{
binary_exponent += 1;
tmp /= 2;
}
mantissa *= tmp;
std::cout << i << "!:\t\t" << mantissa << " x 2^" << binary_exponent << std::endl;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.