Overview:
-
Negative infinity is less than any real number. Subtracting any known smallest real number will still result in negative infinity.
-
The function numpy.isneginf() checks the elements of a numpy array-like for negative infinity. If an element evaluates to negative infinity than the result is True. The result is False otherwise. When the input is a scalar the result is a single Boolean value representing True or False.
-
The numpy function isinf() returns True for both positive infinity and negative infinity (excluding Not a Number) elements of a NumPy array-like. To check whether the elements or a scalar is a real number the NumPy function isreal() can be used.
Example 1:
# Example Python program that checks numbers # Check positive infinity for negative infinity # Create negative infinity # Check negative infinity for negative infinity |
Output:
False -inf True |
Example 2:
# Example Python program that checks each element for # Create an NumPy ndarray # Populate the ndarray with literals initialized to print("Array contents:") print("Results of numpy.isneginf():") # Check complex infinity |
Output:
Array contents: [[ inf -inf -1.e+20] [ -inf -inf -inf]] <class 'numpy.float64'> <class 'numpy.float64'> Results of numpy.isneginf(): [[False True False] [ True True True]] (nan-infj) True
Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/numpy/lib/_ufunclike_impl.py", line 200, in isneginf signbit = nx.signbit(x) ^^^^^^^^^^^^^ . .
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/numpy/lib/_ufunclike_impl.py", line 203, in isneginf raise TypeError(f'This operation is not supported for {dtype} values ' TypeError: This operation is not supported for complex128 values because it would be ambiguous. |