GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Aug-2008, 14:34
nekavnick nekavnick is offline
New Member
 
Join Date: Aug 2008
Posts: 2
nekavnick is on a distinguished road

Help me write this formula in c++


Hello!

I'm creating a program that calculates the distance between two cities. I have problems with converting the formula to c++ code.
Here is the formula

Code:
d=2*R*sin^-1 (sin^2 (a1-a2)/2 + cos*a1*cos*a2*sin^2(b1-b2)/2)^1/2



Here, d is the distance between the towns, a1 and a2 are their latitudes,b1 and b2 are their longitudes, and R is the radius of the earth.

The latitudes and longitudes are given in degrees but sin and cos functions work with radians.

I've been trying to make this two days and it doesn't work

Here is what I've done

CPP / C++ / C Code:
double d;
d=sin((Pi*((a1-a2)/2.0))/180.0);
d*=d;
double dd;
dd=sin((Pi*((b1-b2)/2.0))/180.0);
dd*=dd;
dd*=cos((Pi*a1)/180.0);
dd*=cos((Pi*a2)/180.0);
d+=dd;
d=pow(d, 0.5);
d=2*R/d;
cout << d << endl;


and also

CPP / C++ / C Code:
d=2*R/sin(Pi*pow( sin((Pi*(a1-a2)/2.0)/180.0)*sin((Pi*(a1-a2)/2.0)/180.0)+cos(Pi*a1/180.0)*cos(Pi*a2/180.0)*sin((Pi*(b1-b2)/2.0)/180.0)*sin((Pi*(b1-b2)/2.0)/180.0), 0.5)/180.0); 



but both methods did not work.

Can you please tell me what I'm doing wrong?
Any help will be appreciated.

p.s. Sorry for my English. I'm still learning
  #2  
Old 23-Aug-2008, 15:57
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Help me write this formula in c++


Quote:
Originally Posted by nekavnick
I have problems with converting the formula to c++ code.
Here is the formula

Code:
d=2*R*sin^-1 (sin^2 (a1-a2)/2 + cos*a1*cos*a2*sin^2(b1-b2)/2)^1/2
What does cos*a1*cos*a2 mean? Is this cos(a1) * cos(a2)?
  #3  
Old 23-Aug-2008, 16:12
nekavnick nekavnick is offline
New Member
 
Join Date: Aug 2008
Posts: 2
nekavnick is on a distinguished road

Re: Help me write this formula in c++


Quote:
Originally Posted by ocicat
What does cos*a1*cos*a2 mean? Is this cos(a1) * cos(a2)?
Yes it is cos(a1)*cos(a2)
Sorry for the mistake
  #4  
Old 23-Aug-2008, 16:16
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Help me write this formula in c++


Quote:
Originally Posted by nekavnick
Can you please tell me...

Without validating your formula and without looking very closely at your "simplifications," I would say that, for one thing, I am almost sure that you have not interpreted the following notation correctly:
Code:
sin^-1(xxx)

I believe that this should be taken to designate arcsin(xxx), not 1.0/sin(xxx). The standard library function asin() calculates the arcsin in radians.

Regards,

Dave

Footnote: I have always used the "haversine" formula (look it up on wikipedia or wherever...)
Code:
d = R * acos(sin(lat1)*sin(lat2)+ cos(lat1)*cos(lat2)*cos(long2-long1));
Where R is the radius of the earth (something like 6371 km). I won't try to reconcile it with yours (there are many ways to express the same operations using various trig identities); but you can check it something like the following
Code:
Enter latitude and longitude of first point in degrees: 58.2628 -122.1092 Enter latitude and longitude of second point in degrees: 57.4744 -8.1620 Distance = 5889.658973 km.

If you convert your latitude and longitude into radians before plugging into the formula, it might look a little cleaner:
CPP / C++ / C Code:
    const double M_PI = 3.14159265358979323846

    const double radians_per_degree = M_PI/180.0;
    double d;

    /* latitude and longitude in radians */
    double lat1, long1, lat2, long2;

    /* latitude and longitude in degrees */
    double lat1_degrees, long1_degrees, lat2_degrees, long2_degrees;
.
.
.
    /* get latitude and longitude in degrees */
        printf("Enter latitude and longitude of first  point in degrees: ");
        if (scanf("%lf %lf", &lat1_degrees, &long1_degrees) != 2) {
            /* bail out of the program or try to recover or whatever... */
        }
        printf("Enter latitude and longitude of second point in degrees: ");
        if (scanf("%lf %lf", &lat2_degrees, &long2_degrees) != 2) {
            /* bail out of the program or try to recover or whatever... */
        }


    /* then */
        lat1  = lat1_degrees  * radians_per_degree; /* now it's in radians */
        long1 = long1_degrees * radians_per_degree; /* now it's in radians */
        lat2  = lat2_degrees  * radians_per_degree; /* now it's in radians */
        long2 = long2_degrees * radians_per_degree; /* now it's in radians */
..
.
.
       d = /*function of lat and long in radians */
  #5  
Old 23-Aug-2008, 16:26
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Help me write this formula in c++


Quote:
Originally Posted by davekw7x
I would say that, for one thing, I am almost sure that you have not interpreted the following notation correctly:
Code:
sin^-1(xxx)
sin^-1(x) is frequently the way arcsin(x) is typeset in textbooks. I don't suspect that the OP intended it to be correct C syntax. In C, arcsine is defined as the function asin(double) prototyped in math.h as I am sure you know.
  #6  
Old 23-Aug-2008, 17:05
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Help me write this formula in c++


Quote:
Originally Posted by nekavnick
Here is the formula

Code:
d=2*R*sin^-1 (sin^2 (a1-a2)/2 + cos*a1*cos*a2*sin^2(b1-b2)/2)^1/2
The first thing is to build the solution incrementally to ensure that the solution is correct. After the implementation is validated, then you can worry about whether it is coded in an optimal fashion.

So, beginning with the following assumptions:
  • sin^-1(x) == arcsin(x)
  • sin^2(x) == (sin(x))^2
Decompose the argument to asin() as two separate terms:
  • Code:
    sin^2 (a1-a2)/2
    ...which becomes:
    CPP / C++ / C Code:
    double latitude, term1;
    latitude = (a1 - a2) * M_PI / 180.0;
    term1 = pow(sin(latitude), 2.0) / 2.0;
  • Code:
    cos*a1*cos*a2*sin^2(b1-b2)/2
    ...becomes:
    CPP / C++ / C Code:
    double longitude, term2;
    longitude = (b1 - b2) * M_PI / 180.0;
    term2 = cos(a1) * cos(a2) * pow(sin(longitude), 2.0) / 2.0;
So, putting this together for:
Code:
d=2*R*sin^-1 (sin^2 (a1-a2)/2 + cos*a1*cos*a2*sin^2(b1-b2)/2)^1/2
CPP / C++ / C Code:
double d;
d = 2 * R * pow(asin(term1 + term2), 0.5);
...assuming I have read your notation correctly.
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
Starting in the business? Time to write a Business Plan ! WEBDOMAIN.com Web Hosting Forum 2 01-Aug-2006 05:12
which language ? onauc C++ Forum 2 19-Nov-2004 03:53
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 11:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:41.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.