(original) (raw)
Here's an excerpted (and slightly simplified for consumption here) usage of float.is\_integer() from the top of a function which does some convolution/filtering in a geophysics application. I've mostly seen it used in guard clauses in this way to reject either illegal numeric arguments directly, or particular combinations of arguments as in this case:
def filter\_convolve(x, y, xf, yf, stride=1, padding=1):
x\_out = (x - xf + 2\*padding) / stride + 1
y\_out = (y - yf + 2\*padding) / stride + 1
if not (x\_out.is\_integer() and y\_out.is\_integer()):
raise ValueError("Invalid convolution filter\_convolve({x}, {y}, {xf}, {yf}, {stride}, {padding})"
.format(x=x, y=y, xf=xf, yf=yf, stride=stride, padding=padding))
x\_out = int(x\_out)
y\_out = int(y\_out)
# ...
Of course, there are other ways to do this check, but the approach here is obvious and easy to comprehend.