PrecNum - a library to use arbitrary precision numbers in C++ applications
Sometimes it's the case that the built-in number tpyes are not enough/sufficient:
The PrecNum library is designed to work with with numbers as they are used in the classical paper (written) form, like:
The usage is quite simple and straightforward, we only have to use the TPrecNum class:
TPrecNum op1("56.89"), op2("107.00067"), op3; cout << op1 + op2 << endl << op1 * op2 << endl << op1 / op2 << endl // default division, see later << op1.Div(op2, 8) << endl // divison until the 8th decimal << op1.Div(op2, op3, 8) << endl // the previous divison, the remainder is in op3 << op1.Sqrt(5) << endl // get the square root with 5 decimals ([...].%%%%%) << op1.Sqrt(op2, 5) << endl // get the square root and the remainder in op2 << op1.Pow(7) << endl // raise it to the 7th power << op1.Factorial() // for _very_big_ results... << op1.Rand(5.2) // generate a random number in %%%%%.%% form) |
The remainder can be important in some cases, we can't afford to neglect it.
TPrecNum op1(100), result, remainder; result = op1.Div(remainder, TPrecNum(37)); cout << (result * result) + remainder; // this gives _exactly_ 100 result = op1.Sqrt(remainder, 5); cout << (result * result) + remainder << endl; // precisely 100 again |
Attila NAGY | Last updated: 2018-08-30 |