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
About an EOA Wallet Signature Verification and Related Content
WEB3 This article provides an in-depth analysis of the complete signature verification process for an Ethereum EOA wallet and the mathematical principles behind it. It first reviews the fundamentals of the secp256k1 curve, including the finite field Fₚ, the elliptic curve point group E(Fₚ), the base point G, and its order n, then explains in detail how point addition, point doubling, and scalar multiplication are computed modulo p and modulo n. It then uses a real SIWE (Sign-In with Ethereum) scenario to show step by step how a wallet generates the r/s/v tuple after receiving a signature request, including hash calculation, random nonce k generation, deriving the R point, and the specific formulas for r, s, and v. Next, it explains how the server verifies ownership without exposing the private key by using the known r, s, v, message hash e, and base point G to recover the public key Q with the formula Q = r⁻¹(sR − eG), then deriving the wallet address by taking the last 20 bytes of the keccak-256 hash. The article also highlights the difference between p and n, the computational difficulty of the elliptic curve discrete logarithm problem (approximately 2¹²⁸), and the potential impact of current quantum computing on this security. Overall, it provides developers with a complete reference from theory to implementation and serves as an SEO-friendly blog summary that improves search visibility for related keywords such as “EOA wallet signature verification,” “secp256k1,” “ECDSA,” and “SIWE.”

Table of Contents