We know that computers have a significant characteristic of being fast in computation and high in precision. Especially when dealing with larger data sets, the advantages of computers become even more apparent. As of December 15, 2019, the fastest computer in China is “Tianhe-1”. So how do computers perform calculations?
Example: Write a program in C++ to calculate the sum of two numbers.
Program:
#include<iostream>using namespace std;int main(){ int a,b; cin>>a>>b;//Input a and b cout<<a+b;//Output the sum of a and b return 0;}
Calculating the sum of two numbers is very easy to write in C++. The existing integer data types in C++ are int and long long. We know that the maximum value of an int is 10 to the 9th power, which is a 10-digit number, while the maximum value of a long long can reach 10 to the 19th power, which is a 20-digit number. So the question arises: what if a and b have many digits? For example: if a and b are 100-digit integers or 1000-digit integers, how do we compute their sum? This is what we will discuss today: high precision addition.
Example: Given two positive integers with 10000 digits, we need to add them and output the result.
Analysis:
(1) The algorithm for high precision addition is quite simple; it is actually the