Python math.nextafter() Method



The Python math.nextafter() method is used to calculate the next representable floating-point number after a given floating-point value in the specified direction.

It returns the next floating-point value following the first argument in the direction of the second argument.

Syntax

Following is the basic syntax of the Python math.nextafter() method −

math.nextafter(x, y)

Parameters

This method accepts the following parameters −

  • x − It is the starting point (a floating-point number).

  • y − It is the direction (another floating-point number).

Return Value

The method returns the next representable floating-point value after "x" in the direction of "y".

Example 1

In the following example, we are finding the next representable floating-point value after "2.0" in the direction of "3.0" −

import math
result = math.nextafter(2.0, 3.0)
print("The result obtained is:",result)         

Output

The output obtained is as follows −

The result obtained is: 2.0000000000000004

Example 2

Here, we are finding the next representable floating-point value after "2.0" in the negative direction of "1.0" −

import math
result = math.nextafter(2.0, 1.0)
print("The result obtained is:",result)  

Output

Following is the output of the above code −

The result obtained is: 1.9999999999999998

Example 3

Now, we are finding the next representable floating-point value after "0.0" in the direction of "1.0". The result is the smallest positive normalized floating-point number −

import math
result = math.nextafter(0.0, 1.0)
print("The result is:",result)  

Output

We get the output as shown below −

The result obtained is: 5e-324

Example 4

In this example, we use variables "x" and "y" to store the values "1.5" and "2.5" respectively. We then find the next representable floating-point value after "1.5" in the direction of "2.5" −

import math
x = 1.5
y = 2.5
result = math.nextafter(x, y)
print("The result obtained is:",result)  

Output

The result produced is as shown below −

The result obtained is: 1.5000000000000002
python_maths.htm
Advertisements
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy