High precision algorithm package based on class and object

High precision formula (don’t ask me where it comes from, you can’t find it on the Internet because this is what I wrote)

🙂

1. High-precision addition: add to carry and go to zero
2. High-precision subtraction: judge to subtract and go to zero
3. High-precision multiplication: multiply to estimate and then go to zero
4. High-low precision multiplication: multiply to carry and go to zero

#include

#include

using namespace std;

class BIGINT{
public:
int num[505];
int length;
bool positive;
BIGINT(
string str=""){
memset(num,
0, sizeof(num));
positive
= true;
if(str == ""){
return;
}
else{
length
= str.length();
for(int i = 0; i){
num[i] = str[str.length()-i-1]-'0';
}
}
}
void show(){
if(positive == false){
cout
<<"-";
}
for(int i = length-1; i>=0; i--){
cout
<<num[i];
}
}
};

int compare(BIGINT &a,BIGINT &b){
if(a.length > b.length){
return 1;
}
else if(a.length < b.length){
return -1;
}
else{
int maxlen = max(a.length,b.length);
for(int i = maxlen-1; i>=0; i--){
if(a.num[i] > b.num[i ]){
return 1;
}
else if(a.num[i] < b.num[i]){
return -1;
}
}
return 0;
}
}

BIGINT bigadd(BIGINT
&a,BIGINT &b){
BIGINT c;
int maxlen = max(a.length,b.length);
//Add
for(int i = 0;i){
c.num[i] = a.num[i] + b.num[i];
}
//carry
for(int i = 0;i){
if(c.num[i]>=10){
c.num[i
+1] += 1;
c.num[i]
= c.num[i]% 10;
}
}
//Go to zero
if(c.num[maxlen] == 0){
c.length
= maxlen;
}
else{
c.length
= maxlen + 1;
}
return c;
}

BIGINT bigminus(BIGINT
&a,BIGINT &b){
BIGINT c;
int maxlen = max(a.length,b.length);
//Judge
int maxint = compare(a,b);
if(maxint == 1){
//Subtract
for(int i = 0;i){
if(a.num[i]>=b.num[ i]){
c.num[i]
= a.num[i] - b.num[i];
}
else{
a.num[i
+1] -= 1;
a.num[i]
+= 10;
c.num[i]
= a.num[i] - b.num[i];
}
}
}
else if(maxint == -1){
c
= bigminus(b,a);
c.positive
= false;
}
//Go to zero
for(int i = maxlen+1;i>=0;i--){
if(c.num[i] != 0){
c.length
= i+1;
return c;
}
}
c.length
= 1;
return c;
}

BIGINT bigmultiply(BIGINT
&a,BIGINT &b){
BIGINT c;
//Multiply
for(int i = 0;i){
for(int j = 0;j){
c.num[i+j] += a.num[i]*b.num[j];
c.num[i
+j+1] += c.num[i+j]/10;
c.num[i
+j] %= 10;
}
}
//estimated
int len = a.length + b.length;
//Go to zero
for(int i = len+1;i>=0;i--){
if(c.num[i] != 0){
c.length
= i+1;
return c;
}
}
c.length
= 1;
return c;
}

BIGINT bigmultiplywithsmall(BIGINT a,
int b){
//Multiply
for(int i = 0;i){
a.num[i] *= b;
}
//carry
for(int i = 0;i1;i++){
if(a.num[i]>=10){
a.num[i
+1] += a.num[i] / 10;
a.num[i]
%= 10;
}
}
//Go to zero
if(a.num[a.length] != 0< span style="color: #000000;">){
a.length += 1;
}
return a;
}

int main(){
BIGINT a(
"111111111");
BIGINT b(
"111111111");
BIGINT tmp;

tmp
= bigadd(a,b);
cout
<<"High precision addition:";
tmp.show();
cout
<<endl;

tmp
= bigminus(a,b);
cout
<<"High precision subtraction:";
tmp.show();
cout
<<endl;

tmp
= bigmultiply(a,b);
cout
<<"High precision multiplication:";
tmp.show();
cout
<<endl;

tmp
= bigmultiplywithsmall(a,100);
cout
<<"High and low precision multiplication:";
tmp.show();
cout
<<endl;
}

