当前位置:网站首页>[C language series] - storage of deep anatomical data in memory (II) - floating point type
[C language series] - storage of deep anatomical data in memory (II) - floating point type
2022-07-29 05:35:00 【Gancheng なつき】
꧁ Hello, everybody ! It is a great honor to have your visit , Let's have a long way to go in programming !꧂
* Blog column :【C All living things 】*
Introduction to this article : Deep anatomy of how floating-point data is stored in memory !
Get to know the author : Aspire to be a great programmer , Xiaobai, who is currently a sophomore in College .
Inspirational terms : The tediousness of programming , Let's study together and become interesting !
Text begins
List of articles
Examples of floating point storage
IEEE 754 For significant figures M And the index E, There are also some special provisions
For significant figures M The provisions of the
To index E The provisions of the
Explain why the above operation results are like that !
Preface
In front of the depth of anatomical data stored in memory ( One ) in , We introduced in detail the storage of shaping in memory and the data is based on 16 How to store hexadecimal in memory involves the problem of size end , Now let's discuss in detail how floating-point data is stored in memory .
Common floating point numbers
such as :Π==3.1415926....
1E10==1.0*10^10
I mentioned the integer family before , Here is the family of floating point numbers , Include :float 、double、 long double type
The range represented by floating-point numbers is float.h The document defines .
Examples of floating point storage
#include<stdio.h>
int main()
{
int a = 9;
float* p = (float*)&a;
printf("a The value of is :%d\n", a);// Print as an integer a
printf("*p The value of is :%f\n", *p);//* Dereference found a, Print out in floating point a
*p = 9.0;//a Assigned 9.0
printf("num The value of is :%d\n", a);//a Print as an integer
printf("*p The value of is :%f\n", *p);// Print in floating point a Value
return 0;
}What is the result of this print ? Why? ? Let's see the result first , I will tell you the reason later !

