[C ++] Why int and float types occupy 4 bytes, but float type represents a lot of big scope than an int?

Disclaimer: This article is a blogger original article, reproduced, please indicate the source https://blog.csdn.net/C2681595858/article/details/84865920
  • The reason is simple, that is, an int is stored directly in binary form, and a float is exponentially save.
  • For starters, if the content below to read on the harvest will certainly not small, but there will be some challenges, I hope you Jing Xiaxin carefully read on.

One, background

  • These days writing met with type int and float maximum problems during the initialization of variables "Job algorithm design and analysis." Checked, int INT_MAX can initialize <limits.h> in, float can be used to initialize FLT_MAX in <float.h>, but I am very uncomfortable, why are the maximum basic types, have included two heads file, can not be lazy, with a maximum int or float float directly to initialize the maximum value initialization int it? According to previous misunderstanding, int, and float the number of bytes the same, but the float fractional part, so more narrow range it represents, I wrote a program it looked at, both in the end how much difference the practice of risk lazy how high, so I wrote the following program:
#include <iostream>
#include <limits.h>
#include <float.h>
using namespace std;
int main()
{
  cout<<INT_MAX<<endl;
  cout<<FLT_MAX<<endl;
  return 0;
}

The results are as follows:
Here Insert Picture Description

  • Do not try do not know, try surprised ah, float indicates how the range will be so great, they will not use this memory space is not the same as it:
#include <iostream>
#include <limits.h>
#include <float.h>
using namespace std;
int main()
{
  cout<<INT_MAX<<endl;
  cout<<FLT_MAX<<endl;
  cout<<sizeof(int)<<endl;
  cout<<sizeof(float)<<endl;
  return 0;
}
  • result:
    Here Insert Picture Description
  • Sure enough, I was too dishes, then have a look in the end is how it happened:

Two, int type analysis

1, analysis

  • with 4-byte int save up represents 2 4 8 = 2 32 2^{4*8} = 2^{32} , positive and negative, it may represent 2 31 1 2^{31}-1 positive number, i.e., a maximum value of 2147483647, and consistent with the above results, -2147483648 minimum value, maximum value smaller than the minimum value of the absolute value 1, since 0 represents occupied places a positive number.
  • details as follows:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
  • A total of 32, which means that it can store the full number is 32 0 to 32 full-1. If so, it can only store positive numbers, but we also used to calculate a negative number, which can be looking great ah. So in order to be able to store energy that is positive and negative numbers can be stored, the computer scientists in the design of this 32 when data were reinterpreted, starting with all zeros until, in addition to most former, and the remaining bits are 1, these represent their corresponding positive number. This form is the following
    from all 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

In addition to the first one is beyond all 0 1

0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

The above range are a positive number. You can calculate the maximum int type is just above this number corresponding.

  • The rest are negative numbers, then the problem again, there are these negative numbers indicate how it?
    Then we analyze the above, the maximum number of positive representation (that is written above, except the first one is that all forms of outside 0 1) in the plus-one, it becomes like the following:
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

This number is interpreted as the smallest negative number the computer is -2147483648, and so on, every representation of 32 plus 1, it represents a form will add up to the biggest negative number -1,32-bit representation to become 1 full

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Well int stored on roughly explain, but I mastered these in the end right? Here we look at the use of assembly, in the end is not the case:

2, verification (assembly)

  • Here with mips assembler to view its storage, the following piece of code running in assembler Qtspim in (please delete the comment characters before running):
.data
int_1: .word 0                                   ;定义了一个4字节的变量int_1,并把它赋值为0
int_2: .word 2147483647		;定义了一个4字节的变量int_2,并把它赋值为2147483647
int_3: .word -1					;定义了一个4字节的变量int_3,并把它赋值为-1
int_4: .word -2147483648		;定义了一个4字节的变量int_4,并把它赋值为-2147483648
.text
main:
	 lw $t1,int_1			;把上面定义的变量int_1取值到寄存器t1中
	 lw $t2,int_2			;把上面定义的变量int_2取值到寄存器t2中
	 lw $t3,int_3
	 lw $t4,int_4
		  

See register as follows:
Here Insert Picture Description
and the same as the case explained above.

Three, float type analysis

1, analysis

  • float is also 4 bytes saved but his save in the form of the following formula:
    Here Insert Picture Description
  • That sign bit is the first position, if it is 0 for positive and 1 for negative
  • 2-9 8 bits are bit exponent, where the exponent is used with a partial order counting method 1 , that is, where the real storage index = index + 127, where 127 is its partial order. It can store a maximum index of how much of it, can represent up to 8 2 8 1 2^{8}-1 digit numbers is 255, all-0 from (0) to 1 (255). However IEEE754 full index number 1 is not represented; real index = index -127 stored here, it is possible to store the maximum index is 254-127 = 127, capable of storing the minimum index is 1-127 = -126, where the minimum 1 with a reduced, but not with 0, is because when the index 0, while nonzero mantissa, it is not normalized number. Therefore, the range of the final index portion can be represented by -126 to 127
  • Looking mantissa part, IEEE754 hides a leading one, that is used to store the real mantissa mantissa = - 1, so we must add the data stored in the original data reduced to 1.

2, verification (assembly)

.data
float_1: .float -0.5
float_2: .float 0.0
float_3: .float 3.1875
.text
main:
	 lw $t1,float_1
	 lw $t2,float_2
	 lw $t3,float_3
		

The results are as follows:
Here Insert Picture Description
the results of the analysis:

  • -0.5, it is negative for the first 1, 0.5 = 0. 1 2 0.1_{2} = 1.0 2 ( 2 ) 1 1.0*2^{-1}_{(2)} Therefore it should be the index portion 127 = 126 = -1 + 0111111 0 2 01111110_2 , Ending 1.0, 1 hidden and consistent end results and above.
  • 0, sign bit 0 and 1 will do, as long as the exponent and the mantissa sections are to 0.
  • 3.1875 into binary is 11.001 1 2 11.0011_2 ( Decimal floating-point numbers into binary ), and scientific notation as 1.1001 1 2 1.10011_2 * 2 1 2^{1} , so exponent parts 1,1 + 127 = 128, is a binary Huawei 1000000 0 2 10000000_2 Mantissa remove the hidden part 1, that is, 1001 1 2 10011_2 When shown above, t3 t1 less than one, because t3 is positive, the sign bit is omitted zero display.

  1. Specific reading this book Computer Organization and Design This chapter tells about the third part of the floating-point representation is useful. ↩︎

Guess you like

Origin blog.csdn.net/C2681595858/article/details/84865920