Dear friends:
I am working on a C++ computational geometry code, and i want to make sure whether two points are the same by comparing their x y and z coordinates. The coordinates of the points are in the double formate.
since the pc can not represents the double number exactly in the memory, and i have a large number of points in a small region, i think it is inaccurate to do the comparison by a simple "==".
Could you please give me some advices on that?
Regards
Your Sincerely.
ztdep

You can use range

boot equals (double n1, double n2) {
if (abs((n1 - n2)) < 0.001)
return true;

else {
return false;
}}

Something like this. I wrote this in 2 minutes so you may wont to modify this a little to suit your needs better, but you get the basic idea.

Floating point numbers only support limited precision. For both float and double, they have their limit in holding some digits of precisions. You may use String type to store it just like every big number computation used to do. Or the algorithm of divide and conquer for it's computation.

I don't really good in this thing, and right know, I'll just head up your question.

bool isSame(double a, double b)
{
    return std::fabs(a - b) < std::numeric_limits<double>::epsilon();
}

I think that before you suggest solutions to a problem, it is important to understand exactly what problem you are trying to solve.

When you say that you "want to make sure whether two points are the same," exactly what do you mean? In other words, will you ever want to treat two points that are very close together as being "the same?" If so, how close together?

Perhaps even more important is understanding why. In other words, if you want to know whether two points are the same, why are you ever willing to accept two points as being "the same" if they are merely close together? Note that if point A is close to point B, and point B is close to point C, that does not imply that point A is necessarily clowe to point C. Is that fact acceptable? Why or why not?

Until you understand the answers to these questions, you will have a hard time writing code to check whether two points are "the same" that will actually work.

bool isSame(double a, double b)
{
    return std::fabs(a - b) < std::numeric_limits<double>::epsilon();
}

Thank you very much for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.