用c语言编写的浮点数的范围? [英] Range of magnitude for float in c programming language?
问题描述
由于我们只有32位,这怎么可能呢?上限不应该是2 ^ 32?我试着打印出一个浮点数,循环乘以浮点数10,38次,这就是输出1000000069440617300000000000000000000.0000000000。它也必须跟踪这个符号,那么它如何将所有这些存储在32位?解析方案
参见< a href =https://en.wikipedia.org/wiki/Single-precision_floating-point_format =nofollow noreferrer>单精度浮点格式,了解关于典型C 浮点
。
c语言中浮点数的范围?
#include< float.h>
printf(浮动幅度范围%e到%e \ n,FLT_MIN,FLT_MAX);
//典型结果
//浮动幅度范围1.175494e-38至3.402823e + 38
这是可能的,因为我们只有32位?典型的
sup>不同的值。然而,它们不是线性分布的,而是以线性组对数形式的。浮动
确实只能存储大约2 <32> 32
<2> 23 在范围[2 <-126 < sup>〜2 -127 )
...
2 23 范围[0.5到1.0)
2 23 在范围[1.0至2.0]中的不同值 -
2 23 4.0)
...
2 23 范围内的不同值[2 127到2 128 )
以及它们的负值计数器部分。
也+/- 0零,小的子正常数字,+/-无穷大而不是-a-Number
它还必须跟踪符号,那么它如何将所有这些存储在32位? / b>
1位为符号
8位为二进制指数
23对于重要的位(MSBit通常表示为1)
-
32位
何时打印一个
float
,只有大约6个(FLT_DIG,FLT_DECIMAL_DIG
)有效数字通常很重要。printf(%。* e \\\
,FLT_DIG-1,FLT_TRUE_MIN);
printf(%。* e \\\
,FLT_DIG-1,FLT_MIN);
printf(%。* e \\\
,FLT_DIG-1,acos(-1));
printf(%。* e \\\
,FLT_DECIMAL_DIG - 1,nextafterf(FLT_MAX,0.0));
printf(%。* e \\\
,FLT_DECIMAL_DIG - 1,FLT_MAX);
输出
1.40130e-45 // min sub-normal
1.17549e-38 // min normal normal
3.14159e + 00 // pi
3.40282326e + 38 //最大值之前的数字
3.40282347e + 38 //最大
In The C Programming Language book by Dennis Ritchie, it is mentioned that "A float number is typically a 32-bit quantity, with at least six significant digits and magnitude generally between about 10^-38 and 10^+38."
How is that possible since we have only 32-bits? Shouldn't the top limit be 2^32? I tried printing out a float by looping and multiplying a float by 10, 38 times and this was the output 100000006944061730000000000000000000000.000000 . It also has to keep track of the sign , so how is it storing all this in just 32-bits?
解决方案See Single-precision floating-point format for a details about a typical C
float
.Range of magnitude for float in c programming language?
#include <float.h> printf("float magnitude range %e to %e\n", FLT_MIN, FLT_MAX); // typical result // float magnitude range 1.175494e-38 to 3.402823e+38
How is that possible since we have only 32-bits?
Typical
float
does indeed only store about 232 different values. Yet they are not distributed linearly but logarithmically in linear groups.223 different values in the range [2-126 to 2-127)
...
223 different values in the range [0.5 to 1.0)
223 different values in the range [1.0 to 2.0)
223 different values in the range [2.0 to 4.0)
...
223 different values in the range [2127 to 2128)And their negative counter parts.
Also +/- zeros, small sub-normal numbers, +/- infinity and Not-a-NumberIt also has to keep track of the sign , so how is it storing all this in just 32-bits?
1 bit for sign 8 bits for the binary exponent 23 bits for the significant (with a MSBit usually implied as 1) -- 32 bits
When printing a
float
, only about 6 or so (FLT_DIG, FLT_DECIMAL_DIG
) significant digits usually are important.printf("%.*e\n", FLT_DIG-1, FLT_TRUE_MIN); printf("%.*e\n", FLT_DIG-1, FLT_MIN); printf("%.*e\n", FLT_DIG-1, acos(-1)); printf("%.*e\n", FLT_DECIMAL_DIG - 1, nextafterf(FLT_MAX, 0.0)); printf("%.*e\n", FLT_DECIMAL_DIG - 1, FLT_MAX);
Output
1.40130e-45 // min sub-normal 1.17549e-38 // min normal 3.14159e+00 // pi 3.40282326e+38 // number before max 3.40282347e+38 // max
这篇关于用c语言编写的浮点数的范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!