Arbitrary-precision Decimal Class with Arithmetic Methods

Arbitrary-precision Decimal Class with Arithmetic Methods

A C++ class for arithmetic operations on arbitrary-precision values

  • Language: C/C++
  • Released: Jul 22, 2011
    Last Update: Jul 24, 2011

Have you ever noticed that double precision is not sufficient? Have you ever wanted your software to be able to compute 10^10 + 10^-10 ? If the answer is yes, then this component is definetely for you.

Based on a string representation, this class allows you to work with numbers without any limitations whatsoever on the number of digits being used.

You can perform simple arithmetics (+, -, *, /), to even more complex computations (power, sqrt)

This component is really easy to include in your project, compared to other solutions. So, why would you waste your time installing a whole package (like GMP) when you only want to be able to perform some operations on huge and/or tiny numbers?

Hide

Usage Example

#include 
using namespace std;
#include "BigDecimal.h"
//---------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv[])
{

BigDecimal testa("1234567812345678123456781.2345678");

cout << testa << endl;

string s("12387654321876543218765432187654322");
BigDecimal testb(s);

cout << testb << endl;

testa = BigDecimal("1234.544");
testb.setString("1123.456");
cout << testa + testb << endl;
cout << testa - testb << endl;
cout << testb - testa << endl;
cout << testa + (-testb) << endl;

cout << testa * testb << endl;

cout << testa / testb << endl;
cout << testb / testa << endl;

cout << BigDecimal(4)/3 << endl;

cout << BigDecimal("54321.1234")%5 << endl;

cout << BigDecimal("-54")%14 << endl;

cout << (testa^8) << endl;
cout << (testa^-4) << endl;

++testa = 5;
cout << testa++ << endl;
cout << ++testa << endl;
--testa = 5;
cout << testa-- << endl;
cout << --testa << endl;

testa = BigDecimal("1234.544");

testa += testb;
cout << testa << endl;

testa -= testb;
cout << testa << endl;

testa *= testb;
cout << testa << endl;

testa /= testb;
cout << testa << endl;

testa %= testb;
cout << testa << endl;

testa ^= 5;
cout << testa << endl;

cout << sqrt(testa) << endl;

getchar();

return 0;
}
//----------------------------------------------------------------------
You need to log-in or create an account
  • Create an account
  • Log-in

Please use your real name.

Activation link will be sent to this address.

Minimum 8 characters

Enter your password again

Clicking this button confirms you read and agreed to the terms of use and privacy policy.

X

Save your watchlist

Fill your details below to receive project updates from your watch list - including new versions, price changes and discounts.

I agree to the terms of use and privacy policy.

4 licenses, starting from From » FREE View Licenses 14 day money-back guarantee
Post a comment

Or enter your name and Email
No comments have been posted yet.