当前位置:网站首页>Storage of floating point in memory
Storage of floating point in memory
2022-06-11 07:44:00 【Fried tomatoes 110】
I wrote the storage of integer in memory , Today, let's talk about the storage of floating-point numbers in memory , See if it is the same as integer ~
Let's start with a piece of code :
#include<stdio.h>
int main()
{
int n = 9;
float* pFloat = (float*)&n;
printf("n The value of is :%d\n", n);
printf("*pFloat The value of is :%f\n", *pFloat);
*pFloat = 9.0;
printf("num The value of is :%d\n", n);
printf("*pFloat The value of is :%f\n", *pFloat);
return 0;
}Output results :

From this result, we can see that , When an integer is read as a floating-point type , You will find that the final result is very different from the integer , When a floating-point number is read as an integer , The result is also different from the original floating-point type . From this, we can roughly guess that the memory storage of floating-point type should be different from that of integer type . Is this the case , Please see below ![]()
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 :
V=(-1)^S*M*2^E
(-1)^S The sign bit , When S=0 when ,V Is a positive number ,S=1 when ,V It's a negative number ;
M It's a significant number ,0<=M<=1;
2^E Is the number of digits .
for instance :
Decimal 5.5, Written as binary is 101.1 , amount to 1.01×2^2 .
Because about binary “ Scientific enumeration ” Your meaning is as follows ( Analogous to the decimal system ):

Well, according to the above format ,S=0,M=101,E=2. Take this as an example , All binaries can be written in this form . So floating point numbers are stored in memory only S,M and E The value of the ~
IEEE 754 Regulations :
about 32 Floating point number of bits , The highest bit is the sign bit S, Next 8 Bits are exponents E, The rest 23 Bits are significant numbers M.

about 64 Floating point number of bits , The highest bit is the sign bit S, Next 11 Bits are exponents E, The rest 52 Bits are significant numbers M.
IEEE 754 For significant figures M and E, There are some special rules .
As I said before ,1<=M<2, in other words ,M It can be written. 1.###### In the form of , among ###### Represents the fractional part .
IEEE 754 Regulations , Save on the computer M when , By default, the first digit of this number is always 1, Therefore, it can be omitted , Save only the back #####( decimal ) part . For example, the significant number is 1.001 when , Just save the decimal part 001, Wait until you read , And then 1 Read it . In this way, the storage is saved as significant numbers . With 32 For example, the number of significant digits , Leave to M Only 23 position , But the first 1 Leave it out , I can save it 24 Significant digits .
And for the index E, The situation is more complicated .
First E It's an unsigned integer .
That means , If E by 8 position , that E The value range of is 0~255. If E Position as 11 position , be E The value range of is 0~2047. But for scientific counting ,E Can be negative . So in Deposit in E When it is really worth it, you should first add a middle number in the deposit . about 8 Bit E, To add 127. about 11 Bit E To add 2047.
Last , Index E Fetching from memory can be further divided into three cases :
(1) Stored E Not all for 0 Or not all of them 1 when , Floating point numbers are represented by the following rules , The index E Calculated value subtract 127( or 1023), Get the real value , then Significant figures M Add the first 1.
such as :
0.5( Decimal system ) Converting to binary is 0.1, Because the integer part must be 1, That is, to move the decimal part back by one digit , That is to say 1.0*2^-1(-1^0*1.0*2^-1). among E=-1. When storing, add... To its real value 127, Then the stored value is 126, Binary is 01111110; Remove the integer part 1, The decimal part is 0, Then make up 0 To 23 position 00000000000000000000000, The stored binary representation is :
0 01111110 00000000000000000000000
(2) Stored E All for 0 when , The exponent of a floating point number E( True value ) be equal to 1-127( perhaps 1-1023) That's the true value , Significant figures M No more first 1, It's reduced to 0.xxxxxx Decimals of . This is to show that ±0, And close to 0 A very small number of .
(3) Stored E All for 1 when , If the significant number M All for 0, Express ± infinity ( It depends on the sign bit s)
That's all for the storage rules of floating-point numbers . Now let's analyze the original code .
When the whole number 9 When stored as a floating-point number , First , To put 0x00000009 Split into
0000 0000 0000 0000 0000 0000 0000 1001
Get the first sign bit s=0, Back 8 Bit index E=00000000 , Last 23 The significant number of bits is 000000000000000 00001001,E The real value of is 1-127=-126.
Because the index E All for 0, So the second case in the previous section . therefore , Floating point numbers V The just :
V=(-1)^0 × 0.00000000000000000001001×2^(-126)=1.001×2^(-146 )
obviously ,V It's a very small one, close to 0 Positive number of , So the decimal number is 0.000000.
Let's look at the second part of the example , Floating point numbers 9.0, What happens when it is read out as an integer ?
9.0 It can be written. -1^0*1.001*2^3, Because it is a positive number , therefore S=0,E The real value of is 3, Significant figures M=001. I.e. storage time index E be equal to 3+127=130, namely 10000010; Significant figures M001 Add... To the back 20 individual 0, Cramming 23 position . The storage form is as follows :
01000001000100000000000000000000
This 32 The binary number of bits , Restore to decimal , Read as an integer , Namely 1091567616. That is the final result .
边栏推荐
- C language inherits memory management mechanism (unfinished)
- Import on CSDN MD file
- C wechat upload form data
- Configuration software -- control drag and drop
- 2021-10-24
- 【IoT】项目管理:如何打造更好的跨职能团队?
- Qunhui ds918 creates m.2 SSD read / write cache
- Modular linear equations (Chinese remainder theorem + general solution)
- 【AtCoder2387】+/- Rectangle
- 【HDU6357】Hills And Valleys(DP)
猜你喜欢
![[IOT] project management: how to build a better cross functional team?](/img/df/28dbf0f7ba75d1bb3469cc15e70538.png)
[IOT] project management: how to build a better cross functional team?

C language function stack frame

【AtCoder1980】Mysterious Light(数学模拟)

After 4 years of naked resignation from the test, the test post of 15K interview was rubbed on the ground, and the result made me collapse and cry

Nim product

C language - Growth Diary -03- function definition and function prototype declaration

Sdl-2 thread logic

Use of wordcloud

C language - Growth Diary -02- function

C wechat upload form data
随机推荐
Arduino_ Esp32 development record
2020080 simulation competition [horizontal and vertical coordinates do not affect each other, cactus minimum cut, combined meaning translation formula]
Introduction to operations research
【Oracle 数据库】奶妈式教程day03 排序查询
Xshell7 和 Xftp7要继续使用此程序,您必须应用最新的更新或者使用新版本
MFC custom string linked list
C language - Growth Diary -01- count primes and sum
What is the difference between gaussdb for redis and redis?
Compound ratemodel contract analysis
Wc2020 guessing game
【POJ3691】DNA repair (AC自动机+DP)
Remote office experience | community essay solicitation
String Simulation Implementation
Rabin Miller prime test
[poj3691] DNA repair (AC automata +dp)
[cluster] lvs+keepalived high availability cluster
测试4年裸辞失业,面试15k的测试岗被按在地上摩擦,结局让我崩溃大哭...
Wc2020 course selection
Arduino_ STM development record
2021-10-24