![]() |
|
Orbiter SDK Orbiter software developers post your questions and answers about the SDK, the API interface, LUA, meshing, etc. |
![]() |
|
Thread Tools |
![]() |
#1 |
Aspiring Addon Developer
|
![]()
Hi People.
Quick question, probably not quick answer..... On a distance calculation I'm getting a value of "-1.#IND" intermittently. It's messing up an autopilot. Is it an overrun of some kind. If so, what do I need to do....? Code used: Code:
dis = sin((latitude)*RAD) * sin((tgtlat)*RAD) + cos((latitude)*RAD) * cos((tgtlat)*RAD) * cos((longitude-tgtlng)*RAD); dis = acos(dis); dis = (dis)*DEG; dis = dis * 6371; Last edited by JMW; 09-16-2019 at 03:56 PM. Reason: include code |
![]() |
![]() |
![]() |
#3 |
Orbiter Founder
![]() |
![]() Quote:
Quote:
|
![]() |
![]() |
Thanked by: |
![]() |
#4 |
Not funny anymore
![]() ![]() |
![]() Quote:
Not really, but the other terms should not exceed 2.0 in any case and not 1.0 if the shortest great circle distance is searched. Was just the first difference to the reference I noticed |
![]() |
![]() |
![]() |
#5 |
Orbiter Founder
![]() |
![]()
Also, for the cases where it doesn't fail, does it give you the expected result? I am a bit concerned about those lines:
Code:
dis = (dis)*DEG; dis = dis * 6371; |
![]() |
![]() |
![]() |
#6 |
Aspiring Addon Developer
|
![]()
Gentlemen, sorry for the delay, have been checking a few things.
Urwumpe: I've tried 'fabs(longitude-tgtlng)' and it sent the calcs haywire. martins: By 'the input' do you mean the result of line 1 ? That does seem to be within -1 && 1 always. Yes, when it doesn't read "-1.#IND" it gives a perfect distance. It's occurring whilst hovering directly over a pad, so very small distances are involved.... I got that formula from a website and also from a post in the forum, but can't remember whose. |
![]() |
![]() |
![]() |
#7 |
Not funny anymore
![]() ![]() |
![]() Quote:
That shouldn't happen at all, since it, as martins pointed out, should be just a superfluous term if cos is properly implemented. "-1.#IND" is the windows version of telling you "NaN" or "not a number", which occurs, when the result of an floating point operation is undefined. acos(1) and acos(-1) should be no NaN, but acos(1.0000000000000000000000000000000000000000000 000000000000000000000000000000001) would be one. Your formula is also mentioned in Wikipedia for great circle distance and in the math book I used at university, but always with an absolute definition of difference in latitude. |
![]() |
![]() |
Thanked by: |
![]() |
#8 |
Orbinaut
|
![]()
Have a look at this site with formulas. Under the head "Spherical Law of Cosines", it says that you might need a different formula for very small distances: http://www.movable-type.co.uk/scripts/latlong.html
|
![]() |
![]() |
Thanked by: |
![]() |
#9 |
Aspiring Addon Developer
|
![]()
Urwumpe: Apologies, I didn't implement fabs correctly. Is fine now.
Marijn: I'll take a look there later, (RL taking over for now) Sounds like the small distance might be the culprit. Thanks all. ---------- Post added at 07:48 PM ---------- Previous post was at 07:30 PM ---------- martins: What did you use for distance calc in VOR/VTOL mfd ? (if it's not classified ![]() ---------- Post added 17-09-19 at 05:48 PM ---------- Previous post was 16-09-19 at 07:48 PM ---------- martins: Can it be "open source" ? ^ ^ ^ ^ ^ ![]() I'm curious because there is about 0.22 km difference (mine greater) at 2 km, reducing to 0.11 at 1 km, 0.05 at 500 mtrs, down to less than 0.09 at zero. |
![]() |
![]() |
![]() |
#10 |
Not funny anymore
![]() ![]() |
![]() Quote:
Which radius did you use and what is the true radius? |
![]() |
![]() |
![]() |
#11 |
Aspiring Addon Developer
|
![]()
6371 km.........
---------- Post added at 06:42 PM ---------- Previous post was at 06:37 PM ---------- True... depends where you are ? Quote:
|
![]() |
![]() |
![]() |
#12 |
Orbiter Founder
![]() |
![]() Quote:
Code:
void Orthodome (double lng1, double lat1, double lng2, double lat2, double &dist, double &dir) { double A = lng2-lng1; double dlng = fabs(A); double dlat = fabs(lat2-lat1); if (dlat < 1e-14) { dist = dlng; dir = (lng2 > lng1 ? PI05 : 3*PI05); return; } else if (dlng < 1e-14) { dist = dlat; dir = (lat2 > lat1 ? 0.0 : PI); } else { double sinA = sin(A), cosA = cos(A); double slat1 = sin(lat1), clat1 = cos(lat1); double slat2 = sin(lat2), clat2 = cos(lat2); double cosa = slat2*slat1 + clat2*clat1*cosA; dist = acos (cosa); dir = atan2 (sinA*clat2, clat1*slat2 - slat1*clat2*cosA); if (dir < 0.0) dir += Pi2; // 0 <= dir < 2pi } } Orthodome (sp->lng, sp->lat, tlng, tlat, adist, bdir); bdir -= sp->dir; if (bdir < 0.0) bdir += Pi2; else if (bdir >= Pi2) bdir -= Pi2; dist = adist * sp->ref->Size(); |
![]() |
![]() |
Thanked by: |
![]() |
#13 |
Aspiring Addon Developer
|
![]()
A "mistake" ?
No radius ! We're now in sync ![]() ![]() |
![]() |
![]() |
![]() |
#14 |
Orbiter Founder
![]() |
![]()
So, was the discrepancy just down to a difference in radius? It sounded quite significant.
|
![]() |
![]() |
![]() |
#15 |
Aspiring Addon Developer
|
![]()
Dunno.
I ended up adding this dist = dist *6371; deleting sp->adist * ref->Size(); Last edited by JMW; 09-18-2019 at 04:03 PM. |
![]() |
![]() |
![]() |
|
Thread Tools | |
|
|
Quick Links | Need Help? |