Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if floating point number is normal in Python

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

like image 438
Dave Avatar asked Oct 30 '25 16:10

Dave


1 Answers

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)

like image 134
aka.nice Avatar answered Nov 02 '25 07:11

aka.nice



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!