Return the next floating-point value after a value towards another value in Numpy

NumpyServer Side ProgrammingProgramming

To return the next floating-point value after a value towards another value, element-wise., use the numpy.nextafter() method in Python Numpy. The 1st parameter is the value to find the next representable value of. The 2nd parameter is the direction where to look for the next representable value.

The function returns the next representable values of x1 in the direction of x2. This is a scalar if both x1 and x2 are scalars.

The out is a location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number of outputs.

The condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default out=None, locations within it where the condition is False will remain uninitialized.

Steps

At first, import the required library −

import numpy as np

To return the next floating-point value after a value towards another value, element-wise, use the numpy.nextafter() method.

Check for float and inf −

print("Result? ", np.nextafter(0.1, np.inf))
print("
Result? ", np.nextafter(0.1, -np.inf))

Check for int and inf −

print("
Result? ", np.nextafter(5, np.inf)) print("
Result? ", np.nextafter(10, -np.inf))

Check for nan and inf −

print("
Result? ", np.nextafter(np.nan, np.inf)) print("
Result? ", np.nextafter(np.nan, -np.inf))

Check for nan and inf −

print("
Result? ", np.nextafter(np.nan, np.inf)) print("
Result? ", np.nextafter(np.nan, -np.inf))

Check for log and inf −

print("
Result? ", np.nextafter(np.log(1), np.inf)) print("
Result? ", np.nextafter(np.log(2), -np.inf))

Example

import numpy as np

# To return the next floating-point value after a value towards another value, element-wise., use the numpy.nextafter() method in Python Numpy
# The 1st parameter is the value to find the next representable value of.
# The 2nd parameter is the direction where to look for the next representable value.

# Check for float and inf
print("Result? ", np.nextafter(0.1, np.inf))
print("
Result? ", np.nextafter(0.1, -np.inf)) # Check for int and inf print("
Result? ", np.nextafter(5, np.inf)) print("
Result? ", np.nextafter(10, -np.inf)) # Check for nan and inf print("
Result? ", np.nextafter(np.nan, np.inf)) print("
Result? ", np.nextafter(np.nan, -np.inf)) # Check for nan and inf print("
Result? ", np.nextafter(np.nan, np.inf)) print("
Result? ", np.nextafter(np.nan, -np.inf)) # Check for log and inf print("
Result? ", np.nextafter(np.log(1), np.inf)) print("
Result? ", np.nextafter(np.log(2), -np.inf))

Output

Result? 0.10000000000000002

Result? 0.09999999999999999

Result? 5.000000000000001

Result? 9.999999999999998

Result? nan

Result? nan

Result? nan

Result? nan

Result? 5e-324

Result? 0.6931471805599452
raja
Updated on 08-Feb-2022 11:15:24

Advertisements