简体   繁体   中英

How to store all the floating value in C when dividing the number

In normal C language when dividing a number the resultant number is float value but it is showing only 6 decimal places after floating point I want to store all the floating point after decimal in one number How can I do that example: In general 22/7= 3.142857142857142857....
but C language stores only 3.142857 how can I store all the numbers that appear after floating point.

Consider a decimal floating-point format. A floating-point format represents numbers with a sequence of digits, such as 3142857, and an exponent that tells us where to put the decimal point, making 3.142857. Given this, we can see it is impossible for your computer to store all the decimal digits of 22/7, because there are infinitely many digits, but your computer has only a finite amount of memory. Even if the format stored a million digits, that would not be enough to store all the digits of 22/7.

The formats commonly used for float and double are binary floating-point formats. They store a sequence of binary digits (bits) and an exponent (and a sign, + or −). So, in addition to having only a limited number of digits, they will have deviations from decimal formats. For example, while 3/10 is exactly representable in a decimal format, we represent it in float as 1.0011001100110011001101 2 ⋅2 −2 = 5,033,165 / 16,777,216 = 0.300000011920928955078125.

In general, it is also impossible to do perfect real-number arithmetic in computers, for various theoretical reasons, although you can start with the fact that the type of infinity that is the number of real numbers is greater than the type of infinity that is the number of strings of digits.

It is possible to write software that does arithmetic with rational numbers by recording both a numerator and a denominator to represent each number as a fraction. As with all mathematics, though, the computer will still have finite limits, so rational arithmetic can only be done within limited bounds.

For most purposes, using double arithmetic suffices if it is used with knowledge and skill. You can print more digits of 22/7 by explicitly requesting them, as with printf("%.16g\n", 22./7); .

What do you mean with but C language stores only 3.142857 ? C stores much more than this, take a look to How big of a number can you store in double and float in c? :

A floating point number has an exponent (in 8 bits IEEE-754 standard float, 11 bits in double), and a mantissa (23 and 52 bits in float, and double respectively)

Just print the value using the proper format specifier:

#include <stdio.h>

int main(void)
{
    printf("%f\n", 22. / 7);    // output --> 3.142857
    printf("%.25f\n", 22. / 7); // output --> 3.1428571428571427937015414
    return 0;
}

or ask the number of decimal digits that can be printed without losing precision:

#include <stdio.h>
#include <float.h>

int main(void)
{
    printf("%.*g\n", DBL_DECIMAL_DIG, 22. / 7);
    return 0;
}

Output:

3.1428571428571428

Even when you are using a pocket calculator, not all of the digits on that display are actually "significant." Floating-point numbers have been compared to little piles of dirt on the beach: "every time you pick one up and move it around, you pick up a little dirt and you lose a little sand." Every floating-point representation has a certain number of digits that it can represent in addition to the exponent, but you need to understand how the math is actually performed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM