NumPy frexp separates each number into a fraction and a power of two

A real number can be described as a fractional component multiplied by a power of two. NumPy frexp returns those two components separately. Academia Master Trader presents this English lesson for readers in South Africa, using constructed values to explain paired outputs, binary scaling and the difference between decomposition and rounding.

One input produces two corresponding outputs

After import numpy as np, define values = np.array([6.,-10.,0.125,0.]). The statement fraction, exponent = np.frexp(values) assigns one output array to each name.

The fractions are [0.75,-0.625,0.5,0.], and the exponents are [3,4,-2,0]. Corresponding positions belong together: six is represented by the fraction 0.75 and exponent three.

The relationship is values = fraction * 2**exponent. For the first entry, 0.75 multiplied by eight gives six. For the second, minus 0.625 multiplied by sixteen gives minus ten.

Both arrays follow the input shape. They are not a sorted list of fractions followed by unrelated exponents, and neither output alone describes the complete original magnitude.

Real values decomposed into fractions and binary exponents

Negative exponents describe smaller scales

The third input, 0.125, becomes fraction 0.5 with exponent minus two. Two raised to minus two is one quarter, and one half multiplied by one quarter gives one eighth.

The exponent therefore need not be positive. It records the required power-of-two scale, rather than counting decimal places or digits displayed after a decimal point.

The fractional component preserves the input's sign for these nonzero examples. A negative exponent does not make a positive fraction negative: it changes the scale through a reciprocal power of two.

Zero has its own simple representation

For the zero input in this example, both returned components are zero. The reconstruction is zero multiplied by one, which remains zero.

Keep zero separate when describing the size of a nonzero fractional component. The displayed arrays show why a single informal rule about all fractions being near one cannot describe every input.

This lesson uses finite real values. The function does not support complex dtypes, so complex-valued data require a different operation rather than assuming the same two-output convention applies.

Decomposition is not decimal rounding

Frexp does not ask for a number of decimal places, remove the fractional part or round six to a nearby power of two. It returns components whose combination describes the supplied value.

The exponent is an integer array, while the fractional output contains floating-point values. The name fraction here refers to the returned binary scaling component, not simply the digits after a decimal point.

For these constructed values, fraction * np.power(2.,exponent) reconstructs [6.,-10.,0.125,0.]. Using a floating base makes the negative exponent explicit in the reconstruction expression.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Keep each fraction beside its corresponding exponent and record that the scale uses powers of two.