Big Number C++ library
This article - or better the downloadable attachments - describe the behavior of a BigNumber C++ library – called bn.h that is an acronym for Big Number - developed by myself, performing the standard four operations: sum, subtraction, multiplication and division between two BIG NUMBERS (in the following B.N.) using the standard well-know-lower-school-teached-algorithms. The definition of B.N. is: number with a quantity of digits that it's impossible and/or difficult to deal with them as usual C++ - signed or unsigned - long double number, bigger than LDBL_MAX and/or lesser than LDBL_MIN.
Now, considering that LDBL_MAX/MIN are platform dependent – the values are different for different machines architecture – as a rule of thumbs those values are about: LDBL_MIN=3.3621e-4932 and LDBL_MAX=1.18973e+4932; in practical problems this is not an issue. Some problems start to come up when you consider the precision; for a C++ long double type the precision – that is always platform dependent – has usually the value of 18. This value is the “number of digits (in decimal base) that can be represented without change” (from http://www.cplusplus.com/reference/limits/numeric_limits/). In more technical words, the C++ code:
#include <climits> std::cout << "long double minimum = " << std::numeric_limits<long double>::min() << std::endl; std::cout << "long double maximum = " << std::numeric_limits<long double>::max() << std::endl; std::cout << "long double precision = " << std::numeric_limits<long double>::digits10 << std::endl;
long double minimum = 3.3621e-4932 long double maximum = 1.18973e+4932 long double precision = 18
So, rephrasing, the following B.N. definition should be used: is a B.N. any number that has to be used in calculations requiring a precision greater than the precision of a C++ long double type.
Functions in bn.h library are NOT THREAD SAFE, because they use variables passed by reference. It will not be difficult to “transform” those functions in “thread-safe”, replacing the return value of each function with a C++ structs; should be this behavior will be adjusted –time allowing– in a next release.
All the primary functions - that are: SumB(), SubB(), MulB(), InvB(), DivB() - are located in the core module “bn.h”; in the module “functions.h” are located secondary functions- in this release just one function PowB() - that are considered secondary because functions that call primary functions to operate.
Just to give you a taste of the library usage, the following is a call to MulB() function performing product between two B.N.
strNum1 = "1234567890"; strNum2 = "1234567890.1010"; bolSignNum1 = true; bolSignNum2 = false; bolSignResult = false; isError = false; strSeparator = "."; strResult = MulB(strNum1,strNum2,bolSignNum1,bolSignNum2,bolSignResult,isError,strSeparator); cout << strNum1 << " * (-" << strNum2 << ") = " << strResult << endl; cout << "bolSignResult = " << bolSignResult << endl; cout << "isError = " << isError << endl;
1234567890 * (-1234567890.1010) = 1524157875143743456.89 bolSignResult = 0 isError = 0
Library (source code) and User/Reference Manual should be downloaded at the bottom of current article. The project was developed using Code::Block and the source code include the CodeBlock complete project.
This code is released under GPL version 3 - Copyright (C) 2015 Berta Danilo
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.