当前位置:网站首页>[advanced C language] - analyze the storage of micro data in memory [2] (floating point storage)
[advanced C language] - analyze the storage of micro data in memory [2] (floating point storage)
2022-07-28 17:45:00 【Crazy orange】
Floating point storage in memory
3.1 A chestnut
How are floating-point numbers stored in memory ? We can do it in float.h View the data range under the header file .
Examples of floating point storage
#include<stdio.h>
int main()
{
int n = 9;
float* pFloat = (float*)&n;
printf("n The value of is :%d\n", n);//9
printf("*pFloat The value of is :%f\n", *pFloat);//0.000000
*pFloat = 9.0;
printf("num The value of is :%d\n", n);//1091567616
printf("*pFloat The value of is :%f\n", *pFloat);//9.000000
return 0;
}
The output is quite different from what we thought , Why is that ? Let's explore how
3.2 Floating point storage rules
num and *pFloat In memory, a number is clearly used , Why is there such a big difference between the interpretation results of floating-point numbers and integers ?
To understand this result , Be sure to understand the representation of floating-point numbers in the computer .
Detailed interpretation :
According to international standards IEEE( Institute of electrical and Electronic Engineering )754, Any binary floating point number V It can be expressed in the following form
- (-1)S * M * 2E
- (-1)S The sign bit , When S=0,V Is a positive number ; When S=1,V It's a negative number .
-M Represents a significant number , Greater than or equal to 1, Less than 2.
-2E The exponent of .
Take another chestnut
Decimal 5.5, Written as binary is 101.1, amount to 1.01 * 22.(101.1, After the decimal point 1, It's actually 2-1 be equal to 0.5)
that , According to the above V The equation of , We can draw S=0,M=1.01,E=2.
IEEE 754 Regulations : about 32 Floating point number of bits , The highest 1 Bits are sign bits S, The next 8 Bits are exponents E, The rest 23 Bits are significant numbers M.

For floating-point numbers of bits , The highest 1 Bits are sign bits S, The next 11 Bits are exponents E, The rest 52 Bits are significant numbers M.

But , For significant numbers M And the index E, How is it stored ?IEEE 754 There are some special provisions :
As I said before ,1<=M<2, in other words ,M Must be 1.xxx In the form of , among xxx Represents the fractional part . Keep it in the computer M when , since M Must be 1.xxx In the form of , So by default, the first digit of this number is always 1, So it can be discarded , Save only the back xxx The fractional part . For example preservation 1.01 when , Save only 01, Wait until you read , Put the first 1 Add . The purpose of this , It's saving 1 Significant digits , With 32 For example, a floating-point number , Leave to M Only 23 position , Will come first 1 After giving up , It's equivalent to being able to save 24 Significant digits , The accuracy is also improved .
As for the index E, The situation is more complicated :
First ,E As an unsigned integer (unsigned int) That means , If E by 8 position , it Of take value Fan Surrounded by 0~ 255; If E by 11 position , The value range is 0~ 2047. however , We know , In scientific counting E There may be negative numbers .
According to scientific counting E There may be negative numbers , therefore IEEE 754 Regulations , In memory E The real value of must be added with a real number , about 8 Bit E, The middle number is 127; about 11 Bit E, The middle number is 1023. such as :2^10 Of E yes 10, So save it as 32 When floating-point numbers are in place , Must be saved as 10+127=137, namely 10001001.
Another chestnut (E Negative case )
Example :5.5( Positive numbers )
Example :5.5
int main()
{
float a = 5.5f;
//101.1——>1.011*2^2
//S=0,M=1.011,E=2
// What is really stored is S=0,M=011,E=2+127
// Suppose it is now a memory area :
//0 10000001 01100000000000000000000
// Convert hexadecimal :4 Binary is one 16 Base number
//40 b0 00 00
return 0;
}

explain : Floating point numbers are also stored in memory at the size end ! Now it is clear how floating-point numbers are stored in memory . Of course, if you are interested , You can test it privately double~
What I mentioned earlier is how to save it , Next is , Index E Taking it out of memory can also be divided into 3 In this case :
E Not all for 0 Or not all 1

E For all 1
At this time , The exponent of a floating point number E be equal to 1 subtract 127 perhaps (1023) That's the true value , Significant figures M No more first 1, It's reduced to 0.xxx Decimals of . This is to show that ±0, And close to 0 A very small number of .
// When E added 127 perhaps 1023 Then put it into memory ,
// The result is all 0, Explain the real E A very small
//
// such as :
E= -127
-127+127=0// Value stored in memory
** E For all 1**
At this time , If the significant number M All for 0, Indication ± infinity ( It depends on the sign bit s);
All right. , That's all for the floating-point rule ~
Explain the previous topic :
int main()
{
int n = 9;
float* pFloat = (float*)&n;
printf("n The value of is :%d\n", n);//9
printf("*pFloat The value of is :%f\n", *pFloat);//0.000000
*pFloat = 9.0;
printf("num The value of is :%d\n", n);//1091567616
printf("*pFloat The value of is :%f\n", *pFloat);//9.000000
return 0;
}
below , Let's go back to the first question : Why? 0x00000009 Restore to floating point number , became 0.000000 ?
All right. , My friend is cute , That's all for storing floating-point numbers ~ If you don't understand how to be fat , It is suggested to watch it several times ~
If you think the article is good , Remember the praise. + Share ~ If something is wrong , Feel free to comment and point out ~
边栏推荐
- 【p5.js实战】我的自画像
- Mmcv installation method
- 【p5.js学习笔记】码绘的基础知识
- On the non recursive and recursive implementation of finding the nth Fibonacci number respectively
- Arya professional web automated test platform
- R语言 sub()用法
- Public medical database
- Three ways to dynamically call WebService.Net
- The difference between using switch in a loop and using break and continue after executing a condition
- R language drawing / drawing / drawing 2
猜你喜欢
随机推荐
100+ collection of medical image data sets
软件测试培训两个月骗局?培训机构到底如何选择?
[C language must see] yo, writing bugs, I'm sure you've stepped on the pit
In depth sharing of Ali (ant financial) technical interview process, with preliminary preparation and learning direction
Mmcv installation method
软件测试行业真的饱和了吗?
【Unity Tilemap】教程 | Basic、Rule Tile、Prefab Brush、Tilemap Collider
Alibaba P8 architect talk: seven knowledge points (including interview questions) that must be learned well to become an architect
ROS system installation
strsplit()函数
小白必看的软件测试发展路线
如何使用IDEA将项目上传到码云
Ant financial mobile testing tool solopi monitoring part of the source code guide.. Continuous update
An article takes you closer to the overview and principle of kubernetes
软件测试前景如何?
【Unity Scriptable Object】教程 | 在Unity中使用 Scriptable Object 存储物体数据信息
100+医学影像数据集集锦
USB virtual serial port (CDC) limit speed test
mmdetection3D---(1)
软件测试前景不行了吗?进入寒冬期了吗?









![【C语言进阶】——剖析入微数据在内存中的存储[上]](/img/6a/ac723cee2543cd2403a7e58d556c8e.png)