[Boost-bugs] [Boost C++ Libraries] #9579: conversion error from boost cpp_rational to int

Subject: [Boost-bugs] [Boost C++ Libraries] #9579: conversion error from boost cpp_rational to int
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2014-01-16 00:13:08


#9579: conversion error from boost cpp_rational to int
-------------------------------------------+----------------------
 Reporter: W Randolph Franklin <boost@…> | Owner: turkanis
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: rational
  Version: Boost 1.55.0 | Severity: Problem
 Keywords: cpp_rational conversion |
-------------------------------------------+----------------------
 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.

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/9579>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:15 UTC