C/C ++ algorithm example (5)-high-precision computing

Source: Internet
Author: User

Definition of high precision data:
Type
HP = array [1 .. maxlen] of integer;

1. High-Precision Addition

Procedure plus (A, B: HP; var C: HP );
VaR I, Len: integer;
Begin
Fillchar (C, sizeof (C), 0 );
If a [0]> B [0] Then Len: = A [0] else Len: = B [0];
For I: = 1 to Len do begin
INC (C [I], a [I] + B [I]);
If C [I]> 10 then begin Dec (C [I], 10); Inc (C [I + 1]); end; {carry}
End;
If C [Len + 1]> 0 then Inc (LEN );
C [0]: = Len;
End; {plus}

2. High-Precision Subtraction
Procedure substract (A, B: HP; var C: HP );

VaR I, Len: integer;
Begin
Fillchar (C, sizeof (C), 0 );
If a [0]> B [0] Then Len: = A [0] else Len: = B [0];
For I: = 1 to Len do begin
INC (C [I], a [I]-B [I]);
If C [I] <0 then begin Inc (C [I], 10); Dec (C [I + 1]); end;
While (LEN> 1) and (C [Len] = 0) Do Dec (LEN );
C [0]: = Len;
End;

3. High Precision multiplied by low precision

Procedure multiply (A: HP; B: longint; var C: HP );
VaR I, Len: integer;
Begin
Fillchar (C, sizeof (C), 0 );
Len: = A [0];
For I: = 1 to Len do begin
INC (C [I], a [I] * B );
INC (C [I + 1], (a [I] * B) Div 10 );
C [I]: = C [I] mod 10;
End;
INC (LEN );
While (C [Len]> = 10) Do begin {process the carry of the highest bit}
C [Len + 1]: = C [Len] Div 10;
C [Len]: = C [Len] mod 10;
INC (LEN );
End;
While (LEN> 1) and (C [Len] = 0) Do Dec (LEN); {adjust Len if carry is not needed}
C [0]: = Len;
End; {multiply}

4. High Precision multiplied by High Precision

Procedure high_multiply (A, B: HP; var C: HP}
VaR I, j, Len: integer;
Begin
Fillchar (C, sizeof (C), 0 );
For I: = 1 to a [0] Do
For J: = 1 to B [0] Do begin
INC (C [I + J-1], a [I] * B [J]);
INC (C [I + J], C [I + J-1] Div 10 );
C [I + J-1]: = C [I + J-1] mod 10;
End;
Len: = A [0] + B [0] + 1;
While (LEN> 1) and (C [Len] = 0) Do Dec (LEN );
C [0]: = Len;
End;

5. High Precision divided by low precision

Procedure devide (A: HP; B: longint; var C: HP; var D: longint );
{C: = a div B; D: = a mod B}
VaR I, Len: integer;
Begin
Fillchar (C, sizeof (C), 0 );
Len: = A [0]; D: = 0;
For I: = Len downto 1 do begin
D: = D * 10 + A [I];
C [I]: = D Div B;
D: = D mod B;
End;
While (LEN> 1) and (C [Len] = 0) Then Dec (LEN );
C [0]: = Len;
End;

6. High Precision divided by High Precision

Procedure high_devide (A, B: HP; var C, D: HP );
VaR
I, Len: integer;
Begin
Fillchar (C, sizeof (C), 0 );
Fillchar (D, sizeof (D), 0 );
Len: = A [0]; d [0]: = 1;
For I: = Len downto 1 do begin
Multiply (D, 10, d );
D [1]: = A [I];
While (compare (D, B)> = 0) do {that is, D> = B}
Begin
Subtract (D, B, d );
INC (C [I]);
End;
End;
While (LEN> 1) and (C. s [Len] = 0) Do Dec (LEN );
C. Len: = Len;
End;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.