Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

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

Fouzia Bashir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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

RELATED TAGS

python
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring