1. Float Type Definition
The float keyword indicates a simple type for storing 32-bit floating point values, as shown in Table 4-5.
Table 4-5 Float Type
Type |
Fan Wei |
Big and small |
Type |
Float |
± 1. 5 × 10-45 to ± 3. 4 × 1038 |
7-digit |
System. Single |
2. Float Type identifier
By default, the real number on the right of the value assignment operator is treated as double. Therefore, use the suffix f or F to initialize floating point variables, as shown below:
Note:
If no suffix is used in the preceding statement, a compilation error occurs because you try to store a double value in the float variable.
3. Float Type Conversion
Both integer and floating point types can be used in an expression. In this case, the integer type is converted to the floating point type. Calculate the expression according to the following rules:
If one of the floating point types is double, the calculation result of the expression is double type, and the relational expression or Boolean expression is bool type.
If the expression does not have the double type, the calculation result of the expression is of the float type, and the relational expression or Boolean expression is of the bool type.
A floating-point expression can contain the following values:
Positive and Negative
Positive infinity and negative infinity
Non-numeric value (NAN)
A finite set of non-zero values
Function Description: In the following example, a float result is obtained for mathematical expressions of the int, short, and float types. Note that there is no double in the expression.