• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Python Negative Infinity – How to Use Negative Infinity in Python

Python Negative Infinity – How to Use Negative Infinity in Python

August 12, 2022 Leave a Comment

To use negative infinity in Python and create variables which represent negative infinity, there are four different ways.

The easiest way to get negative infinity in Python is with the float() function.

negative_infinity = -float('inf')

print(negative_infinity)

#Output:
-inf

Another way you can represent negative infinity in Python is with the math module.

import math

negative_infinity = -math.inf

print(negative_infinity)

#Output:
-inf

Another way you can represent negative infinity in Python is with the decimal module.

import decimal 

negative_infinity = -decimal.Decimal("inf")

print(negative_infinity)

#Output:
-inf

One final way to represent negative infinity is with the numpy module.

import numpy as np

negative_infinity = -np.inf

print(negative_infinity)

#Output:
-inf

Each of these methods create variables which are equal to negative infinity and they are all have the same representations in Python.

import math
import decimal
import numpy as np

print(-float('inf') == -math.inf == -decimal.Decimal('inf') == -np.inf)

#Output:
True 

How to Use Negative Infinity in Python

After you’ve initialized a variable and assigned it a value of negative infinity with one of the four methods shown above, you can use negative infinity just like any other number in Python.

For example, you can perform any of the basic arithmetic operations such as addition, subtraction, division or multiplication.

You can also do things such as comparing negative infinity with other numbers or finding the maximum or minimum of numbers including infinity.

negative_infinity = -float('inf')

print(negative_infinity + 100)
print(negative_infinity - 100)
print(negative_infinity * 100)
print(100 / negative_infinity)
print(max(negative_infinity, 100))
print(min(negative_infinity, 100))

#Output:
-inf
-inf
-inf
-0.0
100
-inf

Using Positive Infinity in Python

If you want to use infinity in Python, then don’t multiply by -1 and you will get positive infinity.

For example, if you want to use the float() function to create infinity, then don’t multiply by -1.

Below shows a simple example of how you can get positive infinity in Python.

infinity = float('inf')

print(infinity)

#Output:
inf

Then after you’ve created a variable representing infinity, you can use it just like negative infinity.

Hopefully this article has helped you understand how to create variables representing negative infinity in Python.

Other Articles You'll Also Like:

  • 1.  Python Square Root – Finding Square Roots Using math.sqrt() Function
  • 2.  How to Hide Turtle in Python with hideturtle() Function
  • 3.  Using Python to Sum the Digits of a Number
  • 4.  Remove None From List Using Python
  • 5.  Get Last Day of Month Using Python
  • 6.  Get Day of Week from Datetime in pandas DataFrame
  • 7.  Add Minutes to Datetime Variable Using Python timedelta() Function
  • 8.  pandas Duplicated – Find Duplicate Rows in DataFrame or Series
  • 9.  Python Add Days to Date Using datetime timedelta() Function
  • 10.  pandas percentile – Calculate Percentiles of Series or Columns in DataFrame

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy