当前位置:网站首页>[advanced C language] data storage [Part 2] [ten thousand words summary]
[advanced C language] data storage [Part 2] [ten thousand words summary]
2022-06-10 19:35:00 【You and I are all mortals】
author : You and I are all mortals
Blog home page : You and I are all mortal blogs
Aphorisms : Time will not stay for anyone , And things and people , It's changing all the time . Every one of us , Also keep moving forward !
If you think the blogger's article is good , I hope you'll make it three times in a row ( Focus on , give the thumbs-up , Comment on ), Give me more support !!
series :
List of articles
Catalog
Floating point storage in memory
Examples of floating point storage
A detailed explanation of the example
Preface
This article explains the storage of floating-point types in memory after the previous article , Something you don't know , Why floating point numbers store integer printing , Integer storage floating-point printing is beyond your expectation ? How floating point numbers are stored , How to take it out ? After reading , Your doubts , Will all be resolved
Tips : The following is the main body of this article , The following cases can be used for reference
Floating point storage in memory
Common floating-point numbers are decimals ,π,1E10, This scientific counting method
The family of floating-point numbers includes float double long double type
The range of floating-point numbers is float.h In the definition of
Examples of floating point storage
Think about an example first :
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;
}Why is the output like this ?

Let's look down with this question first
Floating point storage rules
num and *pFloat It's the same number in memory , Why are the interpretation results of floating-point numbers and integers so differentTo 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 * 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 .
for instance :
V=5.0f; How to write it in binary ?
It's written in binary as :101.0
The scientific counting method is :1.01*2^2
So it can be specifically written as :
(-1)^0 *1.01*2^2 Because it's a positive number ,-1 Of 0 The second party is 0, And then we got :
S=0 M =1.01 E=2
V=9.5f, How to write it in binary ?
It's written in binary as :1001.1( After the decimal point is 2 Of -1 Power )
The scientific counting method is :1.0011*2^3
So it can be specifically written as :
(-1)^0*1.0011*2^3
S=0 M =1.0011 E=3
But floating point numbers have some inaccuracies in precision , such as V=9.6f It's written in binary as 1001. The latter value cannot be accurately judged , May be out of range ,float--4byte--32 individual bit position ,double--8byte--64bit position , Then there may be a problem of loss , Can't save it
therefore IEEE 754 Regulations :
about 32 Floating point number of bits , 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 , 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 some special rules .As I said before , 1≤M<2 , in other words ,M It can be written. 1.xxxxxx In the form of , among xxxxxx Represents the fractional part .IEEE 754 Regulations , Keep it in the computer M when , By default, the first digit of this number is always 1, So it can be discarded , Save only the back xxxxxx 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 .As for the index E, The situation is more complicated .First ,E For an unsigned integer (unsigned int)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 You can have negative numbers , therefore IEEE 754 Regulations , 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, In the middle The 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, namely10001001.then , Index E Fetching from memory can be further divided into three cases :E Not all for 0 Or not all of them 1At this time , Floating point numbers are represented by the following rules , The index E The calculated value of minus 127( or 1023), Get the real value , And then the significant number M Add the first 1.such as :0.5(1/2) The binary form of is 0.1, Since it is stipulated that the positive part must be 1, That is to move the decimal point to the right 1 position , Then for1.0*2^(-1), Its order code is -1+127=126, Expressed as 01111110, And the mantissa 1.0 Remove the integer part and make it 0, A filling 0 To 23 position 00000000000000000000000, Then its binary representation is :0 01111110 00000000000000000000000E All for 0At this time , The exponent of a floating point number E 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 .At this time , If the significant number M All for 0, Express ± infinity ( It depends on the sign bit s);for example :Data storagefloat f = 5.5;101.1 Binary system1.011*2^2 Scientific enumerationS=0 M =1.011 E =2 ExpressS Binary for 0 altogether 1 positionE True value 2 Add the median 127 by 129 Binary for 10000001 altogether 8 positionM Round off the number before the decimal , Binary for 011, But not enough , The following is supplemented by 01100000000000000000000, altogether 23 positionConvert to hexadecimal view as 0x40b00000, Pictured :

