1. In the computer, the floating-point number is different from the decimal number.
Public Static void Main (string[] args) { double B1 = 0.1; double b2 = 0.2; double result = B1 + B2; System.out.print (result); }
Results: 0.30000000000000004
Cause: The computer uses the binary to store the data.
PS. Use floating-point numbers to make conditional judgments. For example (while (f! = 0.3) can always loop).
2. Large numbers plus decimals in floating-point numbers
Public Static void Main (string[] args) { float f1 = 30000000; float F2 = 1; float r = f1 + F2; if (R < F1) System.out.println ("left smaller"); Else System.out.println ("What?") ); }
Result: What?
Cause: The larger the floating-point number, the greater the gap between the two adjacent floating-point numbers.
3. Floating-point numbers to other types
Public Static void Main (string[] args) { System.out.println ((int) double.nan); System.out.println ((int) double.positive_infinity); System.out.println ((int) double.negative_infinity); }
Results: 0
2147483647
-2147483648
Java Floating point number (note)