0

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 :)

Kishore Kumar Korada
  • 1,204
  • 6
  • 22
  • 47
  • 2
    possible duplicate of [Java BigDecimal trigonometric methods](http://stackoverflow.com/questions/2173512/java-bigdecimal-trigonometric-methods) – Claude Martin Nov 25 '14 at 09:20
  • 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 Nov 25 '14 at 12:24
  • @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 understand – Kishore Kumar Korada Nov 26 '14 at 00:21
  • 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 Nov 26 '14 at 13:25
  • @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 expect – Kishore Kumar Korada Nov 27 '14 at 13:12
  • Double should be able to handle that conversion easily. Where is the loss of precision occurring? Is it in the conversion to double from BigDecimal? The trigonometric functions? The accumulation steps? – Bob Dalgleish Nov 27 '14 at 13:54

1 Answers1

0

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
Community
  • 1
  • 1
drgPP
  • 926
  • 2
  • 7
  • 22
  • I don't think that this is what @Kishore wants ("Due to loss of accuracy in precision") – reto Nov 25 '14 at 09:25
  • Yes, my bad. I didnt read the method note: Note that even when the return value is finite, this conversion can lose information about the precision of the BigDecimal value. So it's an bad aproach, thanks reto. – drgPP Nov 25 '14 at 09:28
  • 1
    @drgPP double values doesn't not give accurate result so bd.doubleValues(); still gives same result. It doesn't help me. Thank you though for your help :) – Kishore Kumar Korada Nov 25 '14 at 09:56