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

ParameterTypeDescription
x1array_likeArray of multipliers (mantissas).
x2array_like, intArray of two’s exponents. If x1.shape != x2.shape, they must be broadcastable to a common shape.
outndarray, None, or tuple of ndarray and None, optionalOptional output array where the result is stored. If None, a new array is created.
wherearray_like, optionalBoolean mask specifying which elements to compute. Elements where where=False retain their original value.
castingstr, optionalDefines the casting behavior when computing the function.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalDefines the data type of the output array.
subokbool, optionalDetermines 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