3

I tried the following:

#include <cmath>
#include <iostream>
#include <boost/multiprecision/cpp_bin_float.hpp>

using float256 = boost::multiprecision::number<boost::multiprecision::backends::cpp_bin_float<192,
                  boost::multiprecision::backends::digit_base_2, void, long long int>, boost::multiprecision::et_off>;

void Test()
{
   double a = exp(-0.5);
   double b = boost::multiprecision::exp(float256(-0.5)).convert_to<double>();
   cout << a << endl << b << endl;
}

Unfortunately this doesn't work. I am getting 0.606531 for a but 1 for b.

I'm running: boost 1.58, Linux, gcc 5.4.0.

Any idea what I am doing wrong?

3
  • Any idea what I am doing wrong? -- Try a later version of boost. You are 7 versions behind the current. Oct 17, 2017 at 20:11
  • Well, that's the version from Ubuntu repositories. I will try a newer one. I didn't expect boost to contain such basic errors. Oct 17, 2017 at 20:29
  • I have boost 1.62
    – n. m.
    Oct 17, 2017 at 21:10

1 Answer 1

2

This looks like a bug in boost. When the exponent template argument is a 64-bit integer type, the result of exp is rounded to an integer. This doesn't happen when int is used in place of long long int.

Edit: I have tracked the bug to the faulty implementation of floor and ceil functions. The offending line in both instances is:

if((arg.exponent() > (int)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::max_exponent) || (shift <= 0))
//                    --- <=== !!!!

in include/boost/multiprecision/cpp_bin_float.hpp.

3
  • I tried Boost 1.65.1 and the problem persists. I reported the error, I will post a link if the bug is confirmed. Do you know if there is an arbitrary precision float library that is better unit-tested? Oct 18, 2017 at 20:11
  • There ae many, but do you really need a 64 bit exponent? Do you plan to use numbers greater than 2^(2^30)?
    – n. m.
    Oct 18, 2017 at 20:28
  • Great! This is the issue: svn.boost.org/trac10/ticket/13264 I think I can easily work with 32 bit exponents. But I makes a bad feeling if the very first think one tries with a new library immediately fails. Oct 19, 2017 at 5:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.