首页 > 解决方案 > Java 中的扩展或自动类型转换;为什么会有精度损失?

问题描述

以这段代码为例。

class Main {
  public static void main(String[] args) {
   int i = 41;
   long l = i*9/5;  //no explicit type casting required
   float f = i*9/5; //no explicit type casting required
   double d = i*9/5;    //no explicit type casting required
   double e = (double) i*9/5;
   System.out.println("Int value "+i);
   System.out.println("Long value "+l);
   System.out.println("Float value "+f);
   System.out.println("Double value "+d);
   System.out.println("Double value cast "+e);
  }
}

目标类型大于源类型,因此不需要显式转换,但为什么会丢失精度?为什么我不能得到d73.8 f

标签: javacastingprecision

解决方案


从技术上讲,这里发生的不是精度损失,而是整数除法的使用。

让我们来看看ffloat f = i*9/5;

因为乘法和除法运算符具有相同的优先级并且是左结合的,所以该语句等价于:float f = (i*9)/5;

因为i=419都是int-typed,所以结果值的类型也是int( 369)。因此,评估的下一步是369/5。因为两个操作数的类型都是int,所以运算符的解释/是使用整数除法,它会丢弃任何小数余数。因为369/5 = 73 + 4/5,表达式的计算结果为73,然后将其转换为float73.0并分配给f

一个非常相似的过程发生了d,除了73is 到一个double值的最终转换73.0

请注意,尽管我在评估中包括了中间步骤,但左结合性对整数除法的使用没有影响。float f = i*(9/5);结果f = 41.0


推荐阅读