NumPy signbit identifies the sign flag and distinguishes negative zero

A numeric comparison and a representation-level sign check answer different questions. NumPy signbit returns a Boolean result describing whether the sign bit is set. Academia Master Trader presents this English lesson for readers in Suriname, using ordinary finite floating-point values and signed zero to make that distinction visible.

A Boolean result for each entry

After import numpy as np, define values = np.array([-4.,-0.,0.,3.]). The call np.signbit(values) returns [True,True,False,False].

The negative nonzero value has its sign bit set. So does negative zero. Positive zero and the positive nonzero value do not have that bit set in this example.

The output contains Boolean values, not the numbers minus one, zero and one. It records a property of each input representation without replacing the input values themselves.

A scalar input produces a scalar Boolean result. For instance, np.signbit(-2.5) returns True, while np.signbit(2.5) returns False.

Sign-bit results for negative values and signed zero

Negative zero explains the difference

For the same array, values < 0 returns [True,False,False,False]. Negative zero is not less than zero, even though its sign bit is set.

The expression values == 0 returns [False,True,True,False]. Both signed zeros compare equal to zero.

These results do not contradict each other. The comparison asks about numeric order or equality; signbit asks about the sign flag carried by the representation.

Keep the chosen question explicit when building a selection rule. A rule for values strictly below zero should not be silently replaced by a rule that also includes negative zero.

A mask can select the marked entries

With mask = np.signbit(values), the selection values[mask] returns [-4.,-0.] for this constructed array.

The mask keeps the same arrangement as the input array. Each Boolean belongs to the corresponding position, so preserving alignment matters when applying it elsewhere.

A multidimensional input similarly produces a Boolean array with the same shape. The function does not collapse rows into one summary or count marked entries automatically.

Selection also leaves the meaning of the selected values unchanged. A negative-zero entry remains zero numerically; its inclusion reflects the mask's sign-bit criterion.

Representation is not a rounding rule

Signbit does not round a small negative value to zero, measure how far it is from zero or decide whether it is negligible for a particular application.

A separate threshold rule may be useful for such questions, but that rule needs its own stated units and purpose. The sign flag alone supplies neither.

These finite examples isolate the sign-bit distinction. They do not provide a general procedure for classifying every exceptional floating-point value.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Name whether a rule concerns numeric order, equality or the stored sign flag before selecting entries.