c ++中acos函数的精度 [英] Precision of acos function in c++

查看:41
本文介绍了c ++中acos函数的精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算两点之间的距离并在过程中使用 acos() 函数......但我没有得到精确的结果......如果距离很小

i am trying to calculate the distance between two points and using the acos() function in process...but i am not getting a precise result..in case the distance is small

float distance_between(dest& point1,dest point2) {
float EARTH_RADIUS = 6371.0;//in km
float point1_lat_in_radians  =  point1.lat*(PI/180);
float point2_lat_in_radians  = point2.lat*(PI/180);
float point1_long_in_radians  = point1.lon*(PI/180);
float point2_long_in_radians  = point2.lon*(PI/180);

float res =  acos( sin( point1_lat_in_radians ) * sin( point2_lat_in_radians ) + cos( point1_lat_in_radians ) * cos( point2_lat_in_radians ) * cos( point2_long_in_radians - point1_long_in_radians) ) * EARTH_RADIUS;
cout<<res<<endl;
res = round(res*100)/100;
return res; 
}

我正在检查以下坐标之间的距离52.378281 4.900070 和 52.379141 4.880590
52.373634 4.890289 和 52.379141 4.880590两种情况下的结果都是 0..我知道距离很小但是有没有办法获得像 0.xxx 这样的精确距离?

i am checking the distance between the following co-ordinates 52.378281 4.900070 and 52.379141 4.880590
52.373634 4.890289 and 52.379141 4.880590 the result is 0 in both cases..i know the distance is small but is there a way to get precise distance like 0.xxx?

推荐答案

使用 double 而不是 float 以获得更高的精度.

Use double instead of float to get more precision.

这样你就可以使用这个原型了:

That way you are going to use this prototype:

double acos (double x);

必须阅读的是float 和double 之间的区别问题.从那里我们有:

A must read is the Difference between float and double question. From there we have:

  1. 顾名思义,double 的精度是 float 的 2 倍.
  2. C 和 C++ 标准没有指定浮点数的表示,双和长双.有可能所有三个都实现为IEEE 双精度.尽管如此,对于大多数架构(gcc、MSVC;x86, x64, ARM) float 确实是 IEEE 单精度浮点数 (binary32),double 是 IEEE双精度浮点数 (binary64).

这篇关于c ++中acos函数的精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