Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#13148 closed Bugs (fixed)

Zero value cannot be converted from cpp_dec_float_50 to cpp_rational

Reported by: Lev Sch <zorechfan@…> Owned by: John Maddock
Milestone: To Be Determined Component: multiprecision
Version: Boost 1.64.0 Severity: Problem
Keywords: Cc:

Description

String "boost::multiprecision::cpp_rational result(d1);" eats CPU forever.

It is reproduced on boost_1_65_0_beta1 and boost_1_64_0.


boost::multiprecision::cpp_rational rationalfromStr(const char* str)
{
  boost::multiprecision::cpp_dec_float_50 d1(str);
  boost::multiprecision::cpp_rational result(d1); // <--- eats CPU forever
  return result;
}

void test()
{
  // this example is OK
  {
    boost::multiprecision::cpp_rational expected=1;
    assert(expected == rationalfromStr("1"));
  }
  // this example is OK
  {
    boost::multiprecision::cpp_rational expected=boost::multiprecision::cpp_rational(25)/boost::multiprecision::cpp_rational(10);
    assert(expected == rationalfromStr("2.5"));
  }
  // this example is OK
  {
    boost::multiprecision::cpp_rational expected=boost::multiprecision::cpp_rational(5)/boost::multiprecision::cpp_rational(1000);
    assert(expected == rationalfromStr("0.005"));
  }
  // this example is OK
  {
    boost::multiprecision::cpp_rational expected=0;
    assert(expected == boost::multiprecision::cpp_rational("0")); // direct cpp_rational from str is ok
  }
  // this example fails
  {
    boost::multiprecision::cpp_rational expected=0;
    // reacheble code
    assert(expected == rationalfromStr("0")); // cpp_rational from cpp_dec_float_50 is not ok
    // unreacheble code
  }
}

Change History (5)

comment:1 by Jacob Garber <jgarber1@…>, 5 years ago

Compiling with -fsanitize=undefined reveals an integer overflow:

/usr/local/include/boost/multiprecision/detail/generic_interconvert.hpp:433:22: runtime error: negation of -2147483648 cannot be represented in type 'typename cpp_dec_float<50, int, void>::exponent_type' (aka 'int'); cast to an unsigned type to negate this value to itself

I can't reproduce with 1.63. It looks like this was introduced in the fix for #12625.

comment:2 by John Maddock, 5 years ago

Resolution: fixed
Status: newclosed

comment:3 by anonymous, 5 years ago

Hello. Which boost version the fix will be available?

comment:4 by anonymous, 5 years ago

I've just this second merged to master, so it should be in 1.67.

in reply to:  4 comment:5 by anonymous, 5 years ago

Replying to anonymous: thanks

Note: See TracTickets for help on using tickets.