|
|
View previous topic :: View next topic |
Author |
Message |
AdamkT1
Joined: 21 Apr 2007 Posts: 44
|
Math Problems |
Posted: Thu Aug 23, 2007 8:02 am |
|
|
I am developing a code to calculate distance between two points.
The coordinates being used are LAT/LON.
My assumed figures for the two points are:
Point1: LAT1=31.444 deg, LON1=76.244 deg.
Point2=LAT2=31.434 deg, LON2=76.234 deg.
AS per my calculations the two points are 1462 meters apart.
But my code for the distance calculations (initially) show 0.004.
I need guidance from the learned ones to please pull me out of situation.
Below is the chunk of the code doing the calculations.
Code: | #define EarthRad 6371
float rLat,rLon,cLat,cLon;
float DTR(float Ang1)
/*
Converts degrees into radians
*/
{
float Val;
Val = Ang1 * (Pi / 180.0);
return(val);
}
float GetDist()
{
/*
Calculates distance between two points in meters.
Returns the value in meters
*/
float lat1, lon1, lat2, lon2;
double d,d2;
//Assumed values
cLat=31.444;
cLon=76.244;
rLat=31.434;
rLon=76.234;
lat1=dtr(rLat);
lon1=dtr(rLon);
lat2=dtr(cLat);
lon2=dtr(cLon);
d=Sin(Lat1)*Sin(Lat2);
d2=cos(lon2-lon1);
d2=d2*cos(Lat1)*cos(Lat2);
d=acos(d+d2);
d=d*1000;
d=d*EarthRad;
printf("\n\r * d= %f4", d);
return(d);
} |
|
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Thu Aug 23, 2007 8:15 am |
|
|
You have your function GetDist() declared as a float and returning a double. Inside it you are mixing doubles and floats and hoping the compiler will convert the values, between them, properly. Your printf() statement is trying to print a float format but using a double as it's variable. You might want to case your doubles so they are compatible with the floats they are multiplied with.
Counting on the compiler to convert different data types properly is not a good idea. Always assume the compiler doesn't know how to do this and case your variables so you have apples being used with apples. If you don't, you might end up with prunes.
Ronald |
|
|
Ttelmah Guest
|
|
Posted: Thu Aug 23, 2007 9:00 am |
|
|
Without looking at the code, there is a potential problem here. The CCS compiler, _only supports sngle precision floats_. 'Double', does nothing, the compiler just treats this as a normal single precision float. Now, depending on the formula chosen, calculating distance between points, on a spherical surface, can give wildly 'wrong' results, if very high precision is not available.
Look at the 'Haversine' formula, rather than the formula shown. You will find the results will be 'better'. However remember that the basic accuracy of the arithmetic is only about 6.5 digits, so when dealing with something the size of the Earth, you are looking at errors of a significant number of metres.
A search for Haversine here, may well find a discussion about this.
Best Wishes |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Thu Aug 23, 2007 10:12 am |
|
|
For accuracy you need to use the Haversine and I'd look at 32 bit arithmetic .
There are 32 bit CORDIC routines in the code library which will give you 9 significant digits and 100 to 1000 times the accuracy of single precision floating point. Now Haversine assumes a spherical Earth but for short distances the deviation from a perfect sphere won't be a problem. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|