Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible lossy conversion from double to float, given float values? [duplicate]

Tags:

java

if i'm not mistaken, "0.5" are decimal numbers; thus, making it a float value. but why is java telling me that it is a double?. the return statements are detected as errors by java, saying: "incompatible types:possible lossy conversion from double to float"

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;
        }

    }
like image 952
user3202989 Avatar asked Feb 08 '14 12:02

user3202989


People also ask

What is possible lossy conversion from double to float?

As float can have decimal values that don't have corresponding long value. Therefore, we'll receive the same error. The double values can be too large or too small for an int and decimal values will get lost in the conversion. Hence, it is a potential lossy conversion.

Can double be converted to float?

Here double is a primitive data type its object can't be used to call the Float class method.

How do you solve error loss lossy from double to int?

Solution: 1) be consistent, and 2) use names starting with lowercase letters for method names ... as per the Java style guides. The second problem is due to the method signature for Math. pow ; see the javadoc. It returns the result as a double .


2 Answers

If I'm not mistaken, 0.5 are decimal numbers; thus, making it a float value.

You should not rely solely on your intuition when learning a new programming language.

In fact, 0.5 is a double literal. For a float literal you need to write 0.5f.

As The Java Language Specification (JLS 3.10.2) states:

A floating-point literal is of type float if it is suffixed with an ASCII letter F or f; otherwise its type is double and it can optionally be suffixed with an ASCII letter D or d.

See also:

  • The Oracle Java Tutorial > Primitive Data Types
like image 137
2 revs Avatar answered Oct 05 '22 22:10

2 revs


You have two options.

One is change your method's return type to double.

The other is change the double values you are returning to float values, as many have said in the comments.

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;
    }

}
like image 21
pablosaraiva Avatar answered Oct 05 '22 23:10

pablosaraiva