Tolerance management on the component of a vector

Hello, I’m trying my hand at Selection filter, I copied a large part of the code from Mr. ChuongMep (thanks to him)

In the following gif you see that the filtering of the verticals poses a problem (I think it is linked to the tolerance on the vector component but I am not absolutely sure)
how do you handle this in python
question 17janv2024

import sys
import clr
clr.AddReference("RevitAPI")
clr.AddReference("RevitAPIUI")
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *
from Autodesk.Revit.UI.Selection import *
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager

uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
dir = IN[1]
if dir:
	cx=1
else:
	cx=0

#Thanks to Mr. Chuongmep for the following code
class MySelectionFilter(ISelectionFilter):
	def __init__(self):
		pass
	def AllowElement(self, element):
		if element.Location.Curve.Direction.X==cx or element.Location.Curve.Direction.X==-cx:
			return True
		else:
			return False

flag = IN[0]

if flag:
	selection_filter = MySelectionFilter()
	elt = uidoc.Selection.PickElementsByRectangle(selection_filter)

OUT = elt

thanks in advance
Sincerely
christian.stan

I managed to manage it like this, but I don’t know if the code is very elegant.

tol=0.0001

#Thanks to Mr. Chuongmep for the following code
class MySelectionFilter(ISelectionFilter):
	def __init__(self):
		pass
	def AllowElement(self, element):
		if cx-tol<element.Location.Curve.Direction.X<cx+tol or -cx-tol<element.Location.Curve.Direction.X<-cx+tol:
			return True
		else:
			return False

I leave it unsolved, if one of you finds a more effective solution.

Sincerely
christian.stan

Thanks to Mr. Sovitek (Thanks for the round) on another subject I found a solution by shooting tolerance.

#Thanks to Mr. Chuongmep for the following code and tip from Mr. Sovitek
class MySelectionFilter(ISelectionFilter):
	def __init__(self):
		pass
	def AllowElement(self, element):
		if abs(round(element.Location.Curve.Direction.X))==cx:
			return True
		else:
			return False

Sincerely
christian.stan

Hi,
you can also use this method