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 number of bits in the exponent portion of the floating point representation in Python
To get the number of bits in the exponent portion of the floating point representation, use the iexp attribute of the numpy.finfo() method in Python NumPy. The first parameter is the float data type to get information about.
What is numpy.finfo()?
The numpy.finfo() function provides machine limits for floating-point types. The iexp attribute specifically returns the number of bits used for the exponent in the IEEE 754 floating-point representation ?
Syntax
numpy.finfo(dtype).iexp
Float16 Type
Checking for float16 type. The iexp gets the number of bits in the exponent portion ?
import numpy as np
a = np.finfo(np.float16)
print("Number of bits in the exponent portion float16 type:", a.iexp)
print("Minimum of float16 type:", a.min)
print("Maximum of float16 type:", a.max)
Number of bits in the exponent portion float16 type: 5 Minimum of float16 type: -65500.0 Maximum of float16 type: 65500.0
Float32 Type
Checking for float32 type with instances ?
import numpy as np
b = np.finfo(np.float32)
print("Number of bits in the exponent portion float32 type:", b.iexp)
print("Minimum of float32 type:", b.min)
print("Maximum of float32 type:", b.max)
Number of bits in the exponent portion float32 type: 8 Minimum of float32 type: -3.4028235e+38 Maximum of float32 type: 3.4028235e+38
Float64 Type
Checking for float64 type with instances ?
import numpy as np
c = np.finfo(np.float64)
print("Number of bits in the exponent portion float64 type:", c.iexp)
print("Minimum of float64 type:", c.min)
print("Maximum of float64 type:", c.max)
Number of bits in the exponent portion float64 type: 11 Minimum of float64 type: -1.7976931348623157e+308 Maximum of float64 type: 1.7976931348623157e+308
Complete Example
Here's a complete example comparing all three float types ?
import numpy as np
# Get information for different float types
float_types = [np.float16, np.float32, np.float64]
for dtype in float_types:
info = np.finfo(dtype)
print(f"{dtype.__name__}:")
print(f" Exponent bits: {info.iexp}")
print(f" Mantissa bits: {info.nmant}")
print(f" Total bits: {info.nexp + info.nmant + 1}") # +1 for sign bit
print()
float16: Exponent bits: 5 Mantissa bits: 10 Total bits: 16 float32: Exponent bits: 8 Mantissa bits: 23 Total bits: 32 float64: Exponent bits: 11 Mantissa bits: 52 Total bits: 64
Key Points
- Float16 uses 5 bits for exponent, 10 for mantissa, 1 for sign
- Float32 uses 8 bits for exponent, 23 for mantissa, 1 for sign
- Float64 uses 11 bits for exponent, 52 for mantissa, 1 for sign
- More exponent bits allow for a larger range of representable numbers
Conclusion
The numpy.finfo().iexp attribute provides the number of exponent bits in floating-point representations. Float16 has 5, float32 has 8, and float64 has 11 exponent bits, following the IEEE 754 standard.
