Is there a C++ equivalent to Java's BigDecimal?

后端 未结 8 1764
逝去的感伤
逝去的感伤 2020-11-27 19:42

I\'m looking for a C++ class that can do decimal floating point arithmetic. Looking through http://speleotrove.com/decimal/ there are links to all sorts of classes that peop

相关标签:
8条回答
  • 2020-11-27 20:02

    Probably "MAPM, A Portable Arbitrary Precision Math Library in C" is what you are looking for. It also includes C++ Wrappers:

    http://www.tc.umn.edu/~ringx004/mapm-main.html

    0 讨论(0)
  • 2020-11-27 20:06

    Take your pick. There are a bunch of them out there. For instance, you can consult the list on Wikipedia.

    0 讨论(0)
  • 2020-11-27 20:06

    Use GMP and store everything as cents. If you know that you won't pass 2^32 cents (42.949673 million dollars) use a 32 bit unsigned int (or use a 64 bit unsigned int) and keep it simple.

    0 讨论(0)
  • 2020-11-27 20:07

    I may be too late for this but would 128bit decimals work? These have been accepted into C++ and at least gcc has them since gcc-4.5 (we're starting 4.9 now:

    #include <iostream>
    #include <decimal/decimal>
    
    using namespace std;
    
    int main()
    {
      {
        std::decimal::decimal32 dn(.3), dn2(.099), dn3(1000), dn4(201);
        dn-=dn2;
        dn*=dn3;
        cout << "decimal32 = "  << (dn==dn4) << " : " << decimal32_to_double(dn) << endl;
      }
    
      {
        std::decimal::decimal64 dn(.3), dn2(.099), dn3(1000), dn4(201);
        dn-=dn2;
        dn*=dn3;
        cout << "decimal64 = "  << (dn==dn4) << " : " << decimal64_to_double(dn) << endl;
      }
    
      {
        std::decimal::decimal128 dn(.3), dn2(.099), dn3(1000), dn4(201);
        dn-=dn2;
        dn*=dn3;
        cout << "decimal128 = " << (dn==dn4) << " : " << decimal128_to_double(dn) << endl;
      }
    
      return 0;
    }
    

    Note there is decimal32 equal in size to float, decimal64 equal in size to most double. So decimal128 is pretty big. From Wikipedia: Decimal128 supports 34 decimal digits of significand and an exponent range of −6143 to +6144, i.e. ±0.000000000000000000000000000000000×10−6143 to ±9.999999999999999999999999999999999×106144. (Equivalently, ±0000000000000000000000000000000000×10−6176 to ±9999999999999999999999999999999999×106111.)

    The mpfr library is arbitrary precision binary floating point - not arbitrary precision decimal. There is a difference.

    0 讨论(0)
  • 2020-11-27 20:11

    The question is a bit old, but for other people that have the same need : Boost.multiprecision is probably what you're looking for.

    http://www.boost.org/doc/libs/1_57_0/libs/multiprecision/doc/html/boost_multiprecision/tut/floats/cpp_dec_float.html

    It's an arbitrary precision library that can handle 10-based decimals.

    0 讨论(0)
  • 2020-11-27 20:14

    There exists a huge library called GMP (GNU multiple precision library) which supports this and also has C++ bindings, though to be honest the C++ interface is a bit wonky and outdated.

    An example from the documentation, the following creates a float called f with at least 500 bits of precision:

    mpf_class f(1.5, 500);
    
    0 讨论(0)
提交回复
热议问题