【问题标题】:Possible lossy conversion from double to float, given float values? [duplicate]给定浮点值,从双精度到浮点的可能有损转换? [复制]
【发布时间】:2014-02-08 12:06:30
【问题描述】:

如果我没记错的话,“0.5”是十进制数;因此,使其成为浮点值。但为什么 java 告诉我它是双精度的?返回语句被 java 检测为错误,说:“不兼容的类型:从双精度到浮点的可能有损转换”

public float typeDmgMultiplr(String type,String type2){
        if(type.equalsIgnoreCase("grass")){
            if(type2.equalsIgnoreCase("grass"))
                return 0.5;
            else if(type2.equalsIgnoreCase("poison"))
                return 0.5;
            else if(type2.equalsIgnoreCase("fire"))
                return 0.5;
            else
                return 2.0;
        }
        else if(type.equalsIgnoreCase("fire")){
            if(type2.equalsIgnoreCase("grass"))
                return 2.0;
            else if(type2.equalsIgnoreCase("poison"))
                return 1.0;
            else if(type2.equalsIgnoreCase("fire"))
                return 0.5;
            else
                return 0.5;
        }
        else if(type.equalsIgnoreCase("water")){
            if(type2.equalsIgnoreCase("grass"))
                return 0.5;
            else if(type2.equalsIgnoreCase("poison"))
                return 1.0;
            else if(type2.equalsIgnoreCase("fire"))
                return 2.0;
            else
                return 0.5;
        }
        else{
            if(type2.equalsIgnoreCase("grass"))
                return 2.0;
            else if(type2.equalsIgnoreCase("poison"))
                return 0.5;
            else if(type2.equalsIgnoreCase("fire"))
                return 1.0;
            else
                return 1.0;
        }

    }

【问题讨论】:

  • 0.5 默认为double 类型。附加 f 使其成为 float - 0.5f
  • doubledouble 精度的浮点数,floatsingle 精度的浮点数。如果您只写0.5,那么它会被解释为double。要使用float 类型,您必须将其写为0.5f
  • 除非你有充分的理由,否则不要使用float。大多数时候这只是一个不必要的头痛。
  • 当你迁移到 Java-8 时,你会更加头疼,发现很多很酷的新特性根本不支持 float 类型。

标签: java


【解决方案1】:

如果我没记错的话,0.5 是十进制数;因此,使其成为浮点值。

在学习一门新的编程语言时,您不应该完全依赖自己的直觉。

事实上,0.5 是一个 double 字面量。对于 float 文字,您需要编写 0.5f

正如 Java 语言规范 (JLS 3.10.2) 所述:

如果浮点文字以 ASCII 字母 Ff 为后缀,则其类型为 float;否则它的类型是double,并且可以选择用ASCII字母Dd作为后缀。

另见:

【讨论】:

    【解决方案2】:

    你有两个选择。

    一个是将方法的返回类型更改为双精度。

    另一个是将返回的双精度值更改为浮点值,正如许多人在 cmets 中所说的那样。

    public float typeDmgMultiplr(String type,String type2){
        if(type.equalsIgnoreCase("grass")){
            if(type2.equalsIgnoreCase("grass"))
                return 0.5f;
            else if(type2.equalsIgnoreCase("poison"))
                return 0.5f;
            else if(type2.equalsIgnoreCase("fire"))
                return 0.5f;
            else
                return 2.0f;
        }
        else if(type.equalsIgnoreCase("fire")){
            if(type2.equalsIgnoreCase("grass"))
                return 2.0f;
            else if(type2.equalsIgnoreCase("poison"))
                return 1.0f;
            else if(type2.equalsIgnoreCase("fire"))
                return 0.5f;
            else
                return 0.5f;
        }
        else if(type.equalsIgnoreCase("water")){
            if(type2.equalsIgnoreCase("grass"))
                return 0.5f;
            else if(type2.equalsIgnoreCase("poison"))
                return 1.0f;
            else if(type2.equalsIgnoreCase("fire"))
                return 2.0f;
            else
                return 0.5f;
        }
        else{
            if(type2.equalsIgnoreCase("grass"))
                return 2.0f;
            else if(type2.equalsIgnoreCase("poison"))
                return 0.5f;
            else if(type2.equalsIgnoreCase("fire"))
                return 1.0f;
            else
                return 1.0f;
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      在 java 中,我们有 float 和 double 作为浮点字面量,其中 double 是浮点字面量的默认数据类型,而不是浮点数。这就是 java 告诉你 0.5 是双精度数的原因。

      可能的转化:

      1) 浮点数 = (float) 0.5;

      2) 浮点数 a = 0.5f;

      有损转换的原因是,double 大于 float。当您尝试将较大的放入较小的时,您会收到此错误。

      【讨论】:

        【解决方案4】:

        在 java 中,任何没有文字的十进制值都被视为Double
        所以你不得不提到表达式后面的Float (F)。 例如。 Float f = 10.5F;

        希望这对您有所帮助。 :)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-07-06
          相关资源
          最近更新 更多