java - Average Speed Calculation returns 'infinity' -


on android google map application i'm trying calculate average speed taking time (in milliseconds) , distance (in meters) , performing next calculation on it:

private double avgspeed = 0; public void calcavgspeed(double distance, long time) {     distance = distance / 1000;      //1000 = km     time = time / 3600000;           // 1000 = sec, 60000 = min, 3600000 = hrs     avgspeed = (double) (distance / time); } 

to make sure works ran calcavgspeed(300, 30000); in 300 300 meters , 30000 30000ms(or 30sec). calculations made avgspeed should 36 after method. when print result says 'infinity' if i'm dividing zero..

does see if method wrong or why weird answer?

you indeeed dividing zero.

time = time / 3600000; 

performs integer division since time of type long.

to fix use like

avgspeed = distance * 3600000 / time; 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Why am I getting Internal .NET Framework Data Provider error 1025 when passing Method to where? -

linux - phpmyadmin, neginx error.log - Check group www-data has read access and open_basedir -