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?
#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;
}
//----------------------------------------------------------------------
Questions & Comments