Skip to content

tf.experimental.numpy.around gives wrong results for negative decimals (e.g. around(5, -1) returns 9) #129232

Closed

Description

@bodapatisaikrishna

Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

Yes

Source

binary

TensorFlow version

tf-nightly 2.23.0-dev20261014 (git v1.12.1-146947-gfcb676979cd)

Custom code

Yes

OS platform and distribution

macOS 15.7.2 (arm64)

Mobile device

No response

Python version

3.11.7

Bazel version

No response

GCC/compiler version

No response

CUDA/cuDNN version

No response

GPU model and memory

No response

Current behavior?

tnp.around (also tnp.round and round() on a tnp ndarray) returns wrong values for float64 and integer inputs when decimals < 0, and loses precision when decimals >= 11.

Cause (np_array_ops.py#L957-L968): factor = math.pow(10, decimals) is a Python float, and math_ops.cast(factor, float64) first converts it to a float32 tensor and then casts. So 0.1 turns into 0.10000000149011612. For 25, 25 * factor = 2.5000000037, which rounds to 3, and 3 / factor = 29.99999955. Integer inputs are then cast back with truncation, which gives 29. With 5 the result is 9 where NumPy returns 0.

Building the factor directly in the target dtype, e.g. constant_op.constant(factor, dtype), should fix it. Integer inputs with decimals >= 0 could also skip the float round trip (around(2**53 + 1) currently returns 2**53).

Expected (NumPy): around(5, -1) == 0, around(25, -1) == 20, around(35, -1) == 40, around(123.456, -1) == 120.0, around(0.123456789012, 11) == 0.12345678901.

Standalone code to reproduce the issue

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

for v, d in [(5, -1), (25, -1), (35, -1), (123.456, -1), (0.123456789012, 11)]:
  print(f"around({v}, {d}): tnp={tnp.around(v, d).numpy()!r}  np={np.around(v, d)!r}")

Relevant log output

around(5, -1): tnp=np.int64(9)  np=np.int64(0)
around(25, -1): tnp=np.int64(29)  np=np.int64(20)
around(35, -1): tnp=np.int64(39)  np=np.int64(40)
around(123.456, -1): tnp=np.float64(119.99999821186069)  np=np.float64(120.0)
around(0.123456789012, 11): tnp=np.float64(0.12345678900839503)  np=np.float64(0.12345678901)

Activity

  1. added
    comp:ops
    2.21.0
    on Oct 19, 2026
  2. Kayyuri commented on Oct 19, 2026

    @Kayyuri

    Hi @bodapatisaikrishna,
    Thanks for reporting this issue and submitting a fix for TensorFlow. Once the PR is merged, the issue will be resolved. We appreciate your contribution.

  3. added a commit that references this issue on Oct 23, 2026
    adefc9a
  4. google-ml-butler commented on Oct 23, 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