非常基礎,編碼過程中,容易被忽略的錯誤,兩個整數相除,結果必定是整數,
如果用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
浮點上下文中的整數除法