浮点上下文中的整数除法

非常基础,编码过程中,容易被忽略的错误,两个整数相除,结果必定是整数,

如果用float、double等数据类型接收,语法上不构成错误,但是会丢失精度。

/**
 * @author css
 * @date 2019/9/30 9:39
 */
public class Test {
    public static void test(double d){
        System.out.println(d);
    }

    public static void main(String[] args) {
        int a = 1;
        int b = 2;
        test(a/2);
        float c = a/b;
        System.out.println(c);
        //此时c的值丢失精度
    }
}

//Idea警告:浮点上下文中的整数除法
integer division in floating-point context

浮点上下文中的整数除法

原文地址:https://www.cnblogs.com/chenss15060100790/p/11681455.html