在C 中,Harversine Forumla获得了错误的结果
Getting wrong results with Harversine Forumla in C++
我正在尝试使用Haversine公式为我的编程作业计算伦敦和曼彻斯特之间的距离。
给定公式是
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;
我的值为108.342而不是162.66。我在做什么错?
我认为您已经忽略了每条纬度/长时间的护理...
请在下面找到其他临时变量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;
}
相关文章:
- 我在贪吃蛇游戏中收到了错误代码 -1073741571
- C++ 犰狳库中的sort_index()函数给出了错误的结果
- 纹理单位重叠?渲染了错误的纹理
- 由于签名差异,调用了错误的子类函数
- srand() 输出了错误的值
- C++:使用类在向量中搜索特定元素时,我得到了错误的结果
- 我正在尝试传递DMA数组及其大小作为参数,但它给出了错误
- 运行时多态性 - 箭头运算符访问了错误的成员?
- IM 试图显示对象数组,但它给出了错误的输出
- 虽然循环即使应用了错误条件也不会退出
- C++ 将派生类的成员函数指针作为参数传递时选择了错误的模板专用化
- QScreen geometry()api为分辨率2736 x 1824和2560 x 1600提供了错误的值
- std::allocator_traits::construct调用了错误的构造函数
- 为什么我的代码返回了错误的平方值?C++
- 如何检查函数是否真的获得了定义为 const 的变量?
- 带有大结构变量的 CUDA 内核函数给出了错误的结果
- Visual Studio控制台调试了错误的文件
- 我的模板二叉搜索树类给出了错误"Node<T>: no appropriate default constructor available,"如何解决这个问题?
- 在C 中,Harversine Forumla获得了错误的结果
- C++从结构的数组中获得了错误的值