So i am writing some code to calculate the miles driven based off the gps latitude and longitude store in an `ArrayList`, but for some reason randomly I get the `NaN` and I can't figure out why. Could someone please let me know what is wrong with my code or if there is a better way I should be doing it?

```
protected float calculateMiles()
{
    float totalDistance = 0;

    for(int i = 1; i < points.size(); i++) {
        double lat = points.get(i).latitude;
        double lng = points.get(i).longitude;

        double lat2 = points.get(i-1).latitude;
        double lng2 = points.get(i-1).longitude;
        totalDistance += distance(lat, lng, lat2, lng2);
    }

    return totalDistance;
}

    private double distance(double lat1, double lon1, double lat2, double lon2) {
    double theta = lon1 - lon2;
    double dist = Math.sin(deg2rad(lat1))
            * Math.sin(deg2rad(lat2))
            + Math.cos(deg2rad(lat1))
            * Math.cos(deg2rad(lat2))
            * Math.cos(deg2rad(theta));
    dist = Math.acos(dist);
    dist = rad2deg(dist);
    dist = dist * 60 * 1.1515;
    return (dist);
}

private double deg2rad(double deg) {
    return (deg * Math.PI / 180.0);
}

private double rad2deg(double rad) {
    return (rad * 180.0 / Math.PI);
}

```

I am also using the same points to draw a polyline and it works out just fine.