Mailing List

Flat
Fwd: greater circle and close to zero
User: afbach
Date: 8/30/2012 11:42 am
Views: 444
Rating: 0
From the perl beginners list - fellow wanted to do distance
calculations using Math::Trig. After being convinced Geo::Ellipsoid
might be better he noticed that if you give the same coordinates to
the start and end points it doesn't come back w/ a zero but
2.19558883102012e-013

Seems wrong but floating point etc etc and one person noted:
That is 99.99999999999999780441116897988% error. 16 9's is better than
any measuring instrument in existence. I think it'll do:)

and:
It looks like 2*10^-13 miles is about twice the inter atomic distance
in diamond:)

Er, that's close enough ...

a

---------- Forwarded message ----------


>
> The Math::Trig routines all work with radians. Therefore, you are going to have to convert your locations from degrees to radians. You can use the Math::Trig::deg2rad function. Note that pi/2 is radians, so you are subtracting degrees from radians, which never works.
>
> The great_circle_distance function returns radians by default, so you are going to have to convert the return values into miles using the approximate radius of the earth in miles (~3963mi). Note that the earth is not a perfect sphere, so it doesn't have just one radius, but varies from pole to equator.
>
> If you want to do latitude, longitude, and distance calculations, there are modules available from CPAN. I can mention Geo::Distance, GIS::Distance, and Geo::Ellipsoid (which I wrote). These modules take into account the fact that the earth not perfectly spherical.
>
> Here is your program modified to compare results from Geo::Ellipsoid and Math::Trig::great_circle_distance.
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
> use feature qw(say);
> use Math::Trig qw(pi great_circle_distance deg2rad);
> use Geo::Ellipsoid;
>
> my $earth_meters = 6378137.0;
> my $meters_per_mile = 1609.34;
> my $earth_miles = $earth_meters / $meters_per_mile;
> say "The radius of the earth is $earth_miles miles";
>
> my $lat1 = 39.316858;
> my $lat2 = 39.243556;
> my $lon1 = -94.963194;
> my $lon2 = -94.6617;
>
> my $latrad1 = deg2rad($lat1);
> my $latrad2 = deg2rad($lat2);
> my $lonrad1 = deg2rad($lon1);
> my $lonrad2 = deg2rad($lon2);
>
> my $dist = great_circle_distance($lonrad1, pi/2 - $latrad1, $lonrad2, pi/2 - $latrad2);
> say $dist * $earth_meters / $meters_per_mile;
>
> my $ellipsoid = Geo::Ellipsoid->new(units=>'degrees', dist=>'miles');
> say $ellipsoid->range($lat1,$lon1,$lat2,$lon2);
>
>
> Results:
>
> The radius of the earth is 3963.20044241739 miles
> 16.9202528447011
> 16.9368500188459
>

Thank you Jim. Your module seems to be the solution I am looking for.
Just one question. If the two sets of coordinates are the same I was
expecting the distance to be 0 miles.

Instead I get:

2.19558883102012e-013

For the following example.

#!/usr/bin/perl

use strict;
use warnings;

use feature qw(say);
use Geo::Ellipsoid;

my $lat1 = 39.316858;
my $lat2 = 39.316858;
my $lon1 = -94.963194;
my $lon2 = -94.963194;


my $ellipsoid = Geo::Ellipsoid->new(units=>'degrees', dist=>'miles');
say $ellipsoid->range($lat1,$lon1,$lat2,$lon2);

If it is the expected outcome would you please explain why?

Thank you,

Chris

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/




--

a

Andy Bach,
afbach@gmail.com
608 658-1890 cell
608 261-5738 wk
Re: Fwd: greater circle and close to zero
User: tmurray
Date: 8/30/2012 3:02 pm
Views: 0
Rating: 0
I think it's good to hit these examples as soon as possible with new
programmers.  It teaches them to have a healthy mistrust of floating point
numbers.

>
> afbach wrote:

