The principle of high precision pressure four-bit and its realization __ACM

Source: Internet
Author: User
Tags arithmetic

We all know the int type can represent a range of 32bit: [ -2147483648,2147483647] Long type 64bit can represent a range: [ -9223372036854775808,9223372036854775807]

An int can fully express a 9-digit number, and a long long can fully express 18 bits. (a full expression means any number that can be represented to that bit.)

int, long Long is no longer appropriate when we want to express thousands of digits. So we introduce the concept of high precision.

the so-called high precision is the use of Arrays to act as a container of numbers. Open up a number of spaces, each unit containing a fixed number of data.

The most common methods of high accuracy include: Uncompressed method, 4-bit compression method, and 9-bit compression method.

uncompressed: An array of mainly char types, with one digit in each element.

For example:

2156897546

Stored with a normal uncompressed method, whose data structure is:

6 4 5 7 9 8 6 5 1 2


Note: in this case, the values in the array are not ascll corresponding characters. The reason why the reverse storage is to facilitate the future operation of the overflow position.

Compression 4-bit method: an array of type int that holds four digits in each element.

PS: Why it is four-bit. Because the int type can fully express any number within 9 digits. The four-bit number is stored because it is possible to have four-bit x four-bit in the multiplication, and the result will not be more than eight digits. The int type can be stored entirely. If more than four digits are stored, the int cannot be fully stored in its result and therefore cannot be calculated correctly.

For example:

2156897546

Stored using the compression 4 method, whose data structure is:

7546 5689 21st


features: from right to left, each selected 4 bits are stored in one cell of the array. Finally not enough to put a separate space. (four-bit internal or positive sequence, the overall is in reverse order)

Compression 9-bit method: the same as the compression 4-bit method, the difference is that the stored array is a long long type, each element holds 9 digits.

For example:

2156897546

Stored using the compression 9 method, whose data structure is:

156897546 21st

Three methods are in the final analysis of the conversion problem: ordinary compression is equivalent to the decimal, four-bit compression is equivalent to 10,000, nine-bit compression is equivalent to 1 billion into the system.

The time efficiency of three methods is different: Suppose two numbers are arithmetic. The common law store has the most units, followed by the four-bit compression method, at least nine-bit compression. In addition, in two arrays, one of the arrays is the starting point, and each element of the array is added to each unit of the other. Its time complexity is O (n^2) level. Therefore, the compressed 9-bit storage method to open up a small number of units, so consumes the least time.


Here we only introduce the construction of the four-bit compression method and the example of multiplication and addition calculation (if possible, the other arithmetic =_= will be filled later)

int strtonum (const char str[], int num[])//From the last reverse direction of the string, an int array is assigned a positive sequence (an array of int is a large number in reverse order)
{
    int len=strlen (str), i,j=0 ;
    for (i=len-1;i>2;i-=4)/reverse descending every 4 digits in a unit
    {
        if (i-3>=0)
            num[j++]= (str[i-3]-' 0 ') *1000+ (str[i-2]-') 0 ') *100+ (str[i-1]-' 0 ') *10+ (str[i]-' 0 ');
    }
    if (i==0)//If the remaining 1 digits
        num[j++]=str[0]-' 0 ';
    else if (i==1)//remaining 2 digits
        num[j++]= (str[0]-' 0 ') *10+ (str[1]-' 0 ');
    else if (i==2)//remaining 3 digits
        num[j++]= (str[0]-' 0 ') *100+ (str[1]-' 0 ') *10+ (str[2]-' 0 ');
        while (j>0&&num[j-1]==0)	//j Returns the length of Num
            j--;
    return j;
}

int Add (int num1[],int num2[],int num3[],int len1,int len2,int&)/addition Num3_begin,			Num2 is an array of raw data, num3 to store the result {int i=0,j=len1>len2?len1:len2;
	Len1,len2 represents the length of the array num1,num2, Num3_begin returns the subscript that begins with the value in the result.  The IF (len1==0&&len2==0)//raw data is a 0 case discussion.
	0:NUM1, num2 are 0, 1:NUM1 is 0 2:num2 is 0 return 0;
	else if (len1==0) return 1;
	else if (len2==0) return 2; else {while (I<J)///cycle times are len1, and len2 the largest number {num3[i]+=num1[i]+num2[i];//add if (num3[i]>=10000)//If the value is greater than the
			Carry Operation num3[i+1]+=num3[i]/10000,num3[i]%=10000;
		i++;
		} if (num3[i]==0)//returns the subscript num3_begin=i-1 of the value beginning in the result;
	else num3_begin=i;
} return-1; }
int Multiply (int num1[], int num2[], int num3[], int len1, int len2,int &num3_begin)/Multiply the    number stored in the num1,num2 is from subscript 0 starting backwards stored in.
{
    int i=0,j=0,k=0;
    if (len1==0| | len2==0)		//num1, num2 one side is 0 return
        0;
    else
        {while
            (i<len1)
            {
                k=i;
                j=0;
                while (J<len2)
                {
					 num3[k]+=num1[i]*num2[j];		Standard originally may have number, so use + +
					 if (num3[k]>=10000)
						num3[k+1]+=num3[k]/10000,num3[k]%=10000;
					 k++,j++;
                }
                i++
            }
        }
        if (num3[k]!=0)		//return number of the highest bit is not 0 subscript
            num3_begin=k;
        else
            num3_begin=k-1;
    return 1;
}



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.