The IEEE 754 floating-point standard specifies the behavior of positive zero and negative zero under various operations. The outcome may depend on the current
IEEE rounding mode settings.
Notation In systems that include both signed and unsigned zeros, the notation 0^+ and 0^- is sometimes used for signed zeros.
Arithmetic Addition and multiplication are commutative, but there are some special rules that have to be followed, which mean the usual mathematical rules for algebraic simplification may not apply. The = sign below shows the obtained floating-point results (it is not the usual equality operator). The usual rule for signs is always followed when multiplying or dividing: • (-0) \cdot \left|x \right| = -0\,\! (for x different from ±∞) • \frac{-0}{ \left| x \right| } = -0\,\! (for x different from 0) • (-0) \cdot (-0) = +0\,\! There are special rules for adding or subtracting signed zero: • x + (\pm 0) = x\,\! (for x different from 0) • (-0) + (-0) = (-0) - (+0) = -0\,\! • (+0) + (+0) = (+0) - (-0) = +0\,\! • x - x = x + (-x) = +0\,\! (for any finite x, −0 when rounding toward negative) Because of negative zero (and also when the rounding mode is upward or downward), the expressions and , for floating-point variables
x and
y, cannot be replaced by . However can be replaced by
x with rounding to nearest (except when
x can be a
signaling NaN). Some other special rules: • \left|-0 \right| = +0\,\! • \sqrt{-0} = -0\,\! • \frac{-0}{-\infty} = +0\,\! (follows the sign rule for division) • \frac{\left|x\right|}{-0} = -\infty\,\! (for non-zero x, follows the sign rule for division) • {\pm 0} \times {\pm \infty} = \mbox{NaN}\,\! (
Not a Number or interrupt for
indeterminate form) • \frac{\pm 0}{\pm 0} = \mbox{NaN}\,\! Division of a non-zero number by zero sets the divide by zero
flag, and an operation producing a NaN sets the invalid operation flag. An
exception handler is called if enabled for the corresponding flag.
Comparisons According to the IEEE 754 standard, negative zero and positive zero should compare as equal with the usual (numerical) comparison operators, like the == operators of
C and
Java. In those languages, special programming tricks may be needed to distinguish the two values: •
Type punning the number to an integer type, so as to look at the sign bit in the bit pattern; • using the ISO C copysign() function (IEEE 754 copySign operation) to copy the sign of the zero to some non-zero number; • using the ISO C signbit() macro (IEEE 754 isSignMinus operation) that returns whether the sign bit of a number is set; • taking the reciprocal of the zero to obtain either = +∞ or = −∞ (if the
division by zero exception is not trapped). Note:
Conversion to integral type will not usually work, since almost all systems use
two's complement signed integers, which only have one representation of zero. And even when the representation has two zeros, the language, like ISO C, may not provide means to distinguish them. However, some programming languages may provide alternative comparison operators that do distinguish the two zeros. This is the case, for example, of the method in Java's Double
wrapper class. ==In rounded values==