Haversine Formula- trig variable error

I am getting the following error when compiling the code below:

(111): error C2296: '^' : illegal, left operand has type 'double'
(111): error C2297: '^' : illegal, right operand has type 'double'
(112): error C2144: syntax error : 'double' should be preceded by ';'

The lines are the two using trig functions. I looked at the sin function in the Reference pages and tried using "double sin" but that didn't work. I am using MS Visual Studio Express 2010. Are my variables wrong or is my syntax wrong?

Thanks.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
double GetDistance(double dLat1, double dLong1, double dLat0, double dLong0)			
{
	double Radius=6371;
	double RadLat0=dLat0*(PI/180);
	double RadLong0=dLong0*(PI/180);
	double RadLat1=dLat1*(PI/180);
	double RadLong1=dLong1*(PI/180);
	double deltaLat=RadLat1-RadLat0;
	double deltaLong=RadLong1-RadLong0;
	double a=(sin(deltaLat/2))^2 + cos(RadLat0)*cos(RadLat1)*(sin(deltaLong/2))^2
	double c=2*atan2(sqrt(a),sqrt(1-a));
	double d=Radius*c;
	nDistance=d;
	return (nDistance);
}
If I remember correctly C++ doesn't support using '^' for raising numbers to a power, it is used for bitwise calculations. Try using the pow function instead (inside of cmath)

pow( (sin(deltaLat/2)), 2 )
Assuming you actually meant them to be raised to a power?

The 3rd error is just you forgot a semi colon at the end of the statement.
Yes, that makes sense. It worked. Thanks again.
Topic archived. No new replies allowed.