NumPy nextafter moves to a neighbouring representable floating-point value
Floating-point numbers occupy a discrete set of representable values. Between two neighbouring members of that set, there may be mathematical numbers that the chosen format cannot store exactly. Academia Master Trader presents this English lesson for readers in Haiti, using NumPy nextafter to explain a single step through that numeric representation.
The second input selects a direction
After import numpy as np, the call np.nextafter(1.,2.) returns 1.0000000000000002 for the float64 example used here. It finds the next representable value above one, in the direction of two.
The function does not jump straight to two. The second input supplies a direction, while the spacing of the numeric format determines the next available value.
The call np.nextafter(1.,100.) produces the same result in this example. A more distant destination in the same direction does not request a larger first step.
By contrast, np.nextafter(1.,0.) returns 0.9999999999999999. This is the neighbouring representable value below one.

One step is not a fixed decimal increment
The decimal gap above one differs from the gap below it in these float64 examples. Floating-point spacing follows the representation rather than a universal decimal step size.
Nextafter therefore does not mean add 0.01, keep a chosen number of decimal places or move a specified percentage towards the second input.
A displayed value can also hide the step. If an output display rounds both the original and its neighbour to a short decimal string, they may look identical even though their stored values differ.
Use enough displayed precision when explaining these examples. A rounded label alone cannot establish that no movement occurred.
The numeric format matters
The call np.nextafter(np.float32(1),np.float32(2)) uses float32 inputs and commonly displays 1.0000001. Its neighbouring value is farther from one than the float64 neighbour shown above.
Keep both inputs explicitly typed when comparing formats. Changing one input's type can affect the operation's selected numeric type, so an informal comparison may no longer describe the intended format.
The purpose is to inspect representable neighbours, not to manufacture extra precision. Taking a step changes the value to another member of the available set.
Arrays apply the rule position by position
Two compatible arrays can provide separate starting values and directions. Broadcasting also allows a scalar direction to apply across an array of starting values.
The output shape follows the compatible inputs. Each position uses its own starting value, so a shared direction does not imply an identical numeric increment everywhere.
When the two inputs are the same ordinary finite value, the result is that value. For example, np.nextafter(1.,1.) returns 1. rather than choosing an arbitrary side.
Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the starting value, direction and numeric format before describing a neighbouring floating-point value.