NumPy frexp()
The numpy.frexp()
function decomposes each element of an input array into its mantissa and exponent.
It expresses the number in the form:
x = mantissa × 2**exponent
The mantissa will always lie in the range (-1, 1), and the exponent is an integer.
Syntax
</>
Copy
numpy.frexp(x, [out1, out2, ]/, [out=(None, None), ]*, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
Parameter | Type | Description |
---|---|---|
x | array_like | Array of numbers to be decomposed into mantissa and exponent. |
out1 | ndarray, optional | Optional output array for the mantissa. Must have the same shape as x . |
out2 | ndarray, optional | Optional output array for the exponent. Must have the same shape as x . |
out | ndarray, None, or tuple of ndarray and None, optional | A location to store the result. If None, a new array is created. |
where | array_like, optional | Boolean mask specifying elements to process. Elements where where=False retain their original value. |
casting | str, optional | Defines the casting behavior for computation. |
order | str, optional | Memory layout order of the output array. |
dtype | data-type, optional | Defines the data type of the output array. |
subok | bool, optional | Determines if subclasses of ndarray are preserved in the output. |
Return Value
Returns a tuple (mantissa, exponent)
, where:
mantissa
: An array with floating-point values in the range (-1, 1).exponent
: An array of integer exponents.
Examples
1. Decomposing a Single Number
We decompose a single floating-point number into its mantissa and exponent.
</>
Copy
import numpy as np
# Define a number
num = 16.0
# Decompose into mantissa and exponent
mantissa, exponent = np.frexp(num)
# Print the results
print("Mantissa:", mantissa)
print("Exponent:", exponent)
Output:
Mantissa: 0.5
Exponent: 5

The result shows that 16.0 = 0.5 × 2⁵
.
2. Decomposing an Array of Numbers
Here, we decompose multiple numbers in an array.
</>
Copy
import numpy as np
# Define an array of numbers
nums = np.array([4.0, 8.0, 32.0, 64.0])
# Compute the mantissa and exponent
mantissa, exponent = np.frexp(nums)
# Print the results
print("Numbers:", nums)
print("Mantissas:", mantissa)
print("Exponents:", exponent)
Output:
Numbers: [ 4. 8. 32. 64.]
Mantissas: [0.5 0.5 0.5 0.5]
Exponents: [3 4 6 7]

Each number is represented in the form x = mantissa × 2**exponent
.
3. Using the out
Parameter
We store the mantissa and exponent in preallocated output arrays.
</>
Copy
import numpy as np
# Define an array of numbers
nums = np.array([10.0, 20.0, 40.0])
# Preallocate output arrays
mantissa_out = np.empty_like(nums)
exponent_out = np.empty_like(nums, dtype=int)
# Compute frexp and store results in output arrays
np.frexp(nums, out=(mantissa_out, exponent_out))
# Print the results
print("Mantissas:", mantissa_out)
print("Exponents:", exponent_out)
Output:
Mantissas: [0.625 0.625 0.625]
Exponents: [4 5 6]

4. Using the where
Parameter
We use a condition to compute frexp
only for selected elements.
</>
Copy
import numpy as np
# Define an array of numbers
nums = np.array([10.0, 20.0, 40.0, 80.0])
# Define a condition mask (apply frexp only where True)
mask = np.array([True, False, True, False])
# Compute mantissa and exponent where mask is True
mantissa, exponent = np.frexp(nums, where=mask)
# Print the results
print("Mantissas:", mantissa)
print("Exponents:", exponent)
Output:
Mantissas: [0.625 0. 0.625 0. ]
Exponents: [4 0 6 0]

Only elements where mask=True
are processed; other values remain unchanged.