当前位置:网站首页>[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 :

 

 C Language programming questions

  Classic question series

 

 

List of articles

Catalog

List of articles

Preface

Floating point storage in memory

Examples of floating point storage

Floating point storage rules

A detailed explanation of the example

Entrance to exercises

summary


 

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 different
To 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, namely
10001001.
then , Index E Fetching from memory can be further divided into three cases :
E Not all for 0 Or not all of them 1
At 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 for
1.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 00000000000000000000000
E All for 0
At 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 storage
float  f  = 5.5;
101.1    Binary system
1.011*2^2    Scientific enumeration
S=0   M =1.011  E =2      Express
S Binary for 0    altogether 1 position
E True value 2 Add the median 127 by 129  Binary for 10000001  altogether 8 position
M Round off the number before the decimal , Binary for 011, But not enough , The following is supplemented by 01100000000000000000000, altogether 23 position
Convert 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      Express
S Binary for 0    altogether 1 position
E True value 2 Add the median 127 by 129  Binary for 10000001  altogether 8 position
M Round off the number before the decimal , Binary for 011, But not enough , The following is supplemented by 01100000000000000000000, altogether 23 position
The last stored is :
0   10000001    01100000000000000000000
middle 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^2
And 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 bits
M=000 0000 0000 0000 0000  1001.
9 -> 0000 0000 0000 0000 0000 0000 0000 1001
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 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=130
that , 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, namely
0 10000010 001 0000 0000 0000 0000 0000
This 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 :

Large factory push

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

原网站

版权声明
本文为[You and I are all mortals]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101827265858.html