Is it not quite the same as expected , good ! To understand why such a result occurs , We first need to understand the storage rules of floating-point numbers .
Floating point storage rules
According to international standards IEEE( Electrical and Electronic Engineering Association )754, Any binary floating point number V It can be expressed in the following form :
· (-1)^S * M * 2^E
· (-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
· 2^E Indicates the index bit
What does that mean , Let's give a few examples :
such as :V=5.0f
Written in binary form is 101.0, amount to 1.01*2^2.
At this time S=0,M=1.01 ,E=2.
When V=9.5f when ,
Written in binary form is 1001.1, amount to 1.0011*2^3.
Why is the decimal place 1 Well , Just look at a small picture ------->

Of course, there are also those written as binary numbers that cannot be accurate after the decimal point , such as V=9.6f, It will never be accurate , Always a little less than the original number .
IEEE 754 Regulations
about 32 Floating point number of bits , namely (float type ), The highest 1 Bits are sign bits S, And then 8 Bits are exponents E, The rest 23 Bits are significant numbers M.

about 64 Floating point number of bits , namely (double type ), The highest 1 Bits are sign bits S, And then 11 Bits are exponents E, The rest 52 Bits are significant numbers M.

IEEE 754 For significant figures M And the index E, There are also some special provisions
For significant figures M The provisions of the
As mentioned earlier ,M [1,2] , in other words M It can be written. 1.xxxxxxx In which xxxxxxx Represents the fractional part .
Store in the computer M when , By default, the first digit of this number is always 1, Therefore, it can be discarded in storage , Keep only the following fraction .
for example : Storage 1.01 when ,M Only keep 01, The back is not enough 0 A filling , It will not change when it is taken out , Wait until you read , Put the decimal point before 1 add , The purpose of this , Just to save 1 Significant digits , Make it more accurate , be equal to M It's preserved in 24 Significant digits .
To index E The provisions of the
Storage index E when
First ,E Is an unsigned positive number (unsigned int)
On behalf of , If E by 8 digit , The value range is 0~255, If 11 The value range is 0~2047, We know that in scientific counting ,E It can be the existence of negative numbers , Therefore, it is stipulated that E You must add a middle number ,
· 8 Bit E: add 127
· 11 Bit E: add 1023
for example : In the storage 1.01*2^2 when , At this time E by 2, Add 127, namely 2+127=129
So it's stored in E Upper 8 individual bit Position as :10000001
Read from memory E when
It can be divided into three situations :
① E Not all for 0 Or not all 1 when
Regulations : Index E The calculated value of minus 127( Or subtract 1023), Get the real value , Then put the significant bit M Add the first 1.
for instance :

② E All for 0 when
At this time , The exponent of a floating point number E=1-127( perhaps 1-1023), That's the true value , Why? ?
Think about it , When E All for 0 when , It must be the original E by -127+127=0, example :1.01*2^(-127) Such a number , Think about how small it is , Significant figures M Don't add the first one 1, It's reduced to 0.xxxxxxx Decimals of , This is to show that ±0, And close to 0 A very small number of .
③E All for 1 when
At this time , If the significant number M All for 0, Express ± infinity ( The sign depends on the sign bit S).
Explain why the above operation results are like that !
The detailed explanation I will put in the comments in the code will be more intuitive , And understanding :
#include<stdio.h>
int main()
{
int a = 9;
// 00000000 00000000 00000000 00001001 9 Complement
float* p = (float*)&a;
// 0 00000000 00000000000000000001001
// E All for 0, Is an infinitely close 0 Number of numbers , After printing the decimal point 6 When a , Ignore the following bits
printf("a The value of is :%d\n", a);// 9 Print as an integer
printf("*p The value of is :%f\n", *p);// 9 adopt %f Print it out in the form of 0.000000
// 0 00000000 00000000000000000001001
// S E M
// E All for 0,1-127=-126
// M=0.000000000000000000000001001
// The value is (-1)^0 *0.0000000000000000000000001001*2^-126 Close to infinity 0 Number of numbers
*p = 9.0;
// 1001.0
// 1.0010 * 2^3
// S=0,E=3,M=1.0010 3+127=130
// Floating point type is saved as integer 0 10000010 00100000000000000000000
// The highest position is 0 Positive numbers , Take out and print directly
printf("num The value of is :%d\n", a);// floating-point 9.0 With %d Print it out in the form of
printf("*p The value of is :%f\n", *p); // 9.0 (6 Decimal place )
}Okay , That's all for how floating-point types are stored in memory , We don't explore deeply , Just understand the rules of storage , Here it is , Pay attention to bloggers !
边栏推荐
猜你喜欢
![[C language series] - detailed explanation of file operation (Part 1)](/img/12/2d47fde0385d3f1dcb31f5efa82f7b.png)
[C language series] - detailed explanation of file operation (Part 1)

MySQL解压版windows安装
![Niuke network programming problem - [wy22 Fibonacci series] and [replace spaces] detailed explanation](/img/39/1d4fb1774b0f9f7c9bb13221f0d6c2.png)
Niuke network programming problem - [wy22 Fibonacci series] and [replace spaces] detailed explanation

【C语言系列】— 把同学弄糊涂的 “常量” 与 “变量”

无重复字符的最长字串

Clickhouse learning (V) cluster operation

公众号不支持markdown格式文件编写怎么办?

Playwright实战案例之爬取js加密数据

Preemptive appointment | Alibaba cloud shadowless cloud application online conference appointment opens

uniapp组件之tab选项卡滑动切换
随机推荐
三次握手四次挥手针对面试总结
ClickHouse学习(三)表引擎
uniapp页面标题显示效果
移动端-flex项目属性
Alibaba cloud and Dingjie software released the cloud digital factory solution to realize the localized deployment of cloud MES system
Niuke network programming problem - [wy22 Fibonacci series] and [replace spaces] detailed explanation
Terminal shell common commands
Playwright实战案例之爬取js加密数据
Day 2
Cryengine5 shader debugging
Differences between texture2d and texture2dproj under webgl1.0
Clickhouse learning (IV) SQL operation
Side effects and sequence points
Basic use of redis
paddle. Fluid constant calculation error 'nonetype' object has no attribute 'get_ fetch_ list‘
Day 2
微信小程序-组件传参,状态管理
全局components组件注册
Do students in the science class really understand the future career planning?
Best practices for elastic computing in the game industry