Useful Things About Numpy Infinity in Python

We are all familiar with the word infinity. And we know that infinity is a nonfinite number in mathematics. But do you know we can use infinity in python? If you know about it. It is perfect. Suppose you don’t have any knowledge about it. Don’t worry; this article will help you learn about it. Now we are going to see how to use NumPy infinity in python. We can also use the math function for infinity.

Numpy infinity is an infinite number. It may be either positive or negative values. In the digital world, infinity is useful to measure performance and algorithms. This performs computations on large-scale applications. In NumPy, we have np.inf and -np.inf. np.inf is for positive infinity, and -np.inf is for negative infinity. 

Representing Infinite Values

import numpy as np
def inf(num):
    if num==np.inf:
        print("Positive Infinity:",num)
    elif num==-np.inf:
        print("Negative Infinity:",num)
    return num
inf(np.inf)
inf(-np.inf)

This code is to represent a positive infinity and negative infinity in a numpy library.

Import a numpy module. Create a function named inf. If the input value is np.inf, it will return positive infinity. And -np.inf is negative infinity.

Output

Positive Infinity: inf
Negative Infinity: -inf

What is mean by numpy isinf?

The isinf is a function that is useful to check if the given is positive infinity, negative infinity, or not an infinity.

Syntax

numpy.isinf(sequence)

Parameter

sequence: It may be an element or array.

Return

bool value

To check if the number is infinite or not

Example 1

import numpy as np
x=np.inf
y=100
z=-np.inf
print(np.isinf(x))
print(np.isinf(y))
print(np.isinf(z))

Import a numpy module. Assigning values for x, y, and z. If the given value is infinite, it will return True. Otherwise, it returns False.

Output

True
False
True

Example 2: Check using nan

>>> import numpy as np
>>> np.isinf(np.nan)

Now using nan(not a number) to check whether it is an infinite value or not.

Output

False

Example 3: Check using an array

import numpy as np
x=[1,2,4,5]
print(np.isinf(x))

This code is useful to implement an array to isinf function.

Output

[False False False False]

What is mean by numpy isposinf?

It checks whether the element is positive infinity or not.

Syntax

isposinf(sequence)

Parameter

sequence: It may be an element or array.

Return

bool value

Code

import numpy as np
x=[1,2,4,5]
y=np.inf
z=-np.inf
print(np.isposinf(x))
print(np.isposinf(y))
print(np.isposinf(z))

So far, we have learned about isinf. It will return True for both positive infinity and negative infinity value. But isposinf returns True only for the positive infinity.

Output

[False False False False]
True
False

What is mean by numpy isneginf?

It checks whether the element is negative infinity or not.

Syntax

isneginf(sequence)

Parameter

sequence: It may be an element or array.

Code

import numpy as np
x=[1,2,4,5]
y=np.inf
z=-np.inf
print(np.isneginf(x))
print(np.isneginf(y))
print(np.isneginf(z))

This code returns True only for the negative infinity.

Output

[False False False False]
False
True

Other names for inf

  • Inf
  • Infinity
  • PINF
  • infty

numpy.inf and float(‘inf’) are interchangeable

There is no more difference between np.inf and float.inf or math.inf. They all are interchangeable.

Example

import numpy as np
import math
print(np.inf==float('inf'))
print(np.inf==math.inf)

Output

True
True

You can understand from the above example that np.inf, float(‘inf’), math.inf is the same.

Size of numpy.inf

import sys
print(sys.getsizeof(np.inf))

Output

24

More to know: Compare finite numbers and infinite

import numpy as np
x=input("Enter a value:")
y=input("Enter a value:")
def compare(a,b):
    if a>b:
        print(f'{x} is greater than {y}')
    else:
        print(f'{y} is greater than {x}')
compare(x,y)

Import numpy module. Get the values of x and y from the user. Create a function named compare. Use decision-making statements to check the conditions. It is better to use formatted functions while displaying results. So that user can easily understand the result, next to comparing x and y values.

Output

Enter a value:np.inf
Enter a value:1000
np.inf is greater than 1000

Why numpy.inf is better than float(‘inf’)?

Numpy.inf is more better than float(‘inf’). Because creating a variable in numpy.inf is faster than float(‘inf’). np.inf takes 37.9 ns ± 0.69 ns per loop. On the other hand float(‘inf’) takes 232 ns ± 13.9 ns per loop.

Also, NumPy defines many alias names for inf. So we can easily remember it.

So numpy.inf is better than float(‘inf’).

How to replace numpy infinity values from array?

import numpy as np
x=[np.inf,3,9]
x[x==np.inf]=5
print(x)

This code is to replace the infinity value with 5.

Output

[5, 3, 9]

1. What is the floating-point representation of positive infinity?

The floating-point representation of positive infinity is IEEE 754.

Conclusion

Now we have thoroughly learned about NumPy infinity in python. This article will give you a clear idea about NumPy infinity. We hope this article is easy to understand. In case of any queries, feel free to ask in the comments. We will be here to help you. Learn with us. Try to solve the problems on your own.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments