What about type conversion in c?
I. Cause
(1) recently, I have been involved in type conversion. For example, converting a signed number to an unsigned number-converting an int to an unsigned int type does not take it for granted;
(2) to make up for the concept that the original code, the reverse code, and the complement code are easy to confuse when I first learned the principles of computer composition;
(3) How can I determine whether a parameter of the double type is zero? It is equal to or equal to 0.0 directly. It seems yes-sometimes, but sometimes not. Why?
(4) Knowledge supplement:In the signed number of int: 0x80000000 (except for the sign bit is 1, all others are zero) ---- minimum number (-2147483648 is-2 ^ 32 );
0x80000001 ---- minimum number of times (-2147483647 is-2 ^ 32-1 );
0 xFFFFFFFE (-2); 0 xFFFFFFFF (all the symbol bits are 1) ----- the largest negative integer (-1 );
0x7FFFFFF (except the sign bit is 0, all others are 1) ---- the maximum number (2147483647 is 2 ^ 32-1)
(5)The problem is, what is 0x80000000-1? (Maximum 0x7FFFFFF); what is 0x7ffffffff + 1? (Minimum: 0x80000000 );
How is the 0x80000000-1 calculated? (0x80000000 + 0 xFFFFFFFF. First, the subtraction is changed to addition, that is, positive 1 is changed to negative 1, and overflow is discarded)
Ii. view the truth
(1) Code
int min_int = 0x80000000; int int_fu1 = 0xffffffff; int max_int = 0x7FFFFFFF; cout << "min_int= " << min_int << "\tmin_int-1= " << min_int-1 << "\t(unsigned)min_int = " << (unsigned int)min_int << endl; cout << "int_fu1= " << int_fu1 << "\tint_fu1-1= " << int_fu1-1 << "\t(unsigned)int_fu1 = " << (unsigned int)int_fu1 << endl; cout << "0x7FFFFFFF= " << 0x7FFFFFFF << "\t0x7FFFFFFF-1= " << 0x7FFFFFFF +1 << endl; double test_zero = 1.333333; cout << (test_zero-1.3333334) << endl; if(0 == (test_zero-1.3333334)) cout << "(test_zero-1.3333334) = " << (test_zero-1.3333334) << "\t0 == (test_zero-1.3333334)" << endl; else if((test_zero-1.3333334)>-1e-6 && (test_zero-1.3333334) < 1e-6) cout << "(test_zero-1.3333334) = " << (test_zero-1.3333334) << "\t(test_zero-1.3333334)>-1e-6 && (test_zero-1.3333334) < 1e-6" << endl;
(2) result chart
(3) Result Analysis
The floating point number (double) cannot use = and! = Because floating point numbers are stored by precision in the memory (the maximum number of precision digits can also be set)So the 3.12 you see may have been the 2-digit precision after the decimal point of 3.123456, so when compared with another 3.12, there may be many situations, if another 3.12 is actually the 2-digit precision after the decimal point of 3.121111, then the two numbers are equal.
Therefore, when comparing floating point numbers, you usually subtract them and then compare them with a precision. However, sometimes the comparison can pass, because it is lucky.
We can see that the minimum number and the maximum number are one step away.
Knowledge supplement of source code anti-Code complement
(4) I am not a qualified programmer and I once again feel my ignorance.
Distribution of c language variables frequently asked during the c ++ deep copy and shallow copy interviews in the memory (VC6.0)