0
votes

I have this code

public static double distance(double lat1, double lon1, double lat2, double lon2, char unit) {      
      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;
      if (unit == 'K') {
        dist = dist * 1.609344;     
      } else if (unit == 'N') {     
        dist = dist * 0.8684;   
      }
      return (dist);
    }

But Due to loss of accuracy in precision in the above code I have to change it by giving type as BigDecimal instead of double for lat1, lon1, lat2, lon2. But problem is if I use BigDecimal how can I achieve all those operations?
Please guide me in achieving this
Thank you :)

1
What precision do you need? Calculating deg2rad or sin necessarily are limited precision, so you have to set an acceptable level of error for the whole calculation. I count 20 loss-inducing steps, where each step introduces errors.Bob Dalgleish
@BobDalgleish In my application for processing lat, long values I used double but later found that double rounding those original values and giving inaccurate result and came to know that BigDecimal is best for achieving this problem. So I had to change entire dependencies. While doing so I met this above code where I stuck because you know math won't allow bigdecimal as parameters. So I'm seeking for alternatives Hope you understandKishore Kumar Korada
You didn't answer my question. You said "double ... [gave] inaccurate results". How accurate do you need your results to be? How much precision? Given 20 error-inducing steps, how much error can you tolerate in your final answer?Bob Dalgleish
@BobDalgleish 77.829650, 12.991910, 77.838020...When I read data like this with double I expect output data should be as it is with same precision. But I'm not getting expected result but with BigDecimal I'm getting what I expectKishore Kumar Korada

1 Answers

0
votes

Inspired from this post: How to convert BigDecimal to Double in Java?

May be a conversion should work?

BigDecimal bd; // the value you get
double d = bd.doubleValue(); // The double you want