-3

I have a wcf method in which I have to calculate the distance between to latitude longitude point. the distance calculator method takes double value as argument. so when I send latitude value =55.68743, .net converts it to 55.6874299999 and I get wrong distance.

Why the value is converting?? does anybody knows how can I solve it??

Here is some code...

public 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;
        dist = dist * 1.609344;

        return (dist);
    }
    public double deg2rad(double deg)
    {
        return (deg * Math.PI / 180.0);
    }
    public double rad2deg(double rad)
    {
        return (rad / Math.PI * 180.0);
    }

In the above code, I am calculating distance of two points. lat1=55.68743,lon1=12.50400 lat2=55.68758, lon2=12.50403

when I execute, value of lat1 turns to 55.687429999999999 and lon1 to 12.504.

Reza.Hoque
  • 2,690
  • 10
  • 49
  • 78
  • you may want to have a read on Floating Point numbers - http://en.wikipedia.org/wiki/Floating_point. A double is a floating point number. – Russ Cam Aug 24 '11 at 08:41
  • 4
    Have a read of http://stackoverflow.com/questions/618535/what-is-the-difference-between-decimal-float-and-double-in-c – Ray Aug 24 '11 at 08:42
  • Please post some code.. how you send the latitude value? How the method getting it is defined exactly? – Shadow The GPT Wizard Aug 24 '11 at 08:42
  • I'm sure you have floats rather than doubles. – David Heffernan Aug 24 '11 at 08:59
  • As mentioned it is all to do with how the computer stores numbers and unfortunately has trouble storing floats and double to the accuracy you would want. PS I don't think the question deserved to be downgraded. – Shaun Wilde Aug 24 '11 at 10:44
  • possible duplicate of [Double precision problems on .NET](http://stackoverflow.com/questions/566958/double-precision-problems-on-net) – Andrie Aug 24 '11 at 13:40

2 Answers2

5

This is basically due to the fact, that a computer is binary whereas your conventional understanding of mathematics is decimal.

In essence, the computer has no way of exactly expressing 55.68743 in binary floating point formats and thus uses the nearest possible value 55.6874299999

To compare two floating point numbers you subtract the two numbers and check whether the result is below a previously aggreed upon threshold.

double actual = 55.6874299999;
double expected = 55.68743;
if (Math.Abs(actual- expected) < 0.00001) 
{
    // do something
}

The (most commonly used) semantics of floating point arithmetic have been formalized in IEEE 754

Further reading:

yas4891
  • 4,774
  • 3
  • 34
  • 55
  • "no way" is a little exaggerated. Each number of 55.68743 could be also saved as integer or byte value if necessary, – RolandK Aug 24 '11 at 09:19
  • @RolandK thanks for the hint. I updated my answer, now stating that there's no way of expressing this in a floating binary format. – yas4891 Aug 24 '11 at 09:22
0

Try using the decimal structure rather than a floating point type. System.Decimal does not store the information with the same imprecision that typical floating points values have. Unfortunately, they also take up twice the space, but if you need precision, you will have it.

System.Decimal

Bahri Gungor
  • 2,289
  • 15
  • 16