Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Get the Machine limits information for float types in Python
To get the machine limits information for float types, use the numpy.finfo() method in Python NumPy. The first parameter is the floating type i.e. the kind of float data type to get information about.
Syntax
numpy.finfo(dtype)
Where dtype is the floating-point data type such as float16, float32, or float64.
Getting Float16 Limits
Check the machine limits for 16-bit floating-point numbers ?
import numpy as np
# Get machine limits for float16
a = np.finfo(np.float16)
print("Minimum of float16 type...")
print(a.min)
print("Maximum of float16 type...")
print(a.max)
Minimum of float16 type... -65500.0 Maximum of float16 type... 65500.0
Getting Float32 Limits
Check the machine limits for 32-bit floating-point numbers ?
import numpy as np
# Get machine limits for float32
b = np.finfo(np.float32)
print("Minimum of float32 type...")
print(b.min)
print("Maximum of float32 type...")
print(b.max)
Minimum of float32 type... -3.4028235e+38 Maximum of float32 type... 3.4028235e+38
Getting Float64 Limits
Check the machine limits for 64-bit floating-point numbers ?
import numpy as np
# Get machine limits for float64
c = np.finfo(np.float64)
print("Minimum of float64 type...")
print(c.min)
print("Maximum of float64 type...")
print(c.max)
Minimum of float64 type... -1.7976931348623157e+308 Maximum of float64 type... 1.7976931348623157e+308
Complete Example
Here's a complete example showing all float types together ?
import numpy as np
# Get machine limits for different float types
float_types = [np.float16, np.float32, np.float64]
for dtype in float_types:
info = np.finfo(dtype)
print(f"\n{dtype.__name__} limits:")
print(f"Minimum: {info.min}")
print(f"Maximum: {info.max}")
print(f"Precision: {info.precision} digits")
float16 limits: Minimum: -65500.0 Maximum: 65500.0 Precision: 3 digits float32 limits: Minimum: -3.4028235e+38 Maximum: 3.4028235e+38 Precision: 6 digits float64 limits: Minimum: -1.7976931348623157e+308 Maximum: 1.7976931348623157e+308 Precision: 15 digits
Additional Properties
The finfo object provides more than just min and max values ?
import numpy as np
info = np.finfo(np.float64)
print("Float64 machine limits:")
print(f"Minimum value: {info.min}")
print(f"Maximum value: {info.max}")
print(f"Machine epsilon: {info.eps}")
print(f"Precision: {info.precision}")
print(f"Smallest positive number: {info.tiny}")
Float64 machine limits: Minimum value: -1.7976931348623157e+308 Maximum value: 1.7976931348623157e+308 Machine epsilon: 2.220446049250313e-16 Precision: 15 Smallest positive number: 2.2250738585072014e-308
Conclusion
Use numpy.finfo() to get machine limits for floating-point types. It provides min/max values, precision, and epsilon information crucial for numerical computing.
