1
import java.util.*;
import java.lang.*;
import java.io.*;

public class MyClass {
    public static void main (final String[] args) {
        float distanceInMeters = 0.0f;
        long time = 1439230171182L;
        long previousTime = 1439230111554L;
        float speed = (distanceInMeters / 1000) / ((time - previousTime)/3600000);
        System.out.println(speed);
    }
}

This calculation returns "NaN". Does anyone know why?

jfmg
  • 2,626
  • 1
  • 24
  • 32
  • 2
    Why are people down-voting this question? The asker posts code that reproduces the issue and tells us what the issue is. – Mark Aug 10 '15 at 18:47

3 Answers3

8

You're dividing by zero always. You're subtracting two longs then doing integer division. Those two longs will be less than 3600000, so your result will be (distanceInMeters / 1000) / 0 = NaN

Kon
  • 10,702
  • 6
  • 41
  • 58
  • So basically there is just a missing cast to float. Thanks for the answer. The solution is to write `float speed = (distanceInMeters / 1000) / ((float)(time - previousTime)/3600000);` – jfmg Aug 10 '15 at 18:36
  • 1
    You can also just explicitly write `3600000F` – Kon Aug 10 '15 at 18:37
  • Yep. I don't like that my questions has gotten 2 down votes. Everyone can forget about Integer division in Java -.- Especially when you had to write JavaScript for some time. – jfmg Aug 10 '15 at 18:42
  • 2
    @teddy3D I downvoted because this is something really simple you could have debugged yourself. – CubeJockey Aug 10 '15 at 18:43
  • @Trobbins That is what I have done but whatever I did the console just told me the result is NaN. – jfmg Aug 10 '15 at 18:50
  • @teddy3D then you should have broken your math up into separate lines and observed each operation. – CubeJockey Aug 10 '15 at 18:51
  • 1
    I guess debuggers are out of fashion nowadays. – Qix - MONICA WAS MISTREATED Aug 10 '15 at 19:12
  • 2
    This question is perfectly appropriate for StackOverflow. Because the poster does not have the techniques developed to find the answer himself is not a reason for downvoting. He made an effort, posted code, explained the problem, and sought help. – Kon Aug 10 '15 at 19:15
5

Lets put some our values and calculate it:

(distanceInMeters / 1000) / ((time - previousTime)/3600000);

is same as

(       0.0f      / 1000) / ((1439230171182L - 1439230111554L)/3600000)

0.0f/1000 = 0.0f and 1439230171182L - 1439230111554L = 59628L so we get

(           0.0f        ) / ((           59628L              )/3600000)

59628L/3600000 = 0 (integer division)

So we and up with 0.0f / 0 which is NaN (Not a Number).
If you would divide 1.0f / 0 you would get Infinity and for -1.0f / 0 you would get -Infinity

Pshemo
  • 122,468
  • 25
  • 185
  • 269
2

The solution is to add a float cast like this:

float speed = (distanceInMeters / 1000) / ((float)(time - previousTime)/3600000);

or like this:

float speed = (distanceInMeters / 1000) / ((time - previousTime)/3600000.0f);

I forgot about the Integer division in Java. Probably because I wrote a lot of JavaScript code previously.

jfmg
  • 2,626
  • 1
  • 24
  • 32