Search code examples
c++stringboostgmparbitrary-precision

Accuracy loss when calculating magnitudes <1 with magnitudes >1


I'm testing string constructed gmp_float.

This code

#include <boost/multiprecision/number.hpp>
#include <boost/multiprecision/gmp.hpp>
#include <iostream>

using namespace boost::multiprecision;

typedef number<gmp_float<15>> mp_type;

int main()
{
    mp_type total("1.01");
    cout << total.str(0) << endl;
    mp_type first_addition(".01");
    cout << first_addition.str(0) << endl;
    total += first_addition;
    cout << total.str(0) << endl;
}

prints

1.01
0.01
1.01999999999999999998

Why? I ran more tests, and in this particular case, it doesn't matter what the operation is so long as the magnitude of one number is >0 and <1 while the other's is >1.

From the link above

It is not possible to round-trip objects of this type to and from a string and get back exactly the same value. This appears to be a limitation of GMP.

Are there any other zones where accuracy is lost?


Solution

  • Boost Multiprecision has decimal floating point numbers too:

    See it Live On Coliru prints:

    clang++ -std=c++11 -Os -Wall -pedantic main.cpp && ./a.out
    1.01
    0.01
    1.02
    

    #include <boost/multiprecision/cpp_dec_float.hpp>
    #include <iostream>
    
    using namespace boost::multiprecision;
    using std::cout;
    using std::endl;
    
    typedef cpp_dec_float_50 mp_type;
    
    int main()
    {
        mp_type total("1.01");
        cout << total.str(0)          << endl;
    
        mp_type first_addition(".01");
        cout << first_addition.str(0) << endl;
    
        total += first_addition;
        cout << total.str(0)          << endl;
    }