Python math.nextafter() – Next Floating Point Number

Python math.nextafter()

math.nextafter(x, y) function returns the next possible floating point value after x towards y.

Syntax

The syntax to call nextafter() function is

math.nextafter(x, y)

where

ParameterRequiredDescription
xYesA numeric value.
yYesA numeric value.

Examples

Next floating point number of 5.001 towards 10.

Python Program

import math

x = 5.001
y = 10
result = math.nextafter(x, y)
print('nextafter() :', result)
Run

Output

nextafter() : 5.001000000000001

Next floating point number of 5.001 towards -4.

Python Program

import math

x = 5.001
y = -4
result = math.nextafter(x, y)
print('nextafter() :', result)
Run

Output

nextafter() : 5.0009999999999994

Next floating point number of negative infinity towards 0.

Python Program

import math

x = -math.inf
y = 0
result = math.nextafter(x, y)
print('nextafter() :', result)
Run

Output

nextafter() : -1.7976931348623157e+308

Next floating point number of positive infinity towards 0.

Python Program

import math

x = math.inf
y = 0
result = math.nextafter(x, y)
print('nextafter() :', result)
Run

Output

nextafter() : 1.7976931348623157e+308

Summary

In this Python Examples tutorial, we learned the syntax of, and examples for math.nextafter() function.