Looking for a function to return the next floating point value of a given number in the direction of another number? Do you want the same function to carry out the same with a collection of numbers rather than a single number?

Well, look no further for the *numpy *library has got just the thing you are looking for â€“ the *nextafter( ) *function. In this article, we shall demonstrate the functionality of the above function along with understanding the basic constructs that are to be fed as inputs for its functioning.

*Also read: Numpy.kron(): How to Calculate Kronecker Product Using Numpy?*

We shall get things started by first importing the *numpy *library using the following code.

```
import numpy as np
```

Thereafter, we shall explore further the *nextafter( ) *function through each of the following sections.

**Syntax of the***nextafter( )*function**Using***nextafter( )*on Scalars**Using***nextafter( )*on N-Dimensional Arrays

**Syntax ofÂ the ***nextafter( )*Â function

*nextafter( )*Â function

The function relies on the primary inputs x1 & x2 as given below of which, x1 is the entity for which the next floating point value is to be found along the direction of x2. The other optional inputs that can also be used within the *nextafter( ) *function are as follows.

```
numpy.nextafter(x1, x2, out=None, *, where=True, dtype=None)
```

where,

N-dimensional array or a scalar for which the nearest floating point value is to be found*x1 â€“*N-dimensional array or a scalar to provide the direction of search*x2 â€“*an optional construct set to*out â€“**none*by default, but could be used to store the results in the desired array which is of the same length as the outputkwargs or keyword argument which is an optional construct used to pass keyword variable length of argument to a function***â€“an optional construct which is used to calculate the universal function (ufunc) at the given position when set to*where â€“**True*(default setting) or not calculate when set to*False*an optional construct used to specify the data type which is being used*dtype â€“*

**Using ***nextafter( )* on Scalars

*nextafter( )*on Scalars

In this section let us use a couple of scalars for deploying into the *nextafter( ) *function as shown below.

```
a = 100
b = -50
np.nextafter(a, b)
```

Once the above code is run, the following computation happens in the back end to return the next nearest number to â€˜aâ€™ when searched in the direction of â€˜bâ€™.

- â€˜bâ€™ is given as â€˜-50â€™ which means that the direction in which the next nearest number to â€˜aâ€™ is to be searched leftwards or before â€˜100â€™, rather than after it.
- The nearest number to â€˜100â€™ is found to be 99.99999999999999 (Python returns the floating point number to 14-digit precision after the decimal).

From the above explanation, it could be deduced that the direction of search is synonymous with searching a number (first input, x1) in the number line rightwards when the second input â€˜x2â€™ is greater than the first input â€˜x1â€™ or leftwards if the second input â€˜x2â€™ is lesser than the first input â€˜x1â€™.

The same logic holds good even if one uses positive or negative infinity as â€˜x2â€™ rather than the other numbers and we shall use the following code to establish this conundrum.

```
c = -10
d = +np.inf
np.nextafter(c, d)
```

The above result bears evidence that the nearest floating point number returned is towards the right of â€˜-10â€™ in the number line since the direction of the search is positive infinity.

**Using ***nextafter( ) *on N-Dimensional Arrays

*nextafter( )*on N-Dimensional Arrays

When deployed on arrays the results would be rounded off to the same number of digits after the decimal given in the array, rather than the whole 14 digits as depicted below.

```
ar1 = np.array([[1.05, 3, 7],
[2.9, 9, 4.3666667]])
ar2 = np.array([[-1, 0.3, +np.inf],
[0, -np.inf, 10]])
np.nextafter(ar1, ar2)
```

**Conclusion**

Now that we have reached the end of this article, hope it has elaborated on how to use theÂ *nextafter( )Â *function from theÂ *numpyÂ *library. Hereâ€™s another article that details the calculation of the Kronecker product using numpy kron(). There are numerous other enjoyable and equally informative articles in AskPython that might be of great help to those who are looking to level up in Python.Â *Mazel tov*!