# Need help writing a formula in c++

## Recommended Posts

Hi!

First I know this is autoit forum but I've seen many users that are very skilled c++ programmers and also this is the best forum community I've ever seen so I think this the best place to find some help.

To the problem. I'm wirting an application in c++ that calculates distance between cities. The coordinates are given in degrees with latiudes and longitudes. I'm using this formula to calculate the distance.

d=2 R sin-1 (sin2 (a1-a2)/2 + cos a1 cos a2 sin2 (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 the functions sin and cos work with radians.

I've been working on this for two days and I'm stuck. I can't write this formula correctly in c++.

Here is what I've done

```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

`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 those lines don't give accurate results.

Can you tell me what I'm doing wrong?

p.s. Sorry for my English. I'm still learning it

Edited by muhmuuh

I ran. I ran until my muscles burned and my veins pumped battery acid. Then I ran some more.

##### Share on other sites

Hi!

Here's how you should do it:

```#define PI 3.14159

{
return 180.0/(PI*value);
}```

And remember, x^(1/2)=Sqrt(x), I think it is more readable that way

Broken link? PM me and I'll send you the file!

##### Share on other sites

Hi!

Here's how you should do it:

```#define PI 3.14159

{
return 180.0/(PI*value);
}```

And remember, x^(1/2)=Sqrt(x), I think it is more readable that way

one degree is equivalent to π/180 radians

so x degrees should be equal to x*Pi/180 radians

right?

I ran. I ran until my muscles burned and my veins pumped battery acid. Then I ran some more.

##### Share on other sites

so x degrees should be equal to x*Pi/180 radians

right?

Nope.

The radian is a unit of plane angle, equal to 180/π degrees, or about 57.2958 degrees.

Edited by monoceres

Broken link? PM me and I'll send you the file!

##### Share on other sites

I still think I'm right

The quote from wikipedia you posted shows how to convert radians to degrees.

I think I now how I'll prove I'm right.

Call you function with 1 degree. It will return 57.2958 radians so you say that 1degree=572958 radians which is not corret.

But if you call my function with 57.2958 it will return 1 radian so 57.2958 degrees = 1 radian which is correct

p.s.

check after "Conversely, to convert from degrees to radians, multiply by π/180. For example,"

Edited by muhmuuh

I ran. I ran until my muscles burned and my veins pumped battery acid. Then I ran some more.

##### Share on other sites

I still think I'm right

The quote from wikipedia you posted shows how to convert radians to degrees.

I think I now how I'll prove I'm right.

Call you function with 1 degree. It will return 57.2958 radians so you say that 1degree=572958 radians which is not corret.

But if you call my function with 57.2958 it will return 1 radian so 57.2958 degrees = 1 radian which is correct

p.s.

check after "Conversely, to convert from degrees to radians, multiply by π/180. For example,"

Ok you're right.

Here's my attempt btw:

```#include <iostream>
#include <cmath>

#define PI 3.14159

using namespace std;

double CalculateDistance(double,double,double,double,double);
int main ()
{
double d;
d=CalculateDistance(10,10,20,20,10000);
cout << d;
cin.get();
return 0;
}

double CalculateDistance(double a1,double a2,double b1,double b2,double radius)
{
double dist;
return dist;
}

{
return value*(PI/180);
}```

The weird thing is that it gives #INF when giving same coords.

When changing the -1 at the end to a 1 it gives zero so that could be right..

Edited by monoceres

Broken link? PM me and I'll send you the file!

##### Share on other sites

sin-1(x) is arcsine... It's not the multiplicative inverse of sin(x). That's asin(x) in C++, not pow(sin(x), -1) or 1/sin(x). Assuming your formula is correct, the rough translation would be...

2*R*sqrt(asin( pow(sin(a1-a2), 2)/2 + cos(a1)*cos(a2)*pow(sin(b1-b2), 2)/2 ))

Thats after you change from degrees into radians for the input. Which reminds me: you might want to take a bit of effort to check to make sure inputs are within the domains of the respective functions before calling...

asin(x), -1 <= x <= 1

sqrt(x), 0 <= x

@monoceres: Well, if x^1 = 0, then x = 0, in which case x^(-1) = 1/0 = undefined, so it wouldn't really be all that weird, given that for the same point, sin(x-x) = sin(y-y) = sin(0) = 0, which would zero out the first parameter of your your pow(x, -1) call.

Edited by -Ultima-

[ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ]

##### Share on other sites

Hi!

I got it working

Here is the line:

d=2*R*asin(sqrt(sin((a1-a2)/2.0)*sin((a1-a2)/2.0)+cos(a1)*cos(a2)*sin((b1-b2)/2.0)*sin((b1-b2)/2.0)));

@Ultima

It is absolutely the same as yours after the edit

Thank you very much for the help

I ran. I ran until my muscles burned and my veins pumped battery acid. Then I ran some more.

##### Share on other sites

Hi Muhmuuh,

You did a webmail cleaner app for me once morevthan few years ago (I attach screenshot of what it was), need you for quote on new little thing, and.....

......possibly bigger C programming  project later on, which we can also discuss.

I need minor sorting and the difference computed of a 2 column tsv file and stop at the end, it would be similar small size project as that webmail cleaner you did for me.

Hope you can help by getting in contact with me soon, it's important thing, thx, Jerry

batchj2011 at gmail dot com

Only way I found you is by your ID on lower right corner of face of that software and googling this ID of yours.

Thx, Truly, Jerry

## Create an account

Register a new account

×

• Wiki

• Back

• Git