ipk*_*iss 33 c floating-point ieee-754
我正在读一本C书,谈论浮点范围,作者给出了表:
Type Smallest Positive Value Largest value Precision
==== ======================= ============= =========
float 1.17549 x 10^-38 3.40282 x 10^38 6 digits
double 2.22507 x 10^-308 1.79769 x 10^308 15 digits
Run Code Online (Sandbox Code Playgroud)
我不知道最小正值和最大值列中的数字来自哪里.
And*_*nck 28
32位浮点数具有23 + 1位尾数和8位指数(尽管使用-126到127),因此您可以表示的最大数字是:
(1 + 1 / 2 + ... 1 / (2 ^ 23)) * (2 ^ 127) =
(2 ^ 23 + 2 ^ 23 + .... 1) * (2 ^ (127 - 23)) =
(2 ^ 24 - 1) * (2 ^ 104) ~= 3.4e38
Run Code Online (Sandbox Code Playgroud)
float数据类型的值来自总共32位,表示像这样分配的数字:
1位:符号位
8位:指数p
23位:尾数
指数存储p + BIAS在BIAS为127的位置,尾数有23位,假定为第24位隐藏位.该隐藏位是尾数的最高位(MSB),必须选择指数使其为1.
这意味着您可以表示的最小数字01000000000000000000000000000000是1x2^-126 = 1.17549435E-38.
最大值是011111111111111111111111111111111,尾数是2*(1 - 1/65536),指数是127,它给出(1 - 1 / 65536) * 2 ^ 128 = 3.40277175E38.
相同的原则适用于双精度,除了位是:
1位:符号位
11位:指数位
52位:尾数位
偏差:1023
因此从技术上讲,限制来自用于表示浮点数的IEEE-754标准,以上是这些限制的出现方式
无穷大、NaN 和次正规数
这些是迄今为止没有其他答案提到的重要警告。
首先阅读 IEEE 754 和次正规数简介:什么是次正规浮点数?
然后,对于单精度浮点数(32 位):
IEEE 754 规定,如果指数全为 1 ( 0xFF == 255),则它表示 NaN 或 Infinity。
这就是为什么最大的非无穷数有指数0xFE == 254而不是0xFF。
然后加上偏置,就变成了:
254 - 127 == 127
Run Code Online (Sandbox Code Playgroud)FLT_MIN是最小的正规数。但还有更小的非正常的!它们占据了-127指数槽。
以下程序的所有断言都在 Ubuntu 18.04 amd64 上通过:
#include <assert.h>
#include <float.h>
#include <inttypes.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
float float_from_bytes(
uint32_t sign,
uint32_t exponent,
uint32_t fraction
) {
uint32_t bytes;
bytes = 0;
bytes |= sign;
bytes <<= 8;
bytes |= exponent;
bytes <<= 23;
bytes |= fraction;
return *(float*)&bytes;
}
int main(void) {
/* All 1 exponent and non-0 fraction means NaN.
* There are of course many possible representations,
* and some have special semantics such as signalling vs not.
*/
assert(isnan(float_from_bytes(0, 0xFF, 1)));
assert(isnan(NAN));
printf("nan = %e\n", NAN);
/* All 1 exponent and 0 fraction means infinity. */
assert(INFINITY == float_from_bytes(0, 0xFF, 0));
assert(isinf(INFINITY));
printf("infinity = %e\n", INFINITY);
/* ANSI C defines FLT_MAX as the largest non-infinite number. */
assert(FLT_MAX == 0x1.FFFFFEp127f);
/* Not 0xFF because that is infinite. */
assert(FLT_MAX == float_from_bytes(0, 0xFE, 0x7FFFFF));
assert(!isinf(FLT_MAX));
assert(FLT_MAX < INFINITY);
printf("largest non infinite = %e\n", FLT_MAX);
/* ANSI C defines FLT_MIN as the smallest non-subnormal number. */
assert(FLT_MIN == 0x1.0p-126f);
assert(FLT_MIN == float_from_bytes(0, 1, 0));
assert(isnormal(FLT_MIN));
printf("smallest normal = %e\n", FLT_MIN);
/* The smallest non-zero subnormal number. */
float smallest_subnormal = float_from_bytes(0, 0, 1);
assert(smallest_subnormal == 0x0.000002p-126f);
assert(0.0f < smallest_subnormal);
assert(!isnormal(smallest_subnormal));
printf("smallest subnormal = %e\n", smallest_subnormal);
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
编译并运行:
gcc -ggdb3 -O0 -std=c11 -Wall -Wextra -Wpedantic -Werror -o subnormal.out subnormal.c
./subnormal.out
Run Code Online (Sandbox Code Playgroud)
输出:
nan = nan
infinity = inf
largest non infinite = 3.402823e+38
smallest normal = 1.175494e-38
smallest subnormal = 1.401298e-45
Run Code Online (Sandbox Code Playgroud)