June 25, 2024, 05:02:13 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Canging type for returned variable?

Started by Egil, October 24, 2014, 12:39:18 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Egil

I am preparing some routines for Great Circle calulations, and made a sub where all variables are floats, also the return value.
But since the return values are in kilometers, I should like to have that value rounded, whithout the decimals.

I noticed that if the return type in the GCDistance sub below is changed from float to int, I get exactly what I want.
So now I wonder if this is a "clean" way of doing it, or can I expect problems?



'
' GCDistance.iwb
' Test program for calculation of Great Cirlce distances
'
OPENCONSOLE

print "Calulation example for distance between 63.42N/007.67E and 69.74N/29.83E:"
print

print GCDistance(63.42, 7.67, 69.74, 29.83)," km"'

print:print
print "press any key to end"

WAITCON
CLOSECONSOLE
END

'
SUB GCDistance(lat1:float, lon1:float, lat2:float, lon2:float),float
'----------------------------------------------------------------------------------------
' Using The Spherical Law of Cosines
' Original source: "http://www.movable-type.co.uk/scripts/latlong.html"
' Positions in decimal format:
' lat1 = position1 latitude
' lon1 = position1 longitude
' lat2 = position2 latitude
' lon2 = position2 longitude
' Use negative values for West longitudes and South latitudes
'----------------------------------------------------------------------------------------
float dLon
float R = 6372.8 ' Average Earth Radius in km
dLon = Deg2Rad(lon2-lon1)
lat1 = Deg2Rad(lat1)
lat2 = Deg2Rad(lat2)
RETURN acos(sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2) * cos(dLon)) * R
ENDSUB

'
SUB Deg2Rad(deg:FLOAT),float
'----------------------------------------------------------------------------------------
' Convert Degress to Radians
' PI = 4 * atan(1)
' deg = degrees in decimal format
'----------------------------------------------------------------------------------------
RETURN deg*(4*atan(1))/180
ENDSUB



Support Amateur Radio  -  Have a ham  for dinner!

LarryMc

If it were me I would convert it to an INT first just to be sure.
int ret=  acos(sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2) * cos(dLon)) * R
Return ret


But it should be no problem if you are getting the correct result.
I don't know if the rounding would be the same in both cases
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Egil

Thanks Larry.

The result was the same with both methods, but i feel more confident with your method. Been sitting in front of this screen way too many hours today. Which for me means that I don see obvious solutions....


Support Amateur Radio  -  Have a ham  for dinner!

LarryMc

Egil
Wait until you work on trying to find a solution all day without success.
Then you do one last search of the forums for a possible solution.

Yeah!!! You find where someone else has had the same problem.
A better yet, someone posted a solution to your problem.
But wait.... the person who provided the solution was YOU!

I do that on a regular basis.  :o
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Egil

Well, I have not yet come to that stage...
Instead I scratched my head for hours trying to find the correct formula, as the one I used obviously was wrong. Only to find that there was a typo made by yours truly....

So now I'll take a break from programming and wait till after the weekend before I start on the great circle bearing formulas...
Support Amateur Radio  -  Have a ham  for dinner!