A complex value like `3 + 4j` comes back as `0.6 + 0.8j`, not a plain `1`. I was surprised that its direction survives even though its magnitude changes.

That difference matters when you’re mapping complex arrays and interpreting the results. To make sense of each output, it helps to know the rule NumPy applies to each input value.

What np.sign returns

np.sign is a NumPy universal function (ufunc) that applies one operation to each input value. An array receives one result per element, and the output keeps the array’s shape.

Input Result
x < 0 -1
x = 0 0
x > 0 1
Positive or negative infinity 1 or -1, respectively
NaN NaN

What the input and output parameters control

Pass a scalar or array-like value as x. With a NumPy array, each element gets a result at the same index, so the shape stays unchanged.

  • x contains the values whose signs you need.
  • out names an existing array for the result.
  • where selects which output positions are written.

NumPy broadcasts where over x, and false positions retain their current out values. The out array must match the broadcast result’s shape.

With out=None, masked positions in the new result are uninitialized. Initialize out before reading those slots.

Apply np.sign to integer and floating-point arrays

NumPy applies the sign rule independently to each value, for both integer and floating-point arrays.

Input Result form
Integer array Integer sign array
Floating-point array Floating-point sign array
Scalar zero Scalar zero
import numpy as np

values = np.array([4, -10])
floats = np.array([-0.002, 13.2, 4 / 3])
print(np.sign(values))
print(np.sign(floats))
print(np.sign(0))
np.sign returns -1 for negative values, 1 for positive values, and 0 for zero.

Complex values keep their direction

For a nonzero complex z, current NumPy returns z / abs(z). Dividing by abs(z) gives the result magnitude one without changing z’s direction.

The current np.sign reference notes that NumPy 2.0 adopted this definition to follow the Array API standard.

import numpy as np

values = np.array([3 + 4j, -3 + 4j, 5j])
print(np.sign(values))
./.venv/bin/python complex_sign_example.py
Current NumPy complex sign outputs preserve each value's direction
Current np.sign results for three complex inputs.

I ran the complex array on NumPy 2.5.3. The 3 + 4j input returned 0.6 + 0.8j, keeping its component ratio while normalizing the magnitude.

Use signbit when negative zero matters

NumPy maps -0.0 to 0.0, so use np.signbit when the input’s sign bit matters.

Use np.copysign when you want one value’s sign copied onto another magnitude.

import numpy as np

value = -0.0
result = np.sign(value)
print(result)
print(np.signbit(value))
print(np.signbit(result))

I ran the signed-zero sample on NumPy 2.5.3. It printed 0.0, True, then False, so the returned zero no longer carries the input’s sign bit.

Use where to mask output writes

A false mask position is skipped by the ufunc, so NumPy leaves whatever value was already stored in that output slot. Initialize the buffer first.

import numpy as np

values = np.array([-4, 0, 7])
out = np.full(values.shape, 99)
np.sign(values, out=out, where=values != 0)
print(out)
./.venv/bin/python where_example.py
np.sign writes selected array positions and leaves the masked value unchanged
The false where position keeps its prefilled output value.

I set out to 99 before applying where, and the masked middle slot stayed 99, so the mask skipped that assignment rather than writing zero.

Keep magnitude until you need only its sign

np.sign reduces each finite input to its sign label. Keep the original array when later calculations need its magnitude, then compare both outputs directly.

./.venv/bin/python -c 'import numpy as np; x=np.array([-4,0,7]); print(np.sign(x), np.abs(x))'

Common np.sign questions

Scalar input and complex zero follow two separate return rules.

Does np.sign return an array for scalar input?

No. NumPy returns a scalar when the input is scalar, and an array when the input is array-like.

What does np.sign return for a zero-valued complex number?

It returns 0j. NumPy handles zero directly because z / abs(z) would otherwise divide by zero.

Share.
Leave A Reply