NumPy spacing describes a local floating-point step rather than a universal tolerance

Floating-point numbers occupy a discrete representation. The gap associated with one stored value need not match the gap associated with another. Academia Master Trader presents this English lesson for readers in Paraguay, using NumPy spacing to explain local resolution, signed results and the role of the numeric data type.

A local step changes with scale

After import numpy as np, the call np.spacing(np.float64(1)) returns approximately 2.220446049250313e-16.

For the same floating-point type at two, np.spacing(np.float64(2)) returns approximately 4.440892098500626e-16. At eight, the result is approximately 1.7763568394002505e-15.

These examples show why one spacing value should not be treated as a fixed increment for every magnitude. The representation has different local gaps as the scale changes.

For finite input x, NumPy describes the spacing through an adjacency property: there should be no representable number strictly between x and x plus its spacing.

Local float64 spacing for positive values one, two and eight

A returned spacing can be negative

The call np.spacing(np.float64(-1)) returns approximately -2.220446049250313e-16.

That sign matters. The function's result is not always a nonnegative geometric distance, despite the everyday meaning of the word spacing.

If an application needs a magnitude, taking an absolute value is a separate decision. Preserve the signed result when explaining how adding it moves from the original value to an adjacent representable value.

Avoid assuming the gaps on both sides are equal at every value. Near a boundary between binary scales, the neighbouring steps can differ.

Data type is part of the question

For np.float32(1), spacing is approximately 1.1920929e-07, much larger than the float64 result at one.

Both inputs describe the number one, but their representations use different precision. An explanation that omits the input type leaves out a central reason for the different outputs.

Constructing the input with an explicit floating-point type makes the comparison easier to reproduce. Keep the type visible in examples rather than relying on an unstated default.

An array input produces an array of local spacing values. This can describe how resolution varies across stored values without reducing the whole collection to one number.

Representation is not measurement accuracy

A local floating-point step describes the numeric representation. It does not establish the accuracy of a sensor, the reliability of a source or the acceptable error in a calculation.

Likewise, one spacing value is not automatically the right tolerance for comparing two independently computed results. The calculation's scale and purpose still matter.

For positive infinity, negative infinity and NaN, NumPy returns NaN rather than an ordinary finite spacing. Handle those entries separately when an application expects finite increments.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. State the input value, data type and intended interpretation before using a local spacing result.