mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3mobile wallpaper 4
349 words
2 minutes
High-Precision Arithmetic Study Notes
2022-07-08

High-precision#

Normally we implement addition, subtraction, multiplication, and division directly using +-*/, but when the numbers grow to lengths of 100 or 1000, the storage range of int and long long is no longer sufficient; at this point it is time to use high-precision.

1. High-precision addition A+B#

1.1 How it works#

First is addition between large numbers, which can be simulated by following the normal steps of addition. For example, see the figure below:

EZr2Gb4qHlkJixe.png

You can see that addition is performed from right to left, so we can reverse the obtained digits to invert their order, and after computation output in forward order.

1.2 Examples#

Reference code:

void solve(){
//...
for(int i=0,c=0;i<max(a.length(),b.length()) || c;i++){
int t1=0,t2=0;
if(i<a.length()) t1 = a[i]-'0';
if(i<b.length()) t2 = b[i]-'0';
int t = t1 + t2 + c;
c = t / 10;t %= 10;
ans.push_back(t+'0');
}
//...
}

2. High-precision subtraction A-B#

2.1 How it works#

Next is subtraction between large numbers. The method is still similar to high-precision addition; the difference is that due to the relative sizes of A and B, the result may be negative. In that case we can write A-B = -(B-A) to keep the subtraction result non-negative.

P.S. Note that because this is subtraction, leading zeros may appear; remember to remove them before output.

2.2 Examples#

Reference code:

bool comp(){
if(a.length()!=b.length()) return a.length()>b.length();
for(int i=a.length()-1;~i;i--)
if(a[i]!=b[i]) return a[i]>b[i];
return true;
}
void solve(){
//...
if(!comp()) p = 1,swap(a,b);
for(int i=0,c=0;i<a.length();i++){
int t1=0,t2=0;
if(i<a.length()) t1 = a[i]-'0';
if(i<b.length()) t2 = b[i]-'0';
int t = t1 - t2 + c;
if(t < 0) c=-1,t+=10;
else c = 0;
ans.push_back(t+'0');
}reverse(ans.begin(),ans.end());
if(p) cout<<"-";p=-1;
while(++p<ans.length() && ans[p]=='0');
ans = ans.substr(min(p,(int)(ans.length()-1)),max((int)(ans.length()-p),1));
//...
}

3. High-precision multiplication A*b#

3.1 How it works#

Multiplication between a large number and a small number is relatively straightforward: use a variable to record the carry of the products of each digit of A with b, and obtain the result for each digit in sequence.

P.S. Note that when the large number is 0, the product may have leading zeros; be sure to remove them.

3.2 Examples#

Reference code:

void solve(){
for(int i=0,c=0;i<a.length() || c;i++){
int t1 = 0;
if(i<a.length()) t1 = a[i]-'0';
int t = t1 * b + c;
ans.push_back(t%10+'0');c = t / 10;
}
}

4. High-precision division A/b#

4.1 How it works#

Similar to the multiplication of large numbers by small numbers, the division of a large number by a small number is also straightforward. You can divide the leading part of the large number from left to right by b, recording the quotient and remainder of each prefix with c/b.

P.S. Note that in earlier stages the dividend may be too small, causing leading zeros; remember to remove them before output.

4.2 Examples#

Reference code:

void solve(){
for(int i=0;i<a.length();i++){
int t1 = a[i]-'0';
int t = t1 + 10 * c;
ans.push_back(t/b + '0');
c = t%b;
}int p = -1;while(++p<ans.length() && ans[p]=='0');
ans = ans.substr(min(p,(int)(ans.length()-1)),max(1,(int)(ans.length()-p)));
}
Share

If this article helped you, please share it with others!

High-Precision Arithmetic Study Notes
https://dreaife.tokyo/en/posts/high-precision-calcs/
Author
dreaife
Published at
2022-07-08
License
CC BY-NC-SA 4.0

Some information may be outdated

Related Posts Smart
1
Sorting and Binary Search Study Notes
algorithm This article introduces the principles and implementations of quicksort and merge sort, including time complexity and related problems. Quicksort partitions the sequence into two parts around a midpoint, while merge sort merges sorted subarrays. It also discusses binary search algorithms for integers and floating-point numbers and their implementations.
2
Algorithm Study: Bitwise Operations, Discretization, and Interval Merging
algorithm This article introduces algorithms for bitwise operations, discretization, and interval merging. Bitwise operations are used to process binary numbers, discretization is used to optimize storage and queries for sparse data, and interval merging uses sorting and overlap checks to efficiently merge multiple intervals. Related examples and reference code are provided to help understand their applications.
3
Algorithm Study: Prefix Sum, Difference Array, and Two Pointers
algorithm This article introduces the principles and applications of prefix sums, difference arrays, and the two-pointer technique. Prefix sums are used to quickly compute subarray sums, difference arrays are used to efficiently handle range updates, and the two-pointer method optimizes query efficiency by maintaining pointers. Related examples and reference code are provided to help understand and apply these algorithms.
4
Interview Algorithm Study 1
algorithm This post contains multiple algorithm interview problems and solutions, including snake matrix filling, quicksort on a singly linked list, finding peaks and local minima, the egg hardness problem, a stack supporting minimum retrieval, and finding the entry node of a cycle in a linked list. Each problem includes a detailed description, input/output format, and sample code.
5
NumPy Study Notes 2
cs-base This article introduces many NumPy features, including bitwise operations, string operations, mathematical functions, statistical functions, sorting and conditional filtering, byte swapping, array copies and views, the matrix library, linear algebra, file input/output, and integration with Matplotlib. It provides detailed function descriptions and sample code to help readers understand and apply various NumPy capabilities.

Table of Contents