"Possible loss of precision" in my Java program

I am new to Java. I wrote the following code:

import java.io.*;
import java.lang.*;

public class distravel
{
    public static void main(String args[])
    {
        String a1,a2,a3;
        int x=2;
        float d,u,a,t;
         //d=distance travelled,u=initial velocity,a=acceleration,t=timeinterval

        try
        {
            InputStreamReader read=new InputStreamReader(System.in);
            BufferedReader buff=new BufferedReader(read);

            System.out.print("Enter the INTIAL VELOCITY:");
            a1=buff.readLine();
            u=Float.parseFloat(a1);
            System.out.print("Enter the ACCELERATION:");
            a2=buff.readLine();
            a=Float.parseFloat(a2);
            System.out.print("Enter the TIME:");
            a3=buff.readLine();
            t=Float.parseFloat(a3);

            d=((u*t)+a*Math.pow(t,x))/2F;

            System.out.print("The total DISTANCE TRAVELLED:"+d);
        }
        catch(Exception e)
        {}
    }
}

I get this error:

distravel.java28: possible loss of precision
                                   found: double
                                   required: float
                                   d = ((u * t) + a * Math.pow (t, x)) / 2F;
                                                            ^

How can i solve this?

+5
source share
5 answers
d=((u*t)+a*Math.pow(t,x))/2F;

it should be

d=(float)((u*t)+a*Math.pow(t,x))/2F;

or declare das doublelike GrahamS .

+7
source

, ( ). , , , .

+4

Math.pow double, double 'd'. 'd' .

+3

, Math.pow() double, . , , . . :
1)
2) float: d = (float) ((u * t) + a * Math.pow(t, x))/2F;

+2

float, , , ram, double, .

0

All Articles