Number of super-long digits processed

Source: Internet
Author: User
Tags float number

 

Float and double data types are single-precision and double-precision data types. Their values are respectively from the negative power of 3.4E + 10 to the 38 power of 3.4E + 10, and the negative 308 power of 1.7E + 10 to the 308 power of 1.7E + 10.

How can a float 6-7 effective number be loaded with a power of 3.4E + 10, a power of 38, and a power of 15-16, a double character? How can a power of 308, a power of, or a power of, be loaded with a power of 1.7E + 10? number. Isn't that a conflict?

 

Answer: No conflict. Float 6-7 digits refer to the number of digits (precision) of valid numbers rather than the value size. For example, 3.14159267 has nine valid digits, but the value is 3 ~ 4, while 350 has three digits, the size is 300 ~ In the range of 400. Therefore, the float number can reach 3.4E + 10, but the number of valid digits can only reach 6-7. If 3.14159267 is assigned to a float variable, the accuracy will be reduced. For example

Float a = 3234567.1;

Float B = 3234567;

If (a = B)

Printf ("YES ");

Else

Printf ("NO ");

YES will be output, because the 11 at the end of a exceeds the float and can only reach 6-7 digits. (If a = 1234567.1; B = 3234567) the input result is NO. Why? This requires us to analyze: how to deal with the excess precision? It is not rounding, but the loss of binary digits. Therefore, sometimes the precision can reach 6 bits, and sometimes the precision can reach 7 bits, depending on the binary representation of the number.

 

So how can we express super-long digits and super-high-precision digits? For example, 123456789123456789123456789 (a large integer with a length of 30 digits), for example 3.14159012345678901234567890123 (a decimal number with a precision of 30 digits), long float cannot be stored for such a long number, this requires the help of "string" or "character array.

Unsigned _ int64 n;

The unsigned _ int64 Type Variable n has a maximum value of more than 1234567892345678912 (20 bits) and can reach about 1.8E + 19. Generally, it should be enough. _ Int64 type data cannot be output using cout in C ++. It should be that cout does not reload this type. If printf is used for output, it is clear that % d, % f, % l cannot meet the accuracy of 20 bits. printf ("% I64d \ n", n) can be used in VC6. However, the supported bits cannot exceed 20 bits, if the output exceeds 9.23E + 18, an error occurs. The best way is to convert the "long digits" into a string, as shown below:

Char buffer [65];

Printf ("% s", _ ui64toa (n, buffer, 10 ));

Function _ ui64toa is responsible for converting n into a string and storing it in the character Array buffer [65]. 10 indicates converting to a 10th hexadecimal format.

Convert a number to a string:

 

# Include <stdlib. h>

# Include <stdio. h>

Int main (void)

{

Char buffer [65];

Int r;

For (r = 10; r> = 2; -- r)

{

_ Itoa (-1, buffer, r );

Printf ("base % d: % s (% d chars) \ n", r, buffer, strlen (buffer ));

}

Printf ("\ n ");

For (r = 10; r> = 2; -- r)

{

_ I64toa (-1L, buffer, r );

Printf ("base % d: % s (% d chars) \ n", r, buffer, strlen (buffer ));

}

Printf ("\ n ");

For (r = 10; r> = 2; -- r)

{

_ Ui64toa (0 xffffffffffffffl, buffer, r );

Printf ("base % d: % s (% d chars) \ n", r, buffer, strlen (buffer ));

}

}

 

 

Output

Base 10:-1 (2 chars)

Base 9: 12068657453 (11 chars)

Base 8: 37777777777 (11 chars)

Base 7: 211301422353 (12 chars)

Base 6: 1550104015503 (13 chars)

Base 5: 32244002423140 (14 chars)

Base 4: 3333333333333333 (16 chars)

Base 3: 102002022201221111210 (21 chars)

Base 2: 11111111111111111111111111111111 (32 chars)

 

Base 10:-1 (2 chars)

Base 9: 145808576354216723756 (21 chars)

Base 8: 1777777777777777777777 (22 chars)

Base 7: 45012021522523134134601 (23 chars)

Base 6: 3520522010102100444244423 (25 chars)

Base 5: 2214220303114400424121122430 (28 chars)

Base 4: 33333333333333333333333333333333 (32 chars)

Base 3: 11112220022122120101211020120210210211220 (41 chars)

Base 2: 1111111111111111111111111111111111111111111111111111111111111111 (64 chars)

 

Base 10: 18446744073709551615 (20 chars)

Base 9: 145808576354216723756 (21 chars)

Base 8: 1777777777777777777777 (22 chars)

Base 7: 45012021522523134134601 (23 chars)

Base 6: 3520522010102100444244423 (25 chars)

Base 5: 2214220303114400424121122430 (28 chars)

Base 4: 33333333333333333333333333333333 (32 chars)

Base 3: 11112220022122120101211020120210210211220 (41 chars)

Base 2: 1111111111111111111111111111111111111111111111111111111111111111 (64 chars)

 

PS: You can use this function to convert a decimal integer into a binary string;

Int main (void)

{

Char buffer [65];

_ Itoa (12, buffer, 2 );

Printf ("base % d: % s (% d chars) \ n", r, buffer, strlen (buffer ));

}

 

Another way is to define a character array to store the number of extra-long digits, and the decimal point can also be solved. Then, define the algorithm between these strings and overload operators, it is said that this operation is still very efficient.

 

From zollty's column

Related Article

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.