Loss of precision of the result when using sqrt function

I am trying to write a program that calculates the distance between two points of which x and y coordinates are entered by the user.

Coordinates are assigned to variables defined as double.

Program uses pythagorean theorem to calculate the distance and I can see that the sqrt function that I use by including <cmath> header file rounds up the result which I thing it should not.

Is there anything that I should to additional to increase the accuracy of the calculation executed by the sqrt function -or pow () function that I have already tried instead?-

Thank you,
Fatih
Without seeing the actual code, it hard to say.

Please post your code using code tags (the <> formatting button).
Here is my code.

I do run Visual Studio on an MBP running under Bootcamp and in fact getting the "Error 1 error C2666: 'pow' : 6 overloads have similar conversions" error message.

Code yields 8.76413 whereas the result must be as accurate as 8.764131445842194....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
	float x1, x2, y1, y2, distance;
	
	// cout << "here";
	
	cout << "Enter x1 and y1 ";
	cin >> x1 >> y1;

	cout << "Enter x2 and y2 ";
	cin >> x2 >> y2;

	distance = pow(pow(x2 - x1, 2.) + pow(y2 - y1, 2.),.5);
	
	cout << "Distance between the points is " << distance << endl;

	return 0;
}



Thanks, Fatih
closed account (3qX21hU5)
First off remember that float has LESS precision then double. Double gives you 16 significant figures while floats give 7 significant figures I believe. So when you are looking for high precision (Money calculations and other things) always use double. If you need even more precision then double can give use long double

If I am remembering right I believe doubles give 53 significant bits and floats always have 24 significant bits.

As for sqrt I can't really comment since I don't have much experience with it.

Hope this helps a bit.
Last edited on
The most important thing is to use type double (or even long double) rather than float.

The other thing, if you want to actually display the full result, you will need to change the cout precision. Use something like cout.precision(15); or cout << setprecision(15); in order to show the output to 15 digits of precision for example.

You could try different methods of calculation, but in practice the results will be virtually identical in most cases:
distance = pow(pow(x2 - x1, 2.) + pow(y2 - y1, 2.),.5);

Personally I'd prefer something like this, as it requires just a single function call, and it may be more accurate:
distance = sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
Last edited on
I have re-written my code as follows and getting the same loss of precision.
I am starting to think that this has got to do something with my running Visual Studio on an MBP under bootcamp.

Your help will be most appreciated.

Thanks,
Fatih

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
	long double x1, x2, y1, y2, distance;
	
	cout << "Enter x1 and y1 ";
	cin >> x1 >> y1;

	cout << "Enter x2 and y2 ";
	cin >> x2 >> y2;

	distance = sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
	
	cout << "Distance between the points is " << distance << endl;

	return 0;
}
I added setprecision(15) as Chervil suggested as and I'm getting 12 positions to the right of the decimal point using VS2010.
 
cout << "Distance between the points is " << setprecision(15) << distance << endl;

How much precision did you want?
@ fcan1968 Are you sure there is any loss of precision? The code most recently posted uses the default settings of the output stream cout. That will usually show about 6 significant digits.

If you want the to display more digits, you need to do either
 
    cout.precision(15);

or
 
    cout << setprecision(15);


If you are using long double, you may try to display about 17 or 18 digits, usually. (you may be able to display more digits, but beyond that they may not be accurate).
Chervil, AbstractionAnnon,

Resolved with your help.
Thank you very much.
Fatih
Topic archived. No new replies allowed.