I am looking for an easy way to check float ranges in Python, where the minimum and maximum borders can be null.
This code:
tval = float(-b - discriminant) / float (2*a)
if tval >= tmin and tval <= tmax:
return tval
tval = float(-b + discriminant) / float (2*a)
if tval >= tmin and tval <= tmax:
return tval
return None
However, this does not completely allow us to handle the case when tmin or tmax is None (which should be interpreted as meaning that there is no minimum or maximum value).
So far, the best I've been able to come up with is:
tval = float(-b - discriminant) / float (2*a)
if (tmin == None or tval >= tmin) and (tmax == None or tval <= tmax):
return tval
tval = float(-b + discriminant) / float (2*a)
if (tmin == None or tval >= tmin) and (tmax == None or tval <= tmax):
return tval
return None
I keep thinking that there should be a better (cleaner, more readable) way to write this. Any ideas?
source
share