Large Number High-precision calculation (Template), large number high-precision calculation Template
High-Precision Computation refers to the number involved in computation (addition, subtraction, factor ......) The range greatly exceeds the operation that can be expressed by the standard data type (integer, real.
Template: includes the addition, subtraction, multiplication, and division of large numbers and int numbers. The template can be expanded continuously.
Code:
/* All tests are available, but cannot be used for negative number operations. Only positive numbers can be used for large number operations */const int ten [4] = }; const int maxl = 300; struct BigNumber {int d [maxl]; char s [maxl]; BigNumber (const char s []) {int len = strlen (s ); d [0] = (len-1)/4 + 1; int I, j, k; for (int I = 1; I <maxl; I ++) d [I] = 0; for (int I = len-1; I> = 0; I --) {j = (len-i-1)/4 + 1; k = (len-i-1) % 4; d [j] + = ten [k] * (s [I]-'0 ');} while (d [0]> 1 & d [d [0] = 0) d [0] --;} BigNumber () {* this = BigNumber ("0 ");} BigNumber (int x) {for (int I = 0; I <maxl; I ++) d [I] = 0; if (! X) d [0] = 1; while (x) {d [++ d [0] = x % 10000; x/= 10000 ;}} bigNumber (long x) {for (int I = 0; I <maxl; I ++) d [I] = 0; if (! X) d [0] = 1; while (x) {d [++ d [0] = x % 10000; x/= 10000 ;}} void print () {int len = d [0]; printf ("% d", d [d [0]); for (int I = len-1; I> = 1; I --) {if (d [I]> = 1000) printf ("% d", d [I]); else if (d [I]> = 100) printf ("0% d", d [I]); else if (d [I]> = 10) printf ("00% d", d [I]); else printf ("000% d", d [I]);} printf ("\ n");} void toString () {int top = 0; int I, j, temp; for (I = 3; I> = 1; I --) if (d [d [0]> = ten [I]) break; temp = d [d [0]; (J = I; j> = 0; j --) {s [top ++] = (char) (temp/ten [j] + '0 '); temp % = ten [j];} for (I = d [0]-1; I> 0; I --) {temp = d [I]; for (j = 3; j> = 0; j --) {s [top ++] = (char) (temp/ten [j] + '0 '); temp % = ten [j] ;}}zero = BigNumber (), d, temp, mid1 [15], a [3005]; bool operator <(const BigNumber & a, const BigNumber & B) {if (. d [0]! = B. d [0]) return. d [0] <B. d [0]; int I; for (I =. d [0]; I> 0; I --) if (. d [I]! = B. d [I]) return. d [I] <B. d [I]; return false;} bool operator> (const BigNumber & a, const BigNumber & B) {if (B. d [0]! =. D [0]) return B. d [0] <. d [0]; int I; for (I = B. d [0]; I> 0; I --) if (. d [I]! = B. d [I]) return B. d [I] <. d [I]; return false;} bool operator = (const BigNumber & a, const BigNumber & B) {int I; if (. d [0]! = B. d [0]) return false; for (I = 1; I <= a. d [0]; I ++) if (a. d [I]! = B. d [I]) return false; return true;} bool operator <= (const BigNumber & a, const BigNumber & B) {return a <B | a = B ;} bool operator >=( const BigNumber & a, const BigNumber & B) {return a> B | a = B;} BigNumber operator + (const BigNumber &, const BigNumber & B) {BigNumber c; c. d [0] = max (. d [0], B. d [0]); int I, x = 0; for (I = 1; I <= c. d [0]; I ++) {x =. d [I] + B. d [I] + x; c. d [I] = x % 10000; x/= 10000;} while (x! = 0) {c. d [++ c. d [0] = x % 10000; x/= 10000;} return c;} BigNumber operator-(const BigNumber & a, const BigNumber & B) {BigNumber c; c. d [0] =. d [0]; int I, x = 0; for (I = 1; I <= c. d [0]; I ++) {x = 10000 +. d [I]-B. d [I] + x; c. d [I] = x % 10000; x = x/10000-1;} while (c. d [0]> 1) & (c. d [c. d [0] = 0) c. d [0] --; return c;} BigNumber operator * (const BigNumber & a, const BigNumber & B) {BigNumber c; c. d [0] =. d [0] + B. d [0]; int I, j, x; (I = 1; I <=. d [0]; I ++) {x = 0; for (int j = 1; j <= B. d [0]; j ++) {x =. d [I] * B. d [j] + x + c. d [I + J-1]; c. d [I + J-1] = x % 10000; x/= 10000;} c. d [I + B. d [0] = x;} while (c. d [0]> 1) & (c. d [c. d [0] = 0) -- c. d [0]; return c;} bool smaller (const BigNumber & a, const BigNumber & B, int delta) {if (. d [0] + delta! = B. d [0]) return. d [0] + delta <B. d [0]; int I; for (I =. d [0]; I> 0; I --) if (. d [I]! = B. d [I + delta]) return. d [I] <B. d [I + delta]; return true;} void Minus (BigNumber & a, const BigNumber & B, int delta) {int I, x = 0; for (I = 1; I <=. d [0]-delta; I ++) {x = 10000 +. d [I + delta]-B. d [I] + x;. d [I + delta] = x % 10000; x = x/10000-1;} while (. d [0]> 1) & (. d [. d [0] = 0). d [0] --;} BigNumber operator * (const BigNumber & a, const int & k) {BigNumber c; c. d [0] =. d [0]; int I, x = 0; for (I = 1; I <=. d [0]; I ++) {x =. d [I] * k + x; c. d [I] = x % 10000; x/= 10000;} while (x> 0) {c. d [++ c. d [0] = x % 10000; x/= 10000;} while (c. d [0]> 1) & (c. d [c. d [0] = 0) c. d [0] --; return c;} BigNumber operator/(const BigNumber & a, const BigNumber & B) {BigNumber c; d = a; int I, j, temp; mid1 [0] = B; for (int I = 1; I <= 13; I ++) mid1 [I] = mid1 [I-1] * 2; for (I =. d [0]-B. d [0]; I> = 0; I --) {temp = 8192; for (j = 13; j> = 0; j --) {if (smaller (mid1 [j], d, I) {Minus (d, mid1 [j], I); c. d [I + 1] + = temp;} temp/= 2 ;}} c. d [0] = max (1,. d [0]-B. d [0] + 1); while (c. d [0]> 1) & (c. d [c. d [0] = 0) c. d [0] --; return c;} BigNumber operator % (const BigNumber & a, const BigNumber & B) {BigNumber c = a/B; return a-B * c ;}
What are the common big data computing software packages in C/C ++?
In order to solve the problem of big number calculation, software developers and researchers from all over the world have done a lot of research and practice. In recent years, more and more developers have begun to pay attention to the role of software engineering. In order to reduce repetitive work and improve the quality of software and code reuse, many excellent large data computing libraries have emerged. Below is a brief introduction to several excellent large data libraries:
GMP: the GMP big data library was part of the GNU project and was born in 1991. As a big integer arithmetic database of any precision, it includes all kinds of basic arithmetic operations of any precision, such as signed integers, rational numbers, and floating point numbers. It is a c language library, but the official c ++ packaging class, the main application direction is cryptography, network security, algebra systems, computing science and so on. The GMP database runs very fast. its official website says it is the fastest big data database on the Earth, but the GMP database only provides mathematical computing functions, there are no advanced cryptographic functions.
Miracl: the Miracl library is a large database developed by Shamus Software Ltd. Its license is free for educational science research or non-commercial destination applications. It is a c language library for applications and provides several simple c ++ packaging classes. It not only provides high-precision mathematical operations for large integers and scores, but also provides function modules in many cryptographic algorithms, such as SHA, AES, and DSA. It also provides many underlying functional modules in the Elliptic Curve Cryptography system. Since the internal implementation of the Miracl library uses a lot of assembly-Layer Code, the running speed is also very fast.
Crypto ++: the Crypto ++ library is an open-source project and free to use. Because it is a pure c ++ library, the application is very convenient, the library structure is clear, easy to use, and the documentation is also very sound. The Crypto ++ Library provides many cryptographic algorithms.
OpenSSL: OpenSSL is an open-source software package that implements SSL and related encryption technologies. It was written by Eric Yang of Canada. It can encrypt and decrypt message digests, files, digital certificates, digital signatures, and random numbers. Its main purpose is not to use a large number of libraries, but to implement and apply the SSL protocol. However, it also has some functions about big integers. In addition, it is also based on the C language.
Reference: hi.baidu.com/...1.html
How to calculate large numbers in C Language
/* High-precision Power Calculation for any precision large number
In previous articles, we saw an algorithm that uses the 10000 hexadecimal method to store data using arrays.
The principle is as follows:
The counting method is described as follows:
In decimal and other decimal systems, weights and numbers are used to count (as if the term is incorrect and cannot be clearly remembered:
For example
Num = 123456790
The size of this number is:
0*10 ^ 0 + 9*10 ^ 1 + 7*10 ^ 2 +... + 1*10 ^ 8
We can write this number as follows:
123 456 790
Make a = 123, B = 456, c = 790
So abc looks like 123456790.
Do you understand this?
We can segment a large number without considering its overflow,
You only need to consider whether the number of segments is greater than one.
For example:
Above, the maximum value of a is 999, and that of bc is the same. We only need to ensure that these three numbers do not overflow.
Then, num will not overflow.
Another multiplication.
The Abacus left by our ancestors is wonderful,
It is actually one of the most basic computers.
When we calculate the multiplication,
Multiply by only one number:
The column is as follows:
123456790
* 2 =
--------------
246913580
That is:
123 456 790
* 2 = * 2 = * 2 =
----------------
246 912 (1) 580 (overflow) The third paragraph has overflow and is added to the previous section.
------------------
246 913 580
Oh, that's it. It's just like an abacus, carry.
So far, we have solved the overflow and multiplication calculation problems that need to be calculated. We only need to read the code:
The program uses an array containing 1024 unsigned integers (up to 65536) to store each segment of data.
Each number is a segment, and each data can represent a number as large as 9999 (easy to carry)
Calculate it once and check whether it exceeds 9999. If it exceeds 10000, subtract from this section,
Then, enter 1 to the previous digit (that is, the previous digit) (this can be called "10 thousand hexadecimal ")
The program can calculate any two operators less than 13605 to the power of 2 and greater than 0 to the power of 2.
In fact, this is not necessary at all, but I think it is fun and enjoyable.
In addition, with the help of logarithm, you can easily calculate these,
In contrast, this program has no errors.
I call this algorithm "10 thousand hexadecimal 'abacus ":
*/
# Include "stdio. h"
Int main (void)
{
Static unsigned int temp [1024];/* store data in segments */
Unsigned int position = 1;/* The total number of records */
Int overflow = 0;/* indicates whether the record overflows when calculating each segment */
Long
Times = 10000, tm_cnt, sgn_cnt;/* The default value is 10000, which can be changed. Two counters (multiplication times, segment location )*/
Tem ...... remaining full text>