相关疑难解决方法(0)

双原语类型数据的意外行为

我想了解Java double类型如何在Java 中将其值存储在内存中.当我运行以下代码时,我得到意外的输出:

public static void main(String[] args)  {

    float a = 1.5f;
    float b= 0.5f;
    double c= 1.5;
    double d = 0.5;

    float a1 = 1.4f;
    float b1= 0.5f;
    double c1= 1.4;
    double d1 = 0.5;

    System.out.println(" a- b is " + (a-b));
    System.out.println(" c- d is " + (c-d));
    System.out.println("a1-b1 is " + (a1-b1));
    System.out.println("c1-d1 is " + (c1-d1));
Run Code Online (Sandbox Code Playgroud)

}

输出:

 a- b is 1.0
 c- d is 1.0
a1-b1 is 0.9
c1-d1 is 0.8999999999999999

为什么c1-d1 …

java floating-point

7
推荐指数
1
解决办法
859
查看次数

标签 统计

floating-point ×1

java ×1