C++ 在C+中使用Harversine Forumla获得错误结果+;

C++ 在C+中使用Harversine Forumla获得错误结果+;,c++,function,C++,Function,我正在用哈弗森公式计算伦敦和曼彻斯特之间的距离,这是我的编程作业 给出的公式是 To calculate the distance between two places knowing their latitudes lat1 and lat2 and longitudes long1 and long2: Convert all latitudes and longitudes from degrees to radians, then: dLat = lat2 - lat1 dLong =

我正在用哈弗森公式计算伦敦和曼彻斯特之间的距离,这是我的编程作业

给出的公式是

To calculate the distance between two places knowing their latitudes lat1 and 
lat2 and longitudes long1 and long2:
Convert all latitudes and longitudes from degrees to radians, then:
dLat = lat2 - lat1
dLong = long2 - long1
a = sin^2(dLat/2) + cos(lat1)*cos(lat2)*sin^2(dLong/2)
c = 2 * atan2(sqrt(a), sqrt(1-a))
distance = R * c
where R is the radius of the Earth, 3958.75 miles.
Check: you should find the distance between Manchester and London to be 162.66 miles.
我的代码是

double hav_formula(double lat1, double long1, double lat2, double long2){
    // Haversine Formula
    // Calculates distance between two places
    double dlat, dlong, a, c, distance;
    dlat = (lat2 - lat1) / 180 * PI;
    dlong = (long2 - long1) / 180 * PI;
    a = pow(sin(dlat/2),2) + (cos(lat1) * cos(lat2) * pow(sin(dlong/2),2));
    c = 2. * atan2(sqrt(a),sqrt(1-a));
    distance = 3958.75 * c;
    return distance;
}

    double man_lon_distance;
    man_lon_distance = hav_formula(53.48095,-2.23743,51.50853,-0.12574);
    cout<<man_lon_distance<<endl;
double hav_公式(双lat1,双long1,双lat2,双long2){
//哈弗森公式
//计算两个位置之间的距离
双dlat、dlong、a、c、距离;
dlat=(lat2-lat1)/180*PI;
dlong=(long2-long1)/180*PI;
a=功率(sin(dlat/2),2)+(cos(lat1)*cos(lat2)*功率(sin(dlong/2),2));
c=2.*atan2(sqrt(a),sqrt(1-a));
距离=3958.75*c;
返回距离;
}
双人长距离;
man_-lon_距离=hav_公式(53.48095,-2.23743,51.50853,-0.12574);

我想你忘了把每一个拉特/拉特护理到弧度

请在下面找到其他温度变量
radLat1/2
,加上我在您的计算中添加了括号,这很容易被误解,您会得到意外的结果

double hav_formula(double lat1, double long1, double lat2, double long2){
    // Haversine Formula
    // Calculates distance between two places
    double dlat, dlong, a, c, distance;
    double radLat1, radLat2;
    dlat = ((lat2 - lat1) / 180) * PI;
    dlong = ((long2 - long1) / 180) * PI;
    radLat1 = (lat1 / 180) * PI;
    radLat2 = (lat2 / 180) * PI;
    a = pow(sin(dlat/2),2) + (cos(radLat1) * cos(radLat2) * pow(sin(dlong/2),2));
    c = 2. * atan2(sqrt(a),sqrt(1-a));
    distance = 3958.75 * c;
    return distance;
}

你在混合度和弧度吗?我已经把度转换成弧度了。你有没有仔细检查你的弧度转换?我担心你没有在…/180*PI;我看不出你在哪里把lat1转换成弧度…或者lat2Oh。这是我犯的一个可怕的错误。非常感谢你指出这一点。