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

2

文档说_

也允许转换:

d = a; // OK, widening conversion.
d = a * b;  // OK, can convert from an expression template too.

然而,本质上是有损的转换要么被声明为显式,要么被完全禁止:

d = 3.14;  // Error implicit conversion from float not allowed.
d = static_cast<mp::int512_t>(3.14);  // OK explicit construction is allowed

因此,您看到的是有损转换。证明如果您没有明确指定转换将被禁止:

cpp_rational a("4561231231235/123123123123");
int i   = a;        // error: cannot convert `cpp_rational` to `int` in initialization
float f = a;        // error: cannot convert `cpp_rational` to `float` in initialization
long double ld = a; // error: cannot convert `cpp_rational` to `long double` in initialization
long long   ll = a; // error: cannot convert `cpp_rational` to `long long int` in initialization

所以,你对所有显式转换所做的事情是告诉编译器“闭嘴;我知道我在做什么”。因此,为什么您没有收到警告。

现在,如何解决问题:将除法保持在多精度域中:

cpp_int v = numerator(a)/denominator(a);

您的样品已修复:Live On Coliru

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

using namespace boost::multiprecision;

int main() {
    cpp_rational a("4561231231235/123123123123");
    cpp_int v = numerator(a)/denominator(a);
    std::cout << "convert: " << v.convert_to<int>() << "\n";

    a = cpp_rational("456/123");
    v = numerator(a)/denominator(a);
    std::cout << "convert: " << v.convert_to<int>() << "\n";
}
于 2014-01-16T22:22:59.450 回答