Friday, February 6, 2026
HomeLanguagesPython – tensorflow.math.nextafter()

Python – tensorflow.math.nextafter()

TensorFlow is open-source python library designed by Google to develop Machine Learning models and deep learning  neural networks. nextafter() is used to find element wisenext representable value of x1 in the direction of x2.

Syntax: tf.math.nextafter(x1, x2, name)

Parameter:

  • x1: It’s the input tensor. Allowed dtype for this tensor are float64, float32.
  • x2: It’s the input tensor of same dtype as x1.
  • name(optional): It defines the name for the operation.

Returns: It returns a tensor of dtype as x1.

Example 1:

Python3




# Importing the library
import tensorflow as tf
 
# Initializing the input tensor
x1 = tf.constant([1, 2, -3, -4], dtype = tf.float64)
x2 = tf.constant([5, -7, 3, -8], dtype = tf.float64)
 
# Printing the input tensor
print('x1: ', x1)
print('x2: ', x2)
 
# Calculating result
res = tf.math.nextafter(x1, x2)
 
# Printing the result
print('Result: ', res)


Output:

x1:  tf.Tensor([ 1.  2. -3. -4.], shape=(4, ), dtype=float64)
x2:  tf.Tensor([ 5. -7.  3. -8.], shape=(4, ), dtype=float64)
Result:  tf.Tensor([ 1.  2. -3. -4.], shape=(4, ), dtype=float64)

Example 2: This example uses different dtype for x1 and x2. It will raise InvalidArgumentError.

Python3




# importing the library
import tensorflow as tf
 
# Initializing the input tensor
x1 = tf.constant([1, 2, -3, -4], dtype = tf.float64)
x2 = tf.constant([5, -7, 3, -8], dtype = tf.float32)
 
# Printing the input tensor
print('x1: ', x1)
print('x2: ', x2)
 
# Calculating result
res = tf.math.nextafter(x1, x2)
 
# Printing the result
print('Result: ', res)


Output:

x1:  tf.Tensor([ 1.  2. -3. -4.], shape=(4, ), dtype=float64)
x2:  tf.Tensor([ 5. -7.  3. -8.], shape=(4, ), dtype=float32)

---------------------------------------------------------------------------

InvalidArgumentError                      Traceback (most recent call last)

 in ()
      8 
      9 # Calculating result
---> 10 res = tf.math.nextafter(x1, x2)
     11 
     12 # Printing the result

2 frames

/usr/local/lib/python3.6/dist-packages/six.py in raise_from(value, from_value)

InvalidArgumentError: cannot compute NextAfter as input #1(zero-based) was expected to be a double tensor but is a float tensor [Op:NextAfter]
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32491 POSTS0 COMMENTS
Milvus
126 POSTS0 COMMENTS
Nango Kala
6862 POSTS0 COMMENTS
Nicole Veronica
11986 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12075 POSTS0 COMMENTS
Shaida Kate Naidoo
6995 POSTS0 COMMENTS
Ted Musemwa
7236 POSTS0 COMMENTS
Thapelo Manthata
6947 POSTS0 COMMENTS
Umr Jansen
6932 POSTS0 COMMENTS