From the perl beginners list - fellow wanted to do
> distance
>  calculations using Math::Trig. After being convinced Geo::Ellipsoid
>  might be better he noticed that if you give the same coordinates to
>  the start and end points it doesn't come back w/ a zero but
>  2.19558883102012e-013
>
>  Seems wrong but floating point etc etc and one person noted:
>  That is 99.99999999999999780441116897988% error. 16 9's is better than
>  any measuring instrument in existence. I think it'll do:)
>
>  and:
>  It looks like 2*10^-13 miles is about twice the inter atomic
> distance
>  in diamond:)
>
>  Er, that's close enough ...
>
>  a
>
>  ---------- Forwarded message ----------
>
>
>  >
>  > The Math::Trig routines all work with radians. Therefore, you are going
> to have to convert your locations from degrees to radians. You can use
> the Math::Trig::deg2rad function. Note that pi/2 is radians, so you are
> subtracting degrees from radians, which never works.
>  >
>  > The great_circle_distance function returns radians by default, so you
> are going to have to convert the return values into miles using the
> approximate radius of the earth in miles (~3963mi). Note that the earth
> is not a perfect sphere, so it doesn't have just one radius, but varies
> from pole to equator.
>  >
>  > If you want to do latitude, longitude, and distance calculations, there
> are modules available from CPAN. I can mention Geo::Distance,
> GIS::Distance, and Geo::Ellipsoid (which I wrote). These modules take
> into account the fact that the earth not perfectly spherical.
>  >
>  > Here is your program modified to compare results from Geo::Ellipsoid
> and Math::Trig::great_circle_distance.
>  >
>  > #!/usr/bin/perl
>  >
>  > use strict;
>  > use warnings;
>  > use feature qw(say);
>  > use Math::Trig qw(pi great_circle_distance deg2rad);
>  > use Geo::Ellipsoid;
>  >
>  > my $earth_meters = 6378137.0;
>  > my $meters_per_mile = 1609.34;
>  > my $earth_miles = $earth_meters / $meters_per_mile;
>  > say "The radius of the earth is $earth_miles miles";
>  >
>  > my $lat1 = 39.316858;
>  > my $lat2 = 39.243556;
>  > my $lon1 = -94.963194;
>  > my $lon2 = -94.6617;
>  >
>  > my $latrad1 = deg2rad($lat1);
>  > my $latrad2 = deg2rad($lat2);
>  > my $lonrad1 = deg2rad($lon1);
>  > my $lonrad2 = deg2rad($lon2);
>  >
>  > my $dist = great_circle_distance($lonrad1, pi/2 - $latrad1, $lonrad2,
> pi/2 - $latrad2);
>  > say $dist * $earth_meters / $meters_per_mile;
>  >
>  > my $ellipsoid = Geo::Ellipsoid->new(units=>'degrees', dist=>'miles');
>  > say $ellipsoid->range($lat1,$lon1,$lat2,$lon2);
>  >
>  >
>  > Results:
>  >
>  > The radius of the earth is 3963.20044241739 miles
>  > 16.9202528447011
>  > 16.9368500188459
>  >
>
>  Thank you Jim. Your module seems to be the solution I am looking for.
>  Just one question. If the two sets of coordinates are the same I was
>  expecting the distance to be 0 miles.
>
>  Instead I get:
>
>  2.19558883102012e-013
>
>  For the following example.
>
>  #!/usr/bin/perl
>
>  use strict;
>  use warnings;
>
>  use feature qw(say);
>  use Geo::Ellipsoid;
>
>  my $lat1 = 39.316858;
>  my $lat2 = 39.316858;
>  my $lon1 = -94.963194;
>  my $lon2 = -94.963194;
>
>
>  my $ellipsoid = Geo::Ellipsoid->new(units=>'degrees', dist=>'miles');
>  say $ellipsoid->range($lat1,$lon1,$lat2,$lon2);
>
>  If it is the expected outcome would you please explain why?
>
>  Thank you,
>
>  Chris
>
>  --
>  To unsubscribe, e-mail: beginners-unsubscribe@perl.org
>  For additional commands, e-mail: beginners-help@perl.org
>  http://learn.perl.org/
>
>
>
>
>  --
>
>  a
>
>  Andy Bach,
>  afbach@gmail.com
>  608 658-1890 cell
>  608 261-5738 wk
>
> View Online
>
>  Madison Area Perl Mongers - MadMongers
>  http://www.madmongers.org
>

Re: Fwd: greater circle and close to zero
User: chrisdolan
Date: 8/30/2012 4:34 pm
Views: 0
Rating: 0
> tmurray wrote:
> I think it's good to hit these examples as soon as
> possible with new
>  programmers.  It teaches them to have a healthy mistrust of floating
> point
>  numbers.
>

[snip]

Where's the +1 button on this mailing list for that "healthy mistrust"
comment. :-)
Chris

PreviousNext
Madison Area Perl Mongers