First, please look at the following code first:
INT main () {float a = 1.1; // 1warning: truncation from 'const double' to 'float' float b = 1.2; // 2Warning: truncation from 'const double' to 'float' float c = 2.3; b = A B; cout << sizeof (float) << Endl; // 3 cout << "c =" << c << "<<" b = "<< b << endl; if (c == b) // 4 Cout << "B is equal to C." << endl; else cout << "B is not equal." << endl; return 0;}
Explanation: 1) In 1, we can see an error message "Transfer to Float to float in the DATA area" 2) in 2 is the same effect. 3) We look at it at 4 The result is not willing to see, and the output "B is not equal to C.".
Second, understand the simple work principle of code
We use the 16-bit system as a control. The bytes of the data type of Float are 4 (can be seen at 3), 32 digits, let's convert the decimal 1.1 into binary representation (use "Take 2 Law "): Parts: ----------------------------------------- --------------- 0.1 * 2 = 0.2 ------ 0 (integer part) 0.2 * 2 = 0.4 ------ 0 (integer part) A 0.4 * 2 = 0.8 ------ 0 (integer part) ------------------------------------ ---------------------- 0.8 * 2 = 1.6 ------ 1 (integer part) 0.6 * 2 = 1.2 ------ 1 (Integer part) 0.2 * 2 = 0.4 ------ 0 (integer part) B 0.2 * 2 = 0.4 ------ 0 (integer part) 0.4 * 2 = 0.8 ------ 0 ( Integer part) ----------------------------------------------- ------------ 0.8 * 2 = 1.6 ------ 1 (integer part) 0.6 * 2 = 1.2 ------ 1 (integer part) 0.2 * 2 = 0.4- ---- 0 (Integer Part) C (Generation Circulation B) 0.2 * 2 = 0.4 ------ 0 (Integer Part) 0.4 * 2 = 0.8 ------ 0 (Integer Part) - -------------------------------------------------- -----.
We see that the cyclic body (ie: the decimal part is loop) when converting 1.1 into binary, and the default floating point number in the VC is Double type (ie 1.1 is Double Double in the default), then more The 32-bit bit value is deleted, and one such 32-bit binary code is obtained to assign a, and the value of A is no longer a decimal 1.1, but a value of approximately 1.1. In the output device we see two equivalents, this is the reason for the compiler for approximation. This is also true of 1.2 (you don't prevent trying to talk). If we change to the following assignment, we will not appear Warning, the truth is also very simple, because 1.5 can be converted in 32 bits, so there is no data loss, and approximate the problem. INT main () {float a = 1.5; // ok float b = 1.5; // ok float c = 3.0; b = a b; cout << sizeof (float) << Endl; cout << "c =" << c << "<<" b = "<< B << endl; if (c == b) cout <<" B is equal to C. "<< Endl; // Expected result ELSE COUT << "B is not equal to C." << endl; return 0;}
Third, the last step
If we change the data type to Double, we will not appear Warning, that is because Double is handled by the compiler yourself, the compiler handles Double data to handle the integer, form: 1.1 * (10), When the compiler is indicated as 11, 1, add a decimal point, which avoids the error generated by the decimal, thereby achieving accurate form; float is 32 bits, will use CPU processing, convert to binary code Approximation; Final, it is to mention that float is 32 bits, which is handled by CPU, faster; Double is 64-bit, then use the compiler to process, after processing, use the CPU to press the integer data to process and convert It is binary code storage; that is compared to Float is faster than Double.