C語言float內(nèi)存布局示例詳解
前言
C語言中的float并不像大多數(shù)人想象的那樣, 由于計算機模擬的原因, 其本質(zhì)是離散的而非連續(xù)的, 所以精度和范圍是一定的, 這些都寫在float.h頭文件的宏中.
但通常, 我們對教材的每一個字都認識, 連起來就讀不懂了, 所以, 寫下此博文, 詳解之.
學過深入理解計算機系統(tǒng)的同學, 都知道float的實現(xiàn)方式, 按照IEEE標準, 由符號位, 階碼位, 尾數(shù)位組成, 本文給出一個代碼, 打印float的符號位, 階碼位, 尾數(shù)位.
一、float中的宏定義
這是float.h中有關float具體實現(xiàn), 范圍, 精度等的宏常量, 依據(jù)64位系統(tǒng)環(huán)境, 我們逐個解讀.
#include <float.h> FLT_RADIX; // 2; FLT_MANT_DIG; // 24; FLT_DIG; // 6; FLT_MIN_10_EXP; // (-37); FLT_MAX_10_EXP; // 38; FLT_EPSILON; // 1.19209290e-7F; FLT_MAX; // 3.40282347e+38F; FLT_MIN; // 1.17549435e-38F;
FLT_RADIX 2
This is the value of the base, or radix, of the exponent representation. This is guaranteed to be a constant expression, unlike the other macros described in this section. The value is 2 on all machines we know of except the IBM 360 and derivatives.
這是指數(shù)表示的基數(shù)或基數(shù)的值。這保證是一個常量表達式,與本節(jié)中描述的其他宏不同。在我們所知的所有機器上,該值均為 2,除了 IBM 360 及其衍生產(chǎn)品。
float基于二進制實現(xiàn), 其基數(shù)是2.
換句人話說, 所有float浮點數(shù), 都是由一系列2的n次方相加組成.
比如
0.5 就是
2^-1 次方,0.25就是
2^-2 次方,0.75就是
2^-1 + 2^-2
FLT_MANT_DIG 24
This is the number of base-FLT_RADIX digits in the floating point mantissa for the float data type. The following expression yields 1.0 (even though mathematically it should not) due to the limited number of mantissa digits:
這是浮點數(shù)據(jù)類型的浮點尾數(shù)中的基數(shù)FLT_RADIX位數(shù)。由于尾數(shù)位數(shù)有限,以下表達式產(chǎn)生 1.0(盡管在數(shù)學上不應該產(chǎn)生):
float radix = FLT_RADIX; // 2.0 1.0f + 1.0f / radix / radix / … / radix; //在float中, 1.0 其實是 1.0 + 1.0 連續(xù)除以 24 個 2.0
where radix appears FLT_MANT_DIG times.
radix 出現(xiàn)了FLT_MANT_DIG 次, 也就是 24 次.
這個細節(jié)如果有人看過深入理解操作系統(tǒng), 那么就應該明白了, 在IEEE標準中, float的尾數(shù)是23位, 但由于通常都是1.0+0.N的形式, 所以相當于第一位是隱式的1, 加23位就是24位.
這24位就是float的二進制精度.
FLT_DIG 6
float的十進制精度, 包括整數(shù)部分及小數(shù)部分.
比如 123.456 這就是6位精度, 再多就不保證精度可用了, 比如123.4567, 其中7就不保證是正確的.
FLT_MIN_10_EXP (-37)
這代表float能保證的最小的10進制指數(shù), 我這里是 10^-37 次方, 再小就不保證正確了.
FLT_MAX_10_EXP 38
這是float能保證的最大的10進制指數(shù), 再大就不保證正確了
FLT_EPSILON 1.19209290e-7F
這個不好理解, 它的意思是 1.0 和比 1.0 大的最小的float值的差.
FLT_MAX 3.40282347e+38F
float能表示的最大數(shù).
FLT_MIN 1.17549435e-38F
float能表示的最小數(shù).
二、float 內(nèi)存布局打印實現(xiàn)算法
基本思想是實現(xiàn)一個位域結構, 將一個32位的整數(shù)分成三份, 一份占1位, 指示符號, 一份占8位, 指示階碼, 一份占23位, 指示尾數(shù).
typedef struct { uint32_t Mantissa : 23; uint32_t Exponent : 8; uint32_t Sign : 1; } fltToBit;
由于是小端序, 逆著排, 也就是尾數(shù), 階碼, 符號.
通過itoa()函數(shù), 將整數(shù)轉(zhuǎn)為二進制字符串, 并進行打印.
float 內(nèi)存布局打印實現(xiàn)代碼
代碼比較容易, 唯一不好理解的是:
fltToBit test = *(fltToBit *)&a;
float 不能直接強制轉(zhuǎn)為 fltToBit, 需要先取地址, 強轉(zhuǎn)為 fltToBit 指針, 再解引用.
其他的代碼都應該能懂.
#include <stdint.h> #include <stdio.h> #include <stdlib.h> typedef struct { uint32_t Mantissa : 23; uint32_t Exponent : 8; uint32_t Sign : 1; } fltToBit; void print_float_as_binary(float a) { char BufExponet[32] = ""; char BufMantissa[32] = ""; fltToBit test = *(fltToBit *)&a; printf("Sign: %u\nExponent: %08s\nMantissa: %023s\n", test.Sign, itoa(test.Exponent, BufExponet, 2), itoa(test.Mantissa, BufMantissa, 2)); } int main() { const float a = 1.4142136F; print_float_as_binary(a); return 0; }
總結
float 是用離散方法實現(xiàn)有限精度的浮點數(shù), 有時間應仔細查閱文檔進行推敲, 很有意思.
本文介紹了IEEE對float的實現(xiàn), 利用數(shù)據(jù)結構, 將其內(nèi)部真實二進制值打印出來, 方便讀者研究。
更多關于C語言float內(nèi)存布局的資料請關注腳本之家其它相關文章!
相關文章
C語言中pthread_exit和pehread_join的使用
pthread_exit用于強制退出一個線程,pthread_join用于阻塞等待線程退出,獲取線程退出狀態(tài),本文主要介紹了C語言中pthread_exit和pehread_join函數(shù)的使用,具有一定的參考價值,感興趣的可以了解一下2024-02-02