Skip to content

ops.signbit returns False for negative zero #371

Description

@ALinrunrun

Environment

Hardware Environment(Ascend/GPU/CPU):

/device cpu

Software Environment:

  • MindSpore version (source or binary): MindSpore 2.9.0
  • Python version (e.g., Python 3.7.5): Python 3.11.15
  • OS platform and distribution (e.g., Linux Ubuntu 16.04): Ubuntu 24.04.1, Linux 6.17.0-29-generic, x86_64
  • GCC/Compiler version (if compiled from source): gcc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0

Describe the current behavior

mindspore.ops.signbit appears to handle negative zero incorrectly for float32 tensors.

For an input containing -0.0, calling:

ops.signbit(x)

returns False for the -0.0 element.

Actual result:

[[False, False, False],
 [True, False, False]]

The first element should be True because -0.0 has the sign bit set.

Describe the expected behavior

ops.signbit should return True for negative zero and negative finite values, and False for positive zero and positive finite values.

Expected result:

[[True, False, False],
 [True, False, False]]

Steps to reproduce the issue

  1. Install MindSpore 2.9.0 and NumPy 1.26.4.
  2. Save the following script as reproduce.py.
  3. Run python reproduce.py.
import numpy as np
import mindspore as ms
import mindspore.ops.function as ops

x = ms.Tensor(
    np.array(
        [
            [-0.0, 0.0, np.nextafter(np.float32(0.0), np.float32(1.0))],
            [-1.0, 1.0, 9.999999974752427e-07],
        ],
        dtype=np.float32,
    )
)

wrong = ops.signbit(x).asnumpy()

correct = np.array(
    [
        [True, False, False],
        [True, False, False],
    ],
    dtype=bool,
)

print("wrong:", wrong.tolist())
print("correct:", correct.tolist())

Related log / screenshot

Running the script produces:

wrong: [[False, False, False], [True, False, False]]
correct: [[True, False, False], [True, False, False]]

Environment output:

Python version: 3.11.15
OS platform: Linux-6.17.0-29-generic-x86_64-with-glibc2.39
Machine: x86_64
MindSpore version: 2.9.0
NumPy version: 1.26.4
GCC version: gcc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0

Special notes for this issue

This looks like a signed-zero handling issue in ops.signbit.

For the first element, the input is:

-0.0

The current result is:

False

The expected result is:

True

Other values in this example, such as 0.0, positive subnormal values, -1.0, and positive finite values, appear to be handled as expected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions