NumPy ldexp()
The numpy.ldexp() function computes the value of x1 * 2**x2 for each element in the input arrays.
It is useful for constructing floating-point numbers using a mantissa (x1) and an exponent (x2).
Syntax
</>
Copy
numpy.ldexp(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
| Parameter | Type | Description |
|---|---|---|
x1 | array_like | Array of multipliers (mantissas). |
x2 | array_like, int | Array of two’s exponents. If x1.shape != x2.shape, they must be broadcastable to a common shape. |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array where the result is stored. If None, a new array is created. |
where | array_like, optional | Boolean mask specifying which elements to compute. Elements where where=False retain their original value. |
casting | str, optional | Defines the casting behavior when computing the function. |
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 an array or scalar containing the computed values of x1 * 2**x2. If both x1 and x2 are scalars, the result is a scalar.
Examples
1. Computing ldexp for Single Values
Here, we compute ldexp for a single mantissa and exponent.
</>
Copy
import numpy as np
# Define a mantissa and an exponent
mantissa = 3.5
exponent = 2
# Compute the ldexp result
result = np.ldexp(mantissa, exponent)
# Print the result
print("3.5 * 2**2 =", result)
Output:
3.5 * 2**2 = 14.0
