1

分子と分母の桁数が多い場合、boost cpp_rational が誤って int に変換されるようです。

#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>

using namespace boost::multiprecision;
using namespace std;

int main() {
  cpp_rational a("4561231231235/123123123123");
  std::cout << "bad convert: " << a << ' '  << 
     float(a) << ' ' << int(a) << ' ' << 
     a.convert_to<int>() << endl;
  a = (cpp_rational)"456/123";
  std::cout << "good convert: " << a << ' '  <<
     float(a) << ' ' << int(a) << ' ' << 
     a.convert_to<int>() << endl;
}

出力は次のとおりです。

bad convert: 651604461605/17589017589 37.0461 -3 -3
good convert: 152/41 3.70732 3 3

また、cpp_rational を cpp_int に変換しようとしてもコンパイルに失敗します。

cpp_int b = static_cast<cpp_int> (a);
cpp_int b = a.convert_to<cpp_int>();

私がしたいのは、整数に近づいても、除算して切り捨て、決して間違えないようにすることです。

ヘルプ?ありがとう。

4

1 に答える 1