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

Parameter Required Description
x Yes A numeric value.
y Yes A numeric value.

Examples

1. Next floating point number towards 10

In the following program, we find the 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 Code

Output

nextafter() : 5.001000000000001

2. Next floating point number towards -4

In the following program, we find the 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 Code

Output

nextafter() : 5.0009999999999994

3. Next floating point number of -infinity towards 0

In the following program, we find the 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 Code

Output

nextafter() : -1.7976931348623157e+308

4. Next floating point number of +infinity towards 0

In the following program, we find the 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 Code

Output

nextafter() : 1.7976931348623157e+308

Summary

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

Related Tutorials

Privacy Policy Terms of Use

SitemapContact Us