What is math nextafter(x, y) in python?

The math.nextafter() method is used to get a float value between x and y where x, y are argument values. This return float value will be towards y.

This method math.nextafter() is available from the latest version of Python i.e., Python 3.9

Syntax


math.nextafter(x, y)

Parameters

It takes two argument values.

  • x: The lower numeric value.
  • y: The upper numeric value.

Return Value

It will return a single float value.

  • If x=y, then it will return a y value.

Example

In the code snippet below, we have different argument values.

  1. If x=Any_Number,y=Any_Number, then it will return the value towards y.
  2. If x=Any_Number, y=math.inf, then it will go up towards positive infinity.
  3. If x=Any_Number, y=-math.inf, then it will go down towards minus infinity.
  4. If x=Any_Number, y=0.0, then it will go towards zero.
import math
print(math.nextafter(3, 4.2))
1. Expected Output
import math
print(math.nextafter(3, math.inf))
2. Expected Output
import math
print(math.nextafter(3, -math.inf))
3. Expected Output
import math
print(math.nextafter(3, 0.0))
4. Expected Output

Free Resources