High-Precision Big Data calculation (Template)

Source: Internet
Author: User

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 available */const int ten [4] = {300,}; const int maxl =; 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 ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.