Find Maximum Float Value in Python

Namita Chaudhary Jan 30, 2023 May 27, 2022
  1. Find Maximum Float Value in Python Using sys.float_info
  2. Find Maximum Float Value in Python Using the finfo() Function of the Numpy Library
  3. Conclusion
Find Maximum Float Value in Python

Python supports many data types such as integer, float, string, etc. All of them have been assigned a minimum and a maximum value between which the compiler can accept the values.

This article will discuss how to find out the maximum value that the float data type supports.

There are mainly two ways to find the maximum value of float data type in Python. One is the use of sys.float_info that holds the information about the float data type in Python, and the second is to use the finfo() function of the numpy library to return machine limits of the floating-point numbers.

We will see each one of them in detail in this article.

Find Maximum Float Value in Python Using sys.float_info

The sys.float_info is a named tuple that holds information such as the precision and internal representation of the float data type.

To use sys.float_info in our code, we must first import the in-built sys module defined in Python, which provides certain variables and functions used to manipulate various parts of the Python runtime environment.

However, the sys.float_info will give us all the information about the float data type. It is demonstrated using the code below.

import sys
sys.float_info

Output:

sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)

However, we need to find the maximum value of the float data type. Therefore, we will use sys.float_info.max, which will only provide the maximum float data type value in Python.

Let us see the code to demonstrate the use of sys.float_info.max.

import sys
sys.float_info.max

Output:

1.7976931348623157e+308

Find Maximum Float Value in Python Using the finfo() Function of the Numpy Library

If you are using the numpy library of Python, you can easily find the maximum value of the float data type. It contains a function called finfo(), which takes float, dtype or instance as an argument and returns the machine limit for the floating-point types.

However, before using the finfo() function, you first need to import the numpy library in the code. After which, you need to call the finfo function with np.float64 as the argument where np is the alias name used for the numpy library.

Let us see how to code the same.

import numpy as np
np.finfo(np.float64)

Output:

finfo(resolution=1e-15, min=-1.7976931348623157e+308, max=1.7976931348623157e+308, dtype=float64)

However, the above function gives the minimum machine limit for the float data type. You can use np.finfo(np.float64).max for just printing the maximum value of the float data type in Python.

Let us see the code for finding the maximum value of the float data type.

import numpy as np
np.finfo(np.float64).max

Output:

1.7976931348623157e+308

Conclusion

In this article, we have seen two different methods to find the maximum value of the float data type in Python. One of them is to use the float_info.max of the sys module, and the second method is to use the finfo() function of the numpy library in Python.

Both the methods explained above are pretty efficient and can be used to serve our purpose in no time.

Related Article - Python Float