swarnava112
swarnava112

Reputation: 431

Precision of acos function in c++

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; 
}

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?

Upvotes: 2

Views: 1780

Answers (1)

gsamaras
gsamaras

Reputation: 73434

Use double instead of float to get more precision.

That way you are going to use this prototype:

double acos (double x);


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

  1. As the name implies, a double has 2x the precision of float.
  2. The C and C++ standards do not specify the representation of float, double and long double. It is possible that all three implemented as IEEE double-precision. Nevertheless, for most architectures (gcc, MSVC; x86, x64, ARM) float is indeed a IEEE single-precision floating point number (binary32), and double is a IEEE double-precision floating point number (binary64).

Upvotes: 3

Related Questions