浮点数拓展
public class number {
public static void main(String[] args){
float f = 0.1f;//0.1
double d = 1.0/10;//0.1
System.out.println(f==d);
float d1 = 3456365465464f;
float d2 = d1+1;
System.out.println(d1==d2);
}
}
由如上两个例子可以看出浮点数的精度是存在问题的。
浮点数具有有限、离散的特点,存在舍入误差,有着接近但不等于的问题。
因此,最好不要用浮点数比较大小!
至于有需要用到精确的浮点数类型的数据,则采用BigDecimal数学工具类。