Python: Comparison of Equal Points Fails

Can anyone explain why the comparison of 2 equal points fails in the following code? If I create the point using Rhino.Input.Custom.GetPoint() rather than rg.Point3D, then it works just fine.

pStart = rg.Point3d(14634.5162752927, 10571.1069260966, 0.0)

print 'pStart type: ' + str(pStart.GetType())
print 'objC.PointAtStart type: ' + str(objC.PointAtStart.GetType())
print pStart
print objC.PointAtStart
print 'without str: ' + str(pStart == objC.PointAtStart)
print 'using Equals: ' + str(pStart.Equals(objC.PointAtStart))
print 'with str: ' + str(str(pStart) == str(objC.PointAtStart))

Result:
pStart type: Rhino.Geometry.Point3d
objC.PointAtStart type: Rhino.Geometry.Point3d
14634.5162752927,10571.1069260966,0
14634.5162752927,10571.1069260966,0
without str: False
using Equals: False
with str: True

Never use equals to compare points (or real numbers for that matter). You will run into “floating point fuzz” , somewhere out beyond 12 decimal places, one number will be different. Always use something like EpsilonEquals, or that the absolute value of one subtracted from the other is less than a given tolerance. For points you can use ptA.DistanceTo(ptB)<tolerance to compare.

The string comparison probably succeeds because at some point the numbers are truncated.

1 Like