当前位置:网站首页>[data storage] storage of floating point data in memory
[data storage] storage of floating point data in memory
2022-06-12 08:37:00 【Always brush questions every day】
Catalog
1-0 Common floating point numbers
1-1 Floating point numbers are stored in memory
1-2 Rules for storing floating point numbers
1-3 Rules for fetching floating point numbers
1-4 Revisit the question introduced :( Combined storage and retrieval )
1-6 About this floating-point and integer output conversion :
1-7 The end , And the flower , wait , Let's have a little test :
1-0 Common floating point numbers
Two representations :
To express directly :3.14
It's like scientific counting :1E10
Common types of floating point numbers :float ,double( More details in float.h In the definition of , Available software everything Search inside )

1-1 Floating point numbers are stored in memory
Let's first look at the introduction of a problem
#include<stdio.h>
// The storage of floating-point data in memory
int main()
{
int a = 9;
printf("%d\n", a);
float* pa = (float*)&a;
printf("%f\n", *pa);
*pa = 9.0;
printf("%d\n", a);
printf("%f\n", *pa);
return 0;
}


Come to the conclusion that :
1. By integer ( Floating point numbers ) The way to save , At the same time, according to the integer ( Floating point numbers ) It's normal to take out the angle of view
2. But in whole numbers ( Floating point numbers ) The way to save , At the same time, according to the floating point number ( Integers ) It's not normal to take out the angle of view ( It's different from what we started to think ) Of
summary :
From here, we can see that there are differences between integer and floating-point numbers in memory
1-2 Rules for storing floating point numbers
Any binary floating point number V It can be expressed in the following form :( Can be associated with the decimal system of scientific counting )
- (-1)^S*M*2^E
- (-1)^S The sign bit , When S=0,V Is a positive number , When S by 1,V It's a negative number
- M Represents a significant number ,1<=M<2
- 2^E Indicates the index bit :
Bit weight of each bit when floating-point data is written in binary :

For example :


about float:
S The data represented accounts for 1bite
E The data represented accounts for 8bite
M The data represented accounts for 23bite

about double:
S The data represented accounts for 1bite
E The data represented accounts for 11 individual bite
M The data represented accounts for 52 individual bite

about M and E There are special rules :
M( Significant figures ):( Omit the value )1<=E<2, Due to the particularity of the scope , This E The number represented must be 1.XXX, therefore It is stipulated that there is no such thing 1, Only exist XXX, When you take it out, just add 1. That's it
E( Index ):( Correction value ) As for the index E,E For an unsigned integer (unsigned int)--- The specified calculation value can only be a positive integer .
It means , If E by 8 position , Its value range is 0-255: If E by 11 position , Its value range is 0-2047, however , We know , In scientific counting E Yes, there can be negative Of , So the rules are , In memory E The true value of must be added with an intermediate number , about 8 Bit E, The middle number is 127: about 11 Bit E, The middle number is 1023. such as , Example index E yes 10 When , Save as 32 When floating-point numbers are in place , Must be saved as 10+127=137, namely 10001001.
Let's look at two examples of decimal conversion to binary storage format in memory :
Example 1:float a=5.5

Example 2:float b=0.5;

1-3 Floating point number takes The rules of
situation 1. When E Incomplete 0 Or all 1 when
In this case, the floating-point number is : Index E The calculated value of minus 127( or 1023), Get the real value , Put significant numbers in M Add the first 1

situation 2. When E For all 0 when
At this time , The exponent of a floating point number E be equal to 1-127( perhaps 1-1023) That's the true value ( Regulations )
Significant figures M No more first 1, It's reduced to 0.XXX Decimals of , This is to show that +0 and -0, And close to 0 A very small number .

situation 3. When E For all 1 When
At this time , Floating point exponents E be equal to 255-127=128, That is, the index part is 2^128, This is a very large number , It means positive infinity or negative infinity .( Here we leave the reader with an example )
1-4 Revisit the question introduced :( Combined storage and retrieval )

1. Save as an integer , At the same time, from the perspective of floating point numbers

1-5. Save it as a floating-point number , At the same time, from the perspective of integer numbers


1-6 About this floating-point and integer output conversion :
1. When converting long data to short data output , Its value cannot exceed the allowable value range of short data , otherwise An error will occur during conversion .

2. Data type of output f(d) And output format %d(%f) Errors often occur when there is a discrepancy

1-7 The end , And the flower , wait , Let's have a little test :
If you define :float a=1.0f;
problem : Through the debug memory window, you can see &a What is it ?
answer : 0000803f

边栏推荐
- JVM learning notes: garbage collection mechanism
- [essence] explain in detail the memory management mechanism in QT
- A brief summary of C language printf output integer formatter
- 判断对象是否为空
- What kind of sparks will be generated when the remote sensing satellite meets the Beidou navigation satellite?
- Regular expressions in JS
- Hands on learning and deep learning -- simple implementation of linear regression
- MYSQL中的锁的机制
- Webrtc series - mobile terminal hardware coding supports simulcast
- Jump to an interface at a specified time. (Web Development)
猜你喜欢
![[essence] explain in detail the memory management mechanism in QT](/img/7d/0d83158c6b0574dd3b3547b47af67e.jpg)
[essence] explain in detail the memory management mechanism in QT

Website colab and kaggle
![Easyexcel exports excel tables to the browser, and exports excel through postman test [introductory case]](/img/ca/0e2bd54a842a393231ec6db5ab02c2.png)
Easyexcel exports excel tables to the browser, and exports excel through postman test [introductory case]

Hands on deep learning -- Introduction to linear regression model

Hands on learning and deep learning -- simple implementation of linear regression

【 pointeur avancé Ⅲ】 mise en œuvre de la fonction de tri rapide qsort& fonction de rappel en langage C

Regular expressions in JS

MES helps enterprises to transform intelligently and improve the transparency of enterprise production

企业为什么要实施MES?具体操作流程有哪些?

安科瑞电动机保护器具有过载反时限、过载定时限、接地、起动超时、漏电、欠载、断相、堵转等功能
随机推荐
Jump to an interface at a specified time. (Web Development)
Dynamically create and submit forms
vscode 下载慢解决办法
处理异常数据
余压监控系统保证火灾发生时消防疏散通道的通畅,为大型高层建筑的安全运行和人民生命财产安全保驾护航
Hands on learning and deep learning -- Realization of linear regression from scratch
R loop assignment variable name
The residual pressure monitoring system ensures the smoothness of the fire evacuation passage in case of fire, and protects the safe operation of large high-rise buildings and the safety of people's l
(p15-p16) optimization of the right angle bracket of the template and the default template parameters of the function template
[open source project] easycmd command graphical software
超全MES系统知识普及,必读此文
【动态内存管理】malloc&calloc和realloc和笔试题和柔性数组
What exactly is APS? You will know after reading the article
svg中viewbox图解分析
Production scheduling status of manufacturing enterprises and solutions of APS system
Error: what if the folder cannot be deleted when it is opened in another program
JVM学习笔记:垃圾回收机制
Hands on deep learning -- concise implementation code of weight decay
ctfshow web3
ctfshow web4
