Skip to content

tf.experimental.numpy.isclose and allclose return inverted results for infinities #128480

Closed

Description

@CaelusHu

Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

Yes

Source

binary

TensorFlow version

tf-nightly 2.23.0.dev20261003 (v1.12.1-146048-g170cb1a320b)

Custom code

Yes

OS platform and distribution

macOS 26.6.2, arm64

Mobile device

Not applicable

Python version

3.13.7

Bazel version

No response

GCC/compiler version

No response

CUDA/cuDNN version

No response

GPU model and memory

No response

Current behavior?

tf.experimental.numpy.isclose returns the opposite of NumPy for infinities:
same-sign infinities are reported as not close, while opposite-sign infinities
are reported as close with the default tolerances. allclose inherits the
same behavior.

The floating-point branch applies the finite tolerance formula directly.
Thus inf - inf becomes NaN, while abs(-inf - inf) <= rtol * abs(inf)
becomes inf <= inf.

Expected behavior

Matching NumPy, infinities should be close only when they have the same sign.

Related upstream issues

I could not find an existing issue or PR for infinity handling. Issue #108657
and PRs #108702/#123867 concern integer inputs; PR #122605 concerns an XLA
shape mismatch. None cover this floating-point non-finite case.

Standalone code to reproduce the issue

import numpy as np
import tensorflow as tf
import tensorflow.experimental.numpy as tnp

a = tf.constant([np.inf, -np.inf, -np.inf, np.inf, 1.0], tf.float64)
b = tf.constant([np.inf, -np.inf, np.inf, -np.inf, 1.0], tf.float64)

print("TensorFlow isclose:", tnp.isclose(a, b).numpy().tolist())
print("NumPy isclose:     ", np.isclose(a.numpy(), b.numpy()).tolist())
print("TensorFlow allclose same inf:", bool(tnp.allclose([np.inf], [np.inf]).numpy()))
print("NumPy allclose same inf:     ", bool(np.allclose([np.inf], [np.inf])))
print("TensorFlow allclose opposite inf:", bool(tnp.allclose([-np.inf], [np.inf]).numpy()))
print("NumPy allclose opposite inf:     ", bool(np.allclose([-np.inf], [np.inf])))

Relevant log output

TensorFlow isclose: [False, False, True, True, True]
NumPy isclose:      [True, True, False, False, True]
TensorFlow allclose same inf: False
NumPy allclose same inf:      True
TensorFlow allclose opposite inf: True
NumPy allclose opposite inf:      False


The same result occurs for `float32` and `float64`, scalars and vectors, eager
execution, `tf.function`, and XLA. NumPy version: 2.5.3.

Activity

  1. genius-0963 commented on Oct 4, 2026

    @genius-0963
    Contributor

    Could you please check whether the issue has been fixed or not?

  2. added
    comp:ops
    2.21.0
    on Oct 5, 2026
  3. Kayyuri commented on Oct 5, 2026

    @Kayyuri

    Hi @CaelusHu, Thank you for reporting this issue!
    A fix for this issue has been proposed in this PR. Once it is merged, this issue will be resolved.

    Many thanks to @genius-0963 for raising the PR to address this issue. We truly appreciate your initiative and the effort in contributing the fix.

  4. added 2 commits that reference this issue on Oct 5, 2026
    08f0f15
    aa9cb3d
  5. google-ml-butler commented on Oct 15, 2026

    @google-ml-butler

    Are you satisfied with the resolution of your issue?
    Yes
    No

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

2.21.0awaiting PR mergecomp:opstype:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions