“Possible loss of precision” in my Java program

后端 未结 5 1265
北荒
北荒 2021-01-03 03:03

I\'m new to Java. I wrote the following code:

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

public class distravel
{
    public static void main(String args[])
             


        
5条回答
  •  孤街浪徒
    2021-01-03 03:50

    It is because Math.pow() returns a double which you then do some calculations with. No matter what calculations you do the precision you will have to deal with is double. Therefore, you get the error message. Solutions:
    1) make the floats double
    2) cast the result to float: d=(float)((u*t)+a*Math.pow(t,x))/2F;

提交回复
热议问题