Numpy Nextafter: How to Use Numpy Nextafter in Python?

Numpy Nextafter

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

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,

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

Using 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).
Using Nextafter On Scalars
Using nextafter( ) On Scalars

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)
Using Nextafter On Scalars In Direction Of Infinity
Using nextafter( ) on Scalars in the Direction of +Infinity

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

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)
Using Nextafter On Arrays
Using nextafter( ) on Arrays

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!