关于Java浮点运算精确的问题
巴扎黑
巴扎黑 2017-04-17 17:38:20
0
2
303

早知道在java中,浮点运算会出现不精确的情况。解决方案是BigDecimal类。
今天发现个问题:

System.out.println(2-1.1);//0.8999999999999999
System.out.println(2.0-0.9);//1.1

只要没有整数位的都会出现问题?
Why?

巴扎黑
巴扎黑

reply all (2)
左手右手慢动作

The reason is that floating point numbers are composed of two parts: exponent and mantissa. After floating point numbers are converted into binary and participate in calculations, the conversion process will become unpredictable and irreversible
The second situation you mentioned It just happened that no accuracy loss was noticed during the conversion process

    Peter_Zhu

    This problem is not limited to Java, other languages may also have this problem.

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!