How can I fix this C++ problem, probably easy for those of you with more than a years experience.

Mears

Platinum Member
Mar 9, 2000
2,095
1
81
Ok, can someone explain why this set of code is doing this. Here is an excerpt from a test driver that calls a distance between two points function:

(block 1) cout << distance(0,0,2,0) << endl << distance(1,sqrt(3),2,0) << endl
<< distance(1,sqrt(3),0,0);

(block 2) cout << endl << (distance(1,sqrt(3), 0, 0) == distance(2,0, 1, sqrt(3))) <<
endl << (distance(0, 0, 2, 0) == distance(1, sqrt(3),2, 0)) << endl
<< (distance(1,sqrt(3), 0, 0) == distance(0, 0, 2, 0));

When block one is executed it prints:
2
2
2

However when block 2 is executed it prints:
1
0
0

However the last two should be true (1). To see what was going on I insterted a cout << setprecision() above block 1 and gradually increased it. As soon as I set the precision to 17 the first block's output changed to:
2
1.9999999........
1.9999999........

Is there some way that I can inform my distance function to only check to the 16th decimal place or below so that I can get those three points to equal each other?
 

Nevyn522

Senior member
Aug 11, 2000
208
0
0
The moment I saw &quot;==&quot; I got suspicious. :)

You're right on the money with the problem being the rounding. The only way I can think of to do rounding is to actually compute it by hand... I don't know of any way to automate rounding via a library function like &quot;roundoff(float number, int num_digits)&quot; -- look around for a while if you want, but I'm pretty sure it can't be done like that.

So you're best of doing something like multiplying the number by 10000000000 and then casting to an integer or something to get rid of the decimals... Make sense?

HTH,
Andrew
 

Mears

Platinum Member
Mar 9, 2000
2,095
1
81
Well when the program is implemented, one of the things it is supposed to do is identify the type of triangle by using three double points that are inputted by a user. Since all equilateral triangles use square roots for at least one point, the program is going to have somehow consider the distances equal even if by it rounding they are 1 100 billionth of a decimal off.
 

Mears

Platinum Member
Mar 9, 2000
2,095
1
81
What if I changed block 2 so it checked if distance1/dist2 = 1 &amp; dist3/dist1 = 1?