tf.math.truediv

Divides x / y elementwise (using Python 3 division operator semantics).

This function forces Python 3 division operator semantics where all integer arguments are cast to floating types first. If you want integer division that rounds down, use x // y or tf.math.floordiv.

x and y must have the same numeric type. If the inputs are floating point, the output will have the same type. If the inputs are integral, the inputs are cast to float32 for int8 and int16 and float64 for int32 and int64 (matching the behavior of Numpy).

Example:

# Division with integer tensors (returns float)
x1 = tf.constant([10, 20, 30], dtype=tf.int32)
y1 = tf.constant([2, 4, 5], dtype=tf.int32)
result1 = tf.math.truediv(x1, y1)

# Division with different shaped tensors (broadcasting)
x2 = tf.constant([[10, 20], [30, 40]], dtype=tf.float64)
y2 = tf.constant([2, 5], dtype=tf.float64)
result2 = tf.math.truediv(x2, y2)

Handling potential division by zero (returns inf)

>>> x3 = tf.constant(5, dtype=tf.float32)
>>> y3 = tf.constant(0, dtype=tf.float32)
>>> result3 = tf.math.truediv(x3, y3)

x Tensor numerator of numeric type.
y Tensor denominator of numeric type.
name A name for the operation (optional).

x / y evaluated in floating point.

TypeError If x and y have different dtypes.