We know how to put it in , So how to take it out ?
for example :
Data extraction
Under normal conditions :
S=0 M =1.011 E =2 ExpressS Binary for 0 altogether 1 positionE True value 2 Add the median 127 by 129 Binary for 10000001 altogether 8 positionM Round off the number before the decimal , Binary for 011, But not enough , The following is supplemented by 01100000000000000000000, altogether 23 positionThe last stored is :0 10000001 01100000000000000000000middle E Binary for 129, subtract 127 The median value is 2,M Add... Before the decimal , and S be for the purpose of 0, For a positive number(-1)* 0 * 1.01100000000000000000000 * 2^2And the whole 0 Or all 1 When , Is an infinitely large or infinitesimal number , I don't usually meet , So don't do too much research
A detailed explanation of the example
int main()
{
int n = 9;// Integers
//00000000000000000000000000001001
float* pFloat = (float*)&n;
printf("n The value of is :%d\n", n);// Put it in the form of an integer , Take out the integer
printf("*pFloat The value of is :%f\n", *pFloat);// Put it in the form of an integer , Floating point numbers
//00000000000000000000000000001001
//0 00000000 00000000000000000001001
//E =-126
//M =0.00000000000000000001001
//S =0
//+ 0.00000000000000000001001*2^-126
*pFloat = 9.0;// Floating point numbers
//1001.0
// 1.001*2^3
// S=0 E=3 M=1.001
// 0 10000010 00100000000000000000000
printf("num The value of is :%d\n", n);// It is printed as an integer
//0 10000010 00100000000000000000000
printf("*pFloat The value of is :%f\n", *pFloat);
return 0;
}
below , Let's go back to the initial question :Why? 0x00000009 Restore to floating point number , became 0.000000 ?First , take 0x00000009 Split , Get the first sign bit s=0, Back 8 Bit index E=00000000 , Last 23 A significant number of bitsM=000 0000 0000 0000 0000 1001.9 -> 0000 0000 0000 0000 0000 0000 0000 1001Because 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 number 9.0, How to express... In binary ? How much is it to restore to decimal ?First , Floating point numbers 9.0 Equal to binary 1001.0, namely 1.001×2^3.9.0 -> 1001.0 ->(-1)^01.0012^3 -> s=0, M=1.001,E=3+127=130that , The first sign bit s=0, Significant figures M be equal to 001 Add... To the back 20 individual 0, Cramming 23 position , Index E be equal to 3+127=130,namely 10000010.therefore , Written in binary form , Should be s+E+M, namely0 10000010 001 0000 0000 0000 0000 0000This 32 The binary number of bits , Restore to decimal , It is 1091567616 .
Entrance to exercises
After reading these specific operations, it is not allowed , You can click on the top to practice some exercises , You can also have a casual look C Some language exercises , Practice multiple-choice questions and programming questions , Let your knowledge be consolidated , Click directly into the title to go directly to , In addition, if you want to get the qualification of internal promotion of large factories, you can also go and have a look :
summary
Something you don't know , Why floating point numbers store integer printing , Integer storage floating-point printing is beyond your expectation ? How floating point numbers are stored , How to take it out ? After reading , You should know , Because the storage between the two is different , Have the expression of standard method , Yes E S M It is composed of , After reading it, I believe that everyone's internal skills have risen to a higher level , If you think the blogger's writing is good , I hope you can support me more
边栏推荐
- 【C语言进阶】指针的进阶【上篇】
- [database language SPL] a simple and fast database language SPL
- 《Single Image Haze Removal Using Dark Channel Prior》去雾代码实现分析
- Domain Driven Design (VI) - Architecture Design
- 第6章 关系数据理论练习
- Computer:成功教你如何使用一招—就能找回以前的密码(曾经保存的密码但当前显示为******号的密码)
- MySQL数据库设计概念(多表查询&事务操作)
- Beijing Metro ticketing system
- 掌握高性能计算前,我们先了解一下它的历史
- 【 Web 】 page d'accueil personnelle 】 Programme d'études 】 albums de photos 】 babillard d'information 】
猜你喜欢

Ruixin micro rk1126 platform platform porting libevent cross compiling libevent

Openssl1.1.1 compilation error can't locate win32/console pm in @INC

Libcurl 7.61.0 vs2013 compilation tutorial

Implementation analysis of single image haze removal using dark channel prior

Code solution of simplex method (including super detailed code notes and the whole flow chart)

改变世界的开发者丨玩转“俄罗斯方块”的瑶光少年

调试的技巧

基于SSM流量计量云系统的设计与实现.rar(论文+项目源码)

2022.05.27(LC_647_回文子串)

专项测试之「 性能测试」总结
随机推荐
数据库防火墙闪亮登场(好文共赏)
调试的技巧
掌握高性能计算前,我们先了解一下它的历史
Chapter IV data type (III)
Domain Driven Design (VI) - Architecture Design
Vs solution to garbled Chinese characters read from txt files (super simple)
【C语言】还搞不明白结构体吗?不妨来看看这篇文章,带你初步了解结构体
2022.05.25(LC_718_最长重复子数组)
改变世界的开发者丨玩转“俄罗斯方块”的瑶光少年
面试中经常问到的几个问题,快来看看能答对几道吧
Openssl1.1.1 VS2013-编译教程
LeetCode_ Concurrent search set_ Medium_ 399. division evaluation
Sliding window maximum value problem
北京地铁票务系统
Prospect of database firewall technology [final chapter]
云图说|每个成功的业务系统都离不开APIG的保驾护航
nodejs-判断系统类型-获取主机名称-执行控制台命令-中文乱码
2022.05.29(LC_6078_重排字符形成目标字符串)
【C语言】这些经典题型大家都掌握了吗?一文学会这些题
【C语言进阶】指针的进阶【上篇】

