浮點上下文中的整數除法


非常基礎,編碼過程中,容易被忽略的錯誤,兩個整數相除,結果必定是整數,

如果用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

浮點上下文中的整數除法


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM