NumPy ldexp constructs floating values from multipliers and integer powers of two

Binary scaling combines a multiplier with an integer exponent of two. NumPy ldexp performs that combination element by element, returning floating-point values. Academia Master Trader presents this English lesson for readers in Timor-Leste, using constructed inputs to explain paired scaling, negative exponents and compatible input shapes.

Each exponent selects a power of two

After import numpy as np, define multiplier = np.array([0.75,-0.625,0.5]) and exponent = np.array([3,4,-2]). The call np.ldexp(multiplier,exponent) returns [6.,-10.,0.125].

The first multiplier is scaled by two cubed, so 0.75 times eight gives six. The second is scaled by two to the fourth power, so minus 0.625 times sixteen gives minus ten.

The third uses exponent minus two. Its factor is one quarter, and multiplying 0.5 by that factor gives 0.125.

The exponent input contains integers. It describes powers of two, not decimal places, a general base chosen separately or a count of repeated additions.

Multipliers combined with powers of two

A scalar exponent supplies a shared scale

The call np.ldexp([1.5,2.5],2) returns [6.,10.]. The scalar exponent two supplies a factor of four to both multipliers.

The multiplier does not need to be a fraction between minus one and one. Here both inputs exceed one in magnitude, yet the same power-of-two relationship applies.

Similarly, np.ldexp(1.5,[0,1,2]) returns [1.5,3.,6.]. A scalar multiplier can be shared across several integer exponents, producing one output for each compatible position.

Broadcasting determines the common output shape. It does not pair unrelated arrays by sorting them or infer which scale should belong to a particular observation.

Negative exponents reduce the scale

The call np.ldexp(8.,[-1,-2,-3]) returns [4.,2.,1.]. Each negative exponent selects a reciprocal power of two.

This differs from changing a sign: all three results remain positive because the multiplier is positive and powers of two are positive. A negative multiplier would retain its negative sign under these finite examples.

An exponent of zero supplies a factor of one. It does not erase the multiplier, round it or create a zero output automatically.

Reconstruction keeps two components together

A decomposition into a binary fraction and exponent can be reversed by passing those components to ldexp. This is the operation's relationship to frexp, which returns those components separately.

Keep corresponding positions aligned. Combining one fraction with another entry's exponent reconstructs a different value, even when both component arrays have valid shapes.

The function does not support complex dtypes. It also works within floating-point representation rather than promising unlimited numeric range or arbitrary precision.

These examples describe exact constructed values with simple binary representations. They explain scaling, not a forecast or an automatic decision about real-world data.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the multiplier, integer exponent and compatible output arrangement when explaining binary scaling.