Point tolerance

Hi there,

I am having an issue that I did not realize before that Rhino has a point tolerance. It concluded that points;

-5.26930645920454,6.6957560903893,0 and
-5.26930645920482,6.6957560903894,0

are not the same. The first coordinate came from a CurveCurveIntersection and the second came from the end point of the curve. Basically I try to compare the end of the curve (that made the instersection with the curve) and the intersection. Is it possible that I got this result?

Keep in mind that floating points in any programming environment are an approximation with limited precision. See Also see the floating point comparison guide.

Also keep in mind that intersection operations for curves are also an approximation. (Find an intersection point with less tolerance than x)

Therefore: comparing two points are exactly the same (==) will almost never work. You should check if the distance between these two points is less than the document tolerance to conclude if they are the same point.

Not sure about python, but in C# it would be

bool isSamePoint = ptA.DistanceTo(ptB) < RhinoDoc.ActiveDoc.ModelAbsoluteTolerance
1 Like

this is the way to go:
https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_Point3d_EpsilonEquals.htm

2 Likes

Hi @onrender,

You almost never want to write code like the following:

double x; 
double y; 
... 
if (x == y) 
{
  ...
}

Most floating point operations involve at least a tiny loss of precision and so even if two numbers are equal for all practical purposes, they may not be exactly equal down to the last bit, and so the equality test is likely to fail.

For example, the following code snippet prints -1.77635683940025E-15 . Although in theory, squaring should undo a square root, the round-trip operation is slightly inaccurate.

var x = 10.0;
var y = System.Math.Sqrt(x);
y *= y;
if (x == y)
  RhinoApp.WriteLine("Square root is exact");
else
  RhinoApp.WriteLine($"{{0}}", x-y);

In most cases, the equality test above should be written as something like the following:

double tolerance = ... 
if (System.Math.Abs(x - y) < tolerance) 
{
  ...
}

Here tolerance is some threshold that defines what is “close enough” for equality. This begs the question of how close is close enough. This cannot be answered in the abstract; you have to know something about your particular problem to know how close is close enough in your context.

Hope this helps.

– Dale

2 Likes

Thanks for everyone! It was a great help.

As a small addition: Rhino contains quite some tools to help deal with tolerance.

RhinoDoc (The document)
Has the following properties, where you can define what’s good enough for the model.

RhinoMath also has some predefined fields:

  • ZeroTolerance In cases when an absolute “zero” tolerance is required to compare model space coordinates.
  • EpsilonEquals() Are two numbers the same within tolerance?
1 Like