1. Determine whether a number is an integer and call the system function.
# Include <iostream>
# Include <typeinfo>
Using namespace std;
Int main ()
{
Double I = 3;
Cout <typeid (I). name () <endl;
Cout <type ID (3.0000). name () <endl;
System ("pause ");
Return 0;
}
Judge whether a number is an "integer" (it cannot be determined by type, because 4.000 is also an integer) User-Defined Function to judge:
Method: float numbers are somewhat different, for example, 4.000. The full 4.000 is hard to get in the floating point calculation, but when we can enter 4.000 on the keyboard, it must be equal to 4 without any deviation. Suppose we write it as scanf ("% lf", & n) and input 4.000. The result is YES. If we input 4.001, NO is output.
The common idea is: if (n-int (n) = 0)
This method is wrong, because the left is double type, and the right 0 is an integer. It cannot be compared like this. Even if it is changed to 0.00, it is wrong because there is a certain error in the floating point number, 4 may be 4.00... 01, it may also be 3.99... 99, then 4-int (4) may become 0.00... 01 or-0.99... 99. In order to avoid this situation, there must be a certain error with 0. The error range is related to the data type. The double type is generally 1e-15, but the precision (float type) generally 1e-6, so it should be written
If (n> = 0)
If (n-(int) n) <1e-15 | (n-(int) n) <-0.999999999999999)
// The single precision corresponds to 1 E-6 and 6 9, and the double precision corresponds to 1 E-15 and 15 9
Printf ("YES \ n ");
Else
Printf ("NO \ n ");
Else
If (-(n-(int) n) <1e-15 | (n-(int) n) <-0.999999999999999)
Printf ("YES \ n ");
Else
Printf ("NO \ n ");
When n> = 0, as long as (n-(int) n) <1e-15, n is an integer, 0.000... 001 is excluded. As long as (n-(int) n) <-0.999999999999999, n is an integer and-0.999... 999 of the cases are excluded.
When n <0, the situation is similar. As long as-(n-(int) n) <1e-15, n is an integer,-0.000... 001 is excluded; as long as-(n-(int) n) <-0.999999999999999, n is an integer and will be + 0.999... 999 of the cases are excluded.
Note: constant 0.99... The default storage type of 99 is double. If L is added to the end, it is written as 0.99... 99L indicates the long double type. In VC, the long double type is not much different from the double type, so it is generally not used (can be used in GCC ). Similarly, you can add L at the end of an integer to indicate long int. The value range is plus or minus 2.1 billion (in 32-bit systems, both int and long [int] occupy 4 bytes, the value range is also the same, so the long type is generally not commonly used, and can be used in 64-bit systems), such as 212345678L (9-digit, more than 2.1 billion ).
The complete procedure is as follows:
# Include <stdlib. h>
# Include <stdio. h>
Int main ()
{
Double n;
While (scanf ("% lf", & n) = 1)
{
If (n> = 0)
If (n-(int) n) <1e-15 | (n-(int) n) <-0.999999999999999)
// The single precision corresponds to 1 E-6 and 6 9, and the double precision corresponds to 1 E-15 and 15 9
Printf ("YES \ n ");
Else
Printf ("NO \ n ");
Else
If (-(n-(int) n) <1e-15 | (n-(int) n) <-0.999999999999999)
Printf ("YES \ n ");
Else
Printf ("NO \ n ");
}
Return 0;
}
Actual test:
The double type has a valid number of 15 bits. In some cases, it can reach 16 bits. We should test it with data within the valid range.
Test data prediction
0.000000000000001 (14 0) should output NO
0.0000000000000001 (15 0) exceeds the valid range, and the result is uncertain (but NO output is acceptable)
9.000000000000001 (14 0) should output NO
9.0000000000000001 (15 0) exceeds the valid range and the result is uncertain (YES or NO may be output)
Test results:
NO
YES
NO
YES
As expected. Other tests also pass verification.
From zollty's column