Deep understanding of handling problems of large number and high precision number _c language

Source: Internet
Author: User
Tags strlen
The float and double data are single and double, and their values are 3.4E+10 minus 38 times to 3.4E+10 of 38, and 1.7E+10 minus 308 times to 1.7E+10 of the 308.

So for float, only 6-7 digits of the effective number, how can be loaded up to 3.4*10^ (-38) Such a large number? Similarly, 15-16-bit double type, also cannot install 1.7*10^ (-308) so large number ah?

Answer: float 6-7 refers to number of digits of a valid number (precision), not numeric size。 For example, 3.14159267 has 9 digits, the number is between 3~4, and 350 has 3 digits, but the value is between 300~400. So the float can reach 3.4E+10, but its effective digital digits can only reach 6-7 digits, if 3.14159267 assigns to a float variable, then the precision will be lost. For example
Copy Code code as follows:

float a=3234567.1;
float b=3234567;
if (a==b)
printf ("YES");
Else
printf ("NO");

Will output yes because 11 at the end of a is beyond the 6-7-bit precision of float. (if a=1234567.1;b=3234567) The input result will be no, why? This requires us to analyze: How to deal with the parts beyond precision? Not rounded, but bits lost. So sometimes it can reach the 6-bit precision, at times it can reach the 7-bit precision, depending on the binary representation of the number.

So what do we want to say about the super-long digits, the very large precision of the number?
For example, 123456789123456789123456789 (large integers with an extra length of 30 digits);
For example, 3.14159012345678901234567890123 (super high precision 30 decimal digits), so long a number, long float can not save, this is the use of "string" or "character array".

unsigned __int64 n;
The unsigned __int64 type of variable n, with a maximum value of over 1234567892345678912 (20 bits), can reach about 1.8E+19, and should normally suffice.

But the __int64 type of data can not be used in C + + cout to output, it should be cout no overload this type, if you use printf to output, obviously%d,%f,%l can not meet the accuracy of 20 digits, online search to VC6 under the use of printf ("%i64d\n", n), but the number of digits supported is no more than 20, and by my test, the result of about 9.23E+18 output will be wrong. The best way to do this is to convert the "long digits" to strings, as follows:
Copy Code code as follows:

Char buffer[65];
printf ("%s", _ui64toa (n, buffer,10));

The function _ui64toa is responsible for converting N to a string, into a character array buffer[65], and 10 represents converting to 10.

The number is converted to a string, and the reference program is as follows:
Copy Code code as follows:

#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 (0xffffffffffffffffL, buffer, R);
printf ("Base%d:%s (%d chars) \ n", r, Buffer, strlen (buffer));
}
}


Copy Code code as follows:

Output
Base:-1 (2 chars)
Base 9:12068657453 (one chars)
Base 8:37777777777 (one chars)
Base 7:211301422353 (chars)
Base 6:1550104015503 (chars)
Base 5:32244002423140 (chars)
Base 4:3333333333333333 (chars)
Base 3:102002022201221111210 (chars)
Base 2:11111111111111111111111111111111 (chars)

Base:-1 (2 chars)
Base 9:145808576354216723756 (chars)
Base 8:1777777777777777777777 (chars)
Base 7:45012021522523134134601 (chars)
Base 6:3520522010102100444244423 (chars)
Base 5:2214220303114400424121122430 (chars)
Base 4:33333333333333333333333333333333 (chars)
Base 3:11112220022122120101211020120210210211220 (chars)
Base 2:1111111111111111111111111111111111111111111111111111111111111111 (chars)

Base 10:18446744073709551615 (chars)
Base 9:145808576354216723756 (chars)
Base 8:1777777777777777777777 (chars)
Base 7:45012021522523134134601 (chars)
Base 6:3520522010102100444244423 (chars)
Base 5:2214220303114400424121122430 (chars)
Base 4:33333333333333333333333333333333 (chars)
Base 3:11112220022122120101211020120210210211220 (chars)
Base 2:1111111111111111111111111111111111111111111111111111111111111111 (chars)

PS: You can use this function to convert a 10-binary integer into a binary string;
Copy Code code as follows:

int main (void)
{
Char buffer[65];
_itoa (A, buffer, 2);
printf ("Base%d:%s (%d chars) \ n", r, Buffer, strlen (buffer));
}

There is also a way to define the character array to hold the number of super long digits, the decimal point can be resolved, and then define their own string form of the super long number of algorithms and overloaded operators, it is said that the operation efficiency is quite high.

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.