boost cpp_rational seems to convert wrong to int when the numerator and denominator have many digits.
#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;
}
The output is:
bad convert: 651604461605/17589017589 37.0461 -3 -3
good convert: 152/41 3.70732 3 3
Also, attempts to convert cpp_rational to cpp_int fail to compile, e.g., using
cpp_int b = static_cast<cpp_int> (a);
cpp_int b = a.convert_to<cpp_int>();
What I want to happen is to divide and round down and never get it wrong even close to an int.
Help? Thanks.
The docs say
So, what you're seeing is a lossy conversion. Demonstrating that the conversion would be disallowed if you hadn't specified it explicitly:
So, what you're doing with all the explicit casts, is telling the compiler to "Shut up; I know what I'm doing". Hence why you didn't get the warning.
Now, how to fix things: keep the division in the multi-precision domain:
Your sample fixed: Live On Coliru