Reputation: 8099
Are there any python library functions that discriminate between regular floating point numbers, and all of the atypical ones, NaN, +/-Inf, denormal etc.?
Essentially, I'm asking the Python version of this question: check if floating point variable has 'normal' values
Upvotes: 4
Views: 2377
Reputation: 9572
To check if a float x
is finite, just use math.isfinite(x)
.
To check if a float x
is denormal (subnormal), here is a possible snippet:
import numpy
x = 1.0e-320
fmin_normalized = numpy.finfo(type(x)).tiny
x_is_subnormal = numpy.isfinite(x) and x!= 0 and abs(x) < fmin_normalized
print(x_is_subnormal)
I let you finish the work (write the appropriate lambda and filter the collection)
Upvotes: 7
Reputation: 199
EDIT: As pointed out, my previous reply didn't quite work and I'm unsure of a clean method of doing this. So I guess you would have to pick out the types you would like to remove like so:
test = [4, 4e99, 0b0101, 3.2, np.nan, 6, np.nan, np.inf]
result = []
for value in test:
if not np.isnan(value) and np.isfinite(value):
result.append(value)
print(result)
OUTPUT:
[4, 4e+99, 5, 3.2, 6]
Upvotes: 0