Get the number of bits in the exponent portion of the floating point representation in Python

PythonServer Side ProgrammingProgramming

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 i.e. the kind of float data type to get information about.

Steps

At first, import the required library −

import numpy as np

Checking for float16 type. The iexp is to get the number of bits in the exponent portion. The min is the minimum value of given dtype. The max is the minimum value of given dtype. −

a = np.finfo(np.float16(45.9))
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)

Checking for float32 type with instances −

b = np.finfo(np.float32(22.3))
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)

Checking for float type with instances −

c = np.finfo(np.float64(29.2))
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)

Example

import numpy as np

# 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 i.e. the kind of float data type to get information about.

# Checking for float16 type
# The iexp is to get the number of bits in the exponent portion
# The min is the minimum value of given dtype.
# The max is the minimum value of given dtype.
a = np.finfo(np.float16(45.9))
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) # Checking for float32 type with instances b = np.finfo(np.float32(22.3)) 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) # Checking for float type with instances c = np.finfo(np.float64(29.2)) 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)

Output

Number of bits in the exponent portion float16 type...
5
Minimum of float16 type...
-65500.0
Maximum of float16 type...
65500.0

Number of bits in the exponent portion float32 type...
8
Minimum of float32 type...
-3.4028235e+38
Maximum of float32 type...
3.4028235e+38

Number of bits in the exponent portion float64 type...
11
Minimum of float64 type...
-1.7976931348623157e+308
Maximum of float64 type...
1.7976931348623157e+308
raja
Updated on 24-Feb-2022 10:39:21

Advertisements