270 likes | 423 Views
HKOI Intermediate Training. High Precision Arithmetic. Introduction. Overview. Introduction :: Overview. HKOI Intermediate Training :: High Precision Arithmetic. Why HPA (High Precision Arithmetic)?. LongInt (32-bit signed) : -2147483648 .. 2147483647
E N D
HKOI Intermediate Training High Precision Arithmetic Introduction Overview
Introduction :: Overview HKOI Intermediate Training :: High Precision Arithmetic Why HPA (High Precision Arithmetic)? • LongInt (32-bit signed) : -2147483648 .. 2147483647 • Cardinal (32-bit unsigned): 0 .. 4294967295 • Int64 (64-bit signed) : -9223372036854775808 .. 9223372036854775807 • - QWord (64-bit unsigned): 0 .. 18446744073709551615 • Sometimes, we need to deal with really large numbers (10000 digits~) • Often integrated with other problems • Don’t Fear
Introduction :: Overview HKOI Intermediate Training :: High Precision Arithmetic HPA Operations • Addition, Subtraction (negative addition) • Multiplication, Division • Arbitrary base arithmetic • 123(8) + 456(8) = 601(8) • 45A(H) + C9F(H) = 10F9(H) • Usually only with integers
Introduction :: Overview HKOI Intermediate Training :: High Precision Arithmetic HPA Extensions • Exponent (^), Modulus (mod) • Factorial (!) • Composed of basic HPA operations • Recommended : Write your standard HPA functions • in a modular manner • - Recommended : Do not modify the operands Procedure HPAadd(A as HPAtype; B as HPAtype; var C as HPAtype); Function HPAadd(A as HPAtype; B as HPAtype) : HPAtype;{may not work!}
HKOI Intermediate Training High Precision Arithmetic Introduction Representation
Introduction :: Representation HKOI Intermediate Training :: High Precision Arithmetic Common Representations • Array of byte • Type HPAtype = Array[1..1024] of byte; • Advantage : Can apply the integer operations directly • String / ANSIstring • Type HPAtype = ANSIstring; • Advantage : Simple Input Strength of each one is the weakness of the other!
Introduction :: Representation HKOI Intermediate Training :: High Precision Arithmetic Variation in Representations • Zero based or One-based? • Type HPAtype = Array[1..1024] of byte; • Type HPAtype = Array[0..1023] of byte; • Zero based : Corresponds to our concept of digit-value • However, it is not applicable to Strings • Can pack multiple digits into the same cell • Byte/ShortInt - 2 digits • SmallInt - 4 digits • Longint/Cardinal - 9 digits • Int64 - 18 digits • QWord - 19 digits (!) • Advantage : Reduces looping overhead, saves memory
Introduction :: Representation HKOI Intermediate Training :: High Precision Arithmetic Input Considerations • Highly related to your representation • Arrays : Usually the lowest cell stores • the least significant digit (LSD) Array [ 8 7 6 5 4 3 2 1 ] 00 1 3 1 0 7 2 • Strings : If you use readln() directly, • the lowest cell stores the MSD! • You may have to reverse the string first, • in order to make the HPA functions easy • to understand / debug String [ 1 2 3 4 5 6 7 8 ] 1 3 1 0 7 2 0 0
Introduction :: Representation HKOI Intermediate Training :: High Precision Arithmetic Output Considerations • Arrays : When to stop? • Solution 1 : Loop from the end (MSD) back • to the start, and check for the • first non-zero digit • Problem : Can be very time-consuming • Solution 2 : Keep a “size” variable • Problem : Your number can expand or shrink during operations • Increase size whenever needed • Use Solution 1, but loop from size back to the start to check Array [ 8 7 6 5 4 3 2 1 ] 00 1 3 1 0 7 2
Introduction :: Representation HKOI Intermediate Training :: High Precision Arithmetic Output Considerations • Strings : Simple? No. • You have to take care of the length! • ShortStrings : s[0] := chr(size); • - ANSIstring : setlength(s,size); • Since ANSIstring is a pointer type and is dynamic, you must • set the length before accessing the individual cells Var S : ANSIstring; S[5] := ‘x’; SetLength(S,5); Writeln(S); { RUNTIME ERROR } Var S : ANSIstring; SetLength(S,5); S[5] := ‘x’; Writeln(S); { WORKS FINE }
HKOI Intermediate Training High Precision Arithmetic Basic Operations Addition
Basic Operations :: Addition HKOI Intermediate Training :: High Precision Arithmetic How do we do addition? • For normal numeric types, there are hard wired • circuits for + - * / • For HPA types, there is no standard way of doing so, • so you have to implement your own “addition algorithm” • - RETURN TO THE PRIMARY SCHOOL!
Basic Operations :: Addition HKOI Intermediate Training :: High Precision Arithmetic Addition Algorithm • Assumed : 2 non-negative integers • Add the digits one by one • Keep a “carry” • Think about the exact process now • Extra Problem : When to stop? [ 4 3 2 1 ] 1 2 3 +) 4 5 ---------- 1 6 8 [ 4 3 2 1 ] 3 9 +) 8 5 ---------- 1 2 4
Basic Operations :: Addition HKOI Intermediate Training :: High Precision Arithmetic Addition Algorithm • Lazy method: • Carry := 0; • For I := 1 to Max(SizeA,SizeB)+5 Do Begin • C[I] := A[I] + B[I] + Carry; • Carry := C[I] mod 10; • C[I] := C[I] div 10; • If (C[I] <> 0) and (I > SizeC) SizeC := I; • End; [ 4 3 2 1 ] 1 2 3 +) 4 5 ---------- 1 6 8 [ 4 3 2 1 ] 3 9 +) 8 5 ---------- 1 2 4 • Does it always work? • - Think about the growth rate of Carry
Basic Operations :: Addition HKOI Intermediate Training :: High Precision Arithmetic Negative Numbers • In the previous section, we assumed that • the integers to be added are both non-negative • If negative values are allowed, the simple • add-and-carry approach will not succeed, • moreover, even the representation has problems! • More cases : • Negative + Negative = Negative • Smaller Positive + Bigger Negative = Negative • Bigger Positive + Smaller Negative = Positive [ 4 3 2 1 ] 3 9 +) - 8 5 ---------- ?????
Basic Operations :: Addition HKOI Intermediate Training :: High Precision Arithmetic Negative Representation • You can use a flag (e.g. 1 or –1) • Negative + Negative : • Do it the normal way, but mark the output as Negative • Positive + Negative : • Check the absolute sizes, and do a subtraction • Then set a correct flag in the output [ 4 3 2 1 ] 3 9 +) - 8 5 ---------- ?????
HKOI Intermediate Training High Precision Arithmetic Basic Operations Subtraction
Basic Operations :: Subtraction HKOI Intermediate Training :: High Precision Arithmetic Subtraction Algorithm • Assumed : 2 non-negative integers • Subtract instead of adding the digits • Borrow when necessary • When to stop : You should have figured it out • New problem : The size may shrink [ 4 3 2 1 ] 3 9 -) 8 5 ---------- - 4 6
Basic Operations :: Subtraction HKOI Intermediate Training :: High Precision Arithmetic Subtraction Algorithm • Pseudocode • GreatThan(A,B) :- • Returns TRUE if A is greater than B • (It checks the negative flag of A and B) • If (GreaterThan(A,B)) Then • C->Flag := +1; • C->Number := NonNegativeSubtract(A->Number,B->Number); • Else • C->Flag := -1; • C->Number := NonNegativeSubtract(B->Number,A->Number); • End If [ 4 3 2 1 ] 3 9 -) 8 5 ---------- - 4 6
HKOI Intermediate Training High Precision Arithmetic Basic Operations Multiplication
Basic Operations :: Multiplication HKOI Intermediate Training :: High Precision Arithmetic Multiplication Algorithm • Positive/Negative is no longer a problem • How many digits will the output have? • Each output digit is a summation of multiplied values • In the example : 7 = 1*5 + 2*9 + 5*9/10 (carry) • How many terms can there be? • General Strategy : Use 2 loops [ 4 3 2 1 ] 1 9 *) 2 5 ---------- 4 7 6
Basic Operations :: Multiplication HKOI Intermediate Training :: High Precision Arithmetic Forward Multiplication • Loop through each (A[I],B[J]) combination • Where does A[I]*B[J] -> C[K] go? • During the multiplication process, you can • accumulate up the values without worrying • about the size of integers (why?) • Finally, make the number stored in each cell • single-digited [ 4 3 2 1 ] 1 9 *) 2 5 ---------- 4 7 6
Basic Operations :: Multiplication HKOI Intermediate Training :: High Precision Arithmetic Backward Multiplication • Loop through each (C[K],A[I]) combination • What is the corresponding B[J]? • This algorithm can be adapted such that every • number calculated can be printed instantly (how?) [ 4 3 2 1 ] 1 9 *) 2 5 ---------- 4 7 6
HKOI Intermediate Training High Precision Arithmetic Basic Operations Division
Basic Operations :: Division HKOI Intermediate Training :: High Precision Arithmetic Division Algorithm • Positive/Negative is also not a problem • This is the most difficult operation • Align the MSD of the operands • Repeatly subtract the divider instead of “finding the • correct multiple” • When shifting the divider, do it blindly instead of • finding the first non-zero digit. This will simplify • the work and reduce the chance of bug
HKOI Intermediate Training High Precision Arithmetic Extended Operations Others
Basic Operations :: Multiplication HKOI Intermediate Training :: High Precision Arithmetic H-L Precision Multiplication • Sometimes, you need to multiply a large number • with a ordinary sized one • Example : Factorial • Use integer / longint for each cell • Do the multiplication directly for each digit, • and finally convert it into single-digited