当前位置:网站首页>About data storage in memory
About data storage in memory
2022-06-25 13:10:00 【LIn_ jt】
About the storage of data in memory
This article will explain the knowledge of floating-point numbers stored in memory
First , Let's take a look at the following program :>

While studying , The answer I give is very simple 9 9.0 9 9.0, However , When the program runs :>

To analyze the above procedures , First of all, we have to understand the storage of floating-point numbers in memory .
For binary floating point numbers , According to international standards IEEE( Institute of electrical and Electronic Engineering ) 754, Any binary floating point number V Can be expressed in the following form :>
*(-1) ^S * M 2 ^E
among (-1)^S Symbol bit , When s = 0 when ,V Is a positive number , When floating point number s = 1 , V It's a negative number .
M Is the significant bit , M>=1 And M < 2
2 ^E The power represents the exponential bit
for instance :>
For example, decimal floating point numbers 5.5, When we convert it to binary, it is :>
101.1 // Here is .1 The reason is here 1 The weight of is 2 ^-1 Power , That is to say 0.5. And we use it V The format of , Can write like this :>
*(-1)^0 1.011 * 2 ^ 2
namely S = 0; M = 1.011; E = 2;
about -5.5, Use it V The format of , I can express it as
*(-1)^1 (1.011) * 2 ^2;
For the storage of floating-point numbers in memory , There are the following rules
For single precision floating point numbers

For double precision floating point numbers :>

There are also some provisions : because M Always >=1 Less than 2, namely M Always equal to 1 The integer of , therefore , When stored in memory , Only decimal places are saved , example 5.5
It can be expressed as **(-1)^0 1.011 * 2 ^ 2*, M = 1.011. But what we store , It only stores 011, And when we use it , That's what makes 1 Make up for it .
For index E for , The first is a unsigned int Number of types , So its scope is 0 ~ 255(8bit) perhaps 0 ~2047(11bit)
When expressed in scientific counting , It is inevitable that E negative for example Decimal system 0.5 Expressed as a binary number :>
1*10 ^-1 also E The range of is greater than or equal to 0, therefore , Specify storage in memory E The true value of must be added with an intermediate number , The add 127 (8bit) or 1023(11bit) after , Then store it in memory
For example, just now -1., Save as 32 When floating-point numbers are in place , Save as -1 + 127 = 126 namely
01111110
Besides , Index E There are also the following provisions :>
if E The value of is not all 1 or 0, be E The real value of is the value stored in memory minus 127( or 1023), And then the significant number M Add the first one before .
For example, just now 0.5 Binary system V Expressed as :>
1.0*2 ^ -1 here E by -1, M by 1.0, Therefore, the data stored in memory is
0 01111110 00000000000000000000000
if E The data stored in is full 0 when , So let's think about it , What a small number it would be !, namely 2^ -127 Power , therefore , The regulations are as follows :>
When E The data stored in is full 0 when , Its index E The real value of is 1 - 127( or 1023), And the significant number M No more one before , But add 0, To represent a close 0 Number of numbers .
When E The data stored in is all data , that E The real value of is 128(8bit), that 2^E Power is a very large number , therefore ,V Expressed as positive and negative infinity .
Now let's go back to the first example at the beginning :>
#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;
}

Take another look at the second part :>

Convert... As a floating point number n Changed to 9.0, that 9.0 The binary representation of :>
1001.0 namely 1.001*2^3 that S = 0; E = 3 + 127 = 130; M = 0.001. namely
0 1000 0010 00100000000000000000000
So in the form of integers , The data is :>
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-Lw2C6Cbn-1638165164585)(C:\Users\MSI-NB\AppData\Roaming\Typora\typora-user-images\image-20211129090318437.png)]](/img/80/a182c36b6a88835bfbf0b336075649.png)
In the form of floating point numbers , Certainly 9.0 了 .
边栏推荐
- Update PIP & Download jupyter Lab
- Spoken English - weak reading
- 剑指 Offer II 025. 链表中的两数相加
- list.replace, str.append
- Baidu search stability analysis story
- C# 切换中英文输入法
- Geospatial search - > R tree index
- Possible problems when idea encounters errors occurred while compiling module (solved)
- 剑指Offer 第 2 天链表(简单)
- 始终保持疫情防控不放松 营造安全稳定的社会环境
猜你喜欢

剑指 Offer II 025. 链表中的两数相加

Serenvlt first met

Summer Ending

Connect with the flight book and obtain the user information according to the userid

515. Find Largest Value in Each Tree Row

画图常用配色

更新pip&下载jupyter lab

Which Chinese virtual human is better? Sullivan, IDC: Xiaobing Baidu Shangtang ranks in the first echelon

My first experience of go+ language -- a collection of notes on learning go+ design architecture

使用Visio画立方体
随机推荐
[AI helps scientific research] fool drawing of loss curve
原生js---无限滚动
Maui的学习之路(二)--设置
[转]以终为始,详细分析高考志愿该怎么填
[machine learning] what is machine learning?
Another night when visdom crashed
[flask tutorial] flask development foundation and introduction
5 kinds of viewer for browser
二叉树之_哈夫曼树_哈弗曼编码
C # switch between Chinese and English input methods
Analyse de l'optimisation de la réécriture des requêtes lazyagg de l'entrepôt
解析數倉lazyagg查詢重寫優化
Sword finger offer day 2 linked list (simple)
Talk about 11 key techniques of high availability
《MongoDB入门教程》第01篇 MongoDB简介
leetcode - 384. 打乱数组
出手即不凡,这很 Oracle!
CUDA error: unspecified launch failure
Conway's law can not be flexibly applied as an architect?
KDD 2022 | graphmae: self supervised mask map self encoder