From this question that you also asked. Here's a piece of code that will check if your variable is in the array(unless that's not what you meant by compare the variable value with the array elements):
TOLERANCE=10**-6
def are_floats_equal(a,b):
return abs(a-b) <= TOLERANCE
def float_in_array(number, array):
return True in [are_floats_equal(number, a) for a in array]
Edit. This might be a bit more efficient to do this way(though less succinct) as we only loop over the array once:
def float_in_array(number, array):
for a in array:
if are_floats_equal(number, a):
return True
return False
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…