简体   繁体   English

除数时如何存储C中的所有浮点值

[英]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....在正常的 C 语言中,当除以一个数字时,结果数字是浮点值,但它在浮点后仅显示 6 个小数位我想将小数点后的所有浮点数存储在一个数字中我该怎么做这个例子:一般 22/7 = 3.142857142857142857....
but C language stores only 3.142857 how can I store all the numbers that appear after floating point.但是 C 语言只存储 3.142857 如何存储浮点后出现的所有数字。

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.浮点格式表示具有数字序列的数字,例如 3142857,以及告诉我们将小数点放在哪里的指数,即 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.鉴于此,我们可以看到您的计算机不可能存储所有 22/7 的十进制数字,因为有无限多的数字,但您的计算机只有有限数量的 memory。 Even if the format stored a million digits, that would not be enough to store all the digits of 22/7.即使该格式存储了一百万位数字,也不足以存储 22/7 的所有数字。

The formats commonly used for float and double are binary floating-point formats. floatdouble常用的格式是二进制浮点格式。 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.例如,虽然 3/10 可以用十进制格式精确表示,但我们用float表示它为 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.对于大多数目的,如果结合知识和技能使用double算术就足够了。 You can print more digits of 22/7 by explicitly requesting them, as with printf("%.16g\n", 22./7);您可以通过显式请求打印更多 22/7 数字,例如printf("%.16g\n", 22./7); . .

What do you mean with but C language stores only 3.142857 ?但是 C 语言仅存储 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? C 存储的远不止这些,看看你可以在 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)浮点数有一个指数(8 位 IEEE-754 标准浮点数,11 位双精度数)和一个尾数(分别为 23 和 52 位浮点数和双精度数)

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: 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.除了指数之外,每个浮点表示都有一定数量的数字可以表示,但您需要了解数学的实际执行方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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