High-precision calculations refer to the range of numbers involved in calculations Type, real type) can represent the range of operations. For example, find the sum of two 20,000-digit numbers. At this time, it is necessary to use high-precision algorithms.

#include

#include

using namespace std;

class BIGINT{
public:
int num[505];
int length;
bool positive;
BIGINT(
string str=""){
memset(num,
0, sizeof(num));
positive
= true;
if(str == ""){
return;
}
else{
length
= str.length();
for(int i = 0; i){
num[i] = str[str.length()-i-1]-'0';
}
}
}
void show(){
if(positive == false){
cout
<<"-";
}
for(int i = length-1; i>=0; i--){
cout
<<num[i];
}
}
};

int compare(BIGINT &a,BIGINT &b){
if(a.length > b.length){
return 1;
}
else if(a.length < b.length){
return -1;
}
else{
int maxlen = max(a.length,b.length);
for(int i = maxlen-1; i>=0; i--){
if(a.num[i] > b.num[i ]){
return 1;
}
else if(a.num[i] < b.num[i]){
return -1;
}
}
return 0;
}
}

BIGINT bigadd(BIGINT
&a,BIGINT &b){
BIGINT c;
int maxlen = max(a.length,b.length);
//Add
for(int i = 0;i){
c.num[i] = a.num[i] + b.num[i];
}
//carry
for(int i = 0;i){
if(c.num[i]>=10){
c.num[i
+1] += 1;
c.num[i]
= c.num[i]% 10;
}
}
//Go to zero
if(c.num[maxlen] == 0){
c.length
= maxlen;
}
else{
c.length
= maxlen + 1;
}
return c;
}

BIGINT bigminus(BIGINT
&a,BIGINT &b){
BIGINT c;
int maxlen = max(a.length,b.length);
//Judge
int maxint = compare(a,b);
if(maxint == 1){
//Subtract
for(int i = 0;i){
if(a.num[i]>=b.num[ i]){
c.num[i]
= a.num[i] - b.num[i];
}
else{
a.num[i
+1] -= 1;
a.num[i]
+= 10;
c.num[i]
= a.num[i] - b.num[i];
}
}
}
else if(maxint == -1){
c
= bigminus(b,a);
c.positive
= false;
}
//Go to zero
for(int i = maxlen+1;i>=0;i--){
if(c.num[i] != 0){
c.length
= i+1;
return c;
}
}
c.length
= 1;
return c;
}

BIGINT bigmultiply(BIGINT
&a,BIGINT &b){
BIGINT c;
//Multiply
for(int i = 0;i){
for(int j = 0;j){
c.num[i+j] += a.num[i]*b.num[j];
c.num[i
+j+1] += c.num[i+j]/10;
c.num[i
+j] %= 10;
}
}
//estimated
int len = a.length + b.length;
//Go to zero
for(int i = len+1;i>=0;i--){
if(c.num[i] != 0){
c.length
= i+1;
return c;
}
}
c.length
= 1;
return c;
}

BIGINT bigmultiplywithsmall(BIGINT a,
int b){
//Multiply
for(int i = 0;i){
a.num[i] *= b;
}
//carry
for(int i = 0;i1;i++){
if(a.num[i]>=10){
a.num[i
+1] += a.num[i] / 10;
a.num[i]
%= 10;
}
}
//Go to zero
if(a.num[a.length] != 0< span style="color: #000000;">){
a.length += 1;
}
return a;
}

int main(){
BIGINT a(
"111111111");
BIGINT b(
"111111111");
BIGINT tmp;

tmp
= bigadd(a,b);
cout
<<"High precision addition:";
tmp.show();
cout
<<endl;

tmp
= bigminus(a,b);
cout
<<"High precision subtraction:";
tmp.show();
cout
<<endl;

tmp
= bigmultiply(a,b);
cout
<<"High precision multiplication:";
tmp.show();
cout
<<endl;

tmp
= bigmultiplywithsmall(a,100);
cout
<<"High and low precision multiplication:";
tmp.show();
cout
<<endl;
}

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2804 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.