1. Intelligent addition, subtraction, multiplication and division of floating-point numbers , Can not do bit operation and shift operation .

2. Floating point numbers are often not accurately represented , therefore , Floating point operations often produce errors or even errors !!!

for example ：

* 0.1 It cannot be accurately represented in the computer , Namely float and double Can only store 0.1 Approximate value of ;
* 0.5 Can be accurately represented . public class TestFloatCal { public static void main(String[]
args){ double x = 1.0/10; //0.1 double y = 1- 9.0/10;
// When calculating ,0.9 The inaccuracy of the binary representation of 1-0.9≠0.1 double z = 1- 9/10;
// When calculating , To the right of the equal sign is an integer , So the result is 1 of double express 1.0 System.out.println("x="+x);
System.out.println("y="+y); System.out.println("z="+z); } }
output ：

3. A method to determine whether two floating-point numbers are equal ： Judge whether the absolute value of the difference between two numbers is less than a small number .

give an example ：
public class TestFloatEqual { public static void main(String[] args){ double x
= 0.1; double y = 1.0 - 9.0/10; double r = Math.abs(x-y); boolean isEqual =
false; if (r<0.00001){ isEqual = true; } System.out.println(isEqual); } }
output ：

4. overflow ： Floating point number operation in divisor 0 No error will be reported when （ Different from integer operation ）, However, several special values are returned ：

* NaN：Not a Number
* Infinity： Represents infinity
* -Infinity： Represents negative infinity
give an example ：(VScode The output is not the above result ）
public class TestOverFlow { public static void main(String[] args){ double x =
0/0; double y = 1.0/0; double z = -1.0/0; System.out.println("x="+x);
System.out.println("y="+y); System.out.println("z="+z); } }
output ：

5. Forced transformation ： Floating point numbers can be cast to integers , syntax ：(int)(number)

* During transformation , The decimal part of the floating point number is discarded directly ;
* After transformation , If it exceeds the maximum range that the specified integer can represent , Returns the maximum value of an integer ;
* If necessary, round the transition , Floating point numbers can be added 0.5 Re forced transformation .
give an example ：
public class TestFloatToInt { public static void main(String[] args){ double d
= 2.6; int m = (int)(d); // Rounding int n = (int)(d+0.5); // rounding
System.out.println(" Direct transformation to ："+m); System.out.println(" Round to ："+n); } }
output ：

Technology
Daily Recommendation
views 2