Signed zero#Comparisons
{{Short description|Differentiating positive and negative zero}}
Signed zero is zero with an associated sign. In ordinary arithmetic, the number 0 does not have a sign, so that −0, +0 and 0 are equivalent. However, in computing, some number representations allow for the existence of two zeros, often denoted by −0 (negative zero) and +0 (positive zero), regarded as equal by the numerical comparison operations but with possible different behaviors in particular operations. This occurs in the sign-magnitude and ones' complement signed number representations for integers, and in most floating-point number representations. The number 0 is usually encoded as +0, but can still be represented by +0, −0, or 0.
The IEEE 754 standard for floating-point arithmetic (presently used by most computers and programming languages that support floating-point numbers) requires both +0 and −0. Real arithmetic with signed zeros can be considered a variant of the extended real number line such that {{Fraction||1|−0}} = −∞ and {{Fraction||1|+0}} = +∞; division is undefined only for {{Fraction||±0|±0}} and {{Fraction||±∞|±∞}}.
Negatively signed zero echoes the mathematical analysis concept of approaching 0 from below as a one-sided limit, which may be denoted by x → 0−, x → 0−, or x → ↑0. The notation "−0" may be used informally to denote a negative number that has been rounded to zero. The concept of negative zero also has some theoretical applications in statistical mechanics and other disciplines.
It is claimed that the inclusion of signed zero in IEEE 754 makes it much easier to achieve numerical accuracy in some critical problems,William Kahan, "Branch Cuts for Complex Elementary Functions, or Much Ado About Nothing's Sign Bit", in [http://portal.acm.org/citation.cfm?id=59678 The State of the Art in Numerical Analysis] (eds. Iserles and Powell), Clarendon Press, Oxford, 1987. in particular when computing with complex elementary functions.William Kahan, [http://www.cs.berkeley.edu/~wkahan/Math185/Derivative.pdf Derivatives in the Complex z-plane], p. 10. On the other hand, the concept of signed zero runs contrary to the usual assumption made in mathematics that negative zero is the same value as zero. Representations that allow negative zero can be a source of errors in programs, if software developers do not take into account that while the two zero representations behave as equal under numeric comparisons, they yield different results in some operations.
Representations
Binary integer formats can use various encodings. In the widely used two's complement encoding, zero is unsigned. In a 1+7-bit sign-and-magnitude representation for integers, negative zero is represented by the bit string 1000 0000
. In an 8-bit ones' complement representation, negative zero is represented by the bit string 1111 1111
. In all these three encodings, positive or unsigned zero is represented by 0000 0000
. However, the latter two encodings (with a signed zero) are uncommon for integer formats. The most common formats with a signed zero are floating-point formats (IEEE 754 formats or similar), described below.
Image:IEEE 754 Single Negative Zero.svg ]]
In IEEE 754 binary floating-point formats, zero values are represented by the biased exponent and significand both being zero. Negative zero has the sign bit set to one. One may obtain negative zero as the result of certain computations, for instance as the result of arithmetic underflow on a negative number (other results may also be possible), or −1.0 × 0.0
, or simply as −0.0
.
In IEEE 754 decimal floating-point formats, a negative zero is represented by an exponent being any valid exponent in the range for the format, the true significand being zero, and the sign bit being one.
Properties and handling
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 and 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:
- (for different from ±∞)
- (for different from 0)
There are special rules for adding or subtracting signed zero:
- (for different from 0)
- (for any finite , −0 when rounding toward negative)
Because of negative zero (and also when the rounding mode is upward or downward), the expressions {{nowrap|−(x − y)}} and {{nowrap|(−x) − (−y)}}, for floating-point variables x and y, cannot be replaced by {{nowrap|y − x}}. However {{nowrap|(−0) + x}} can be replaced by x with rounding to nearest (except when x can be a signaling NaN).
Some other special rules:
- {{cite web|url=https://speleotrove.com/decimal/daops.html#refsqrt|title=Decimal Arithmetic: Arithmetic operations{{snd}}square-root|first=Mike|last=Cowlishaw|author-link=Mike Cowlishaw|date=7 April 2009|publisher=speleotrove.com (IBM Corporation)|access-date=7 December 2010}}
- (follows the sign rule for division)
- (for non-zero , follows the sign rule for division)
- (Not a Number or interrupt for indeterminate form)
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 {{Fraction||1|+0}} = +∞ or {{Fraction||1|−0}}= −∞ (if the division by zero exception is not trapped).
Note: Casting to integral type will not always work, especially on two's complement systems.
However, some programming languages may provide alternative comparison operators that do distinguish the two zeros. This is the case, for example, of the {{mono|equals}} method in Java's Double
wrapper class.{{Cite web |title=Double |url=http://java.sun.com/javase/6/docs/api/java/lang/Double.html#equals%28java.lang.Object%29 |url-status=live |archive-url=https://web.archive.org/web/20090804205228/http://java.sun.com/javase/6/docs/api/java/lang/Double.html#equals%28java.lang.Object%29 |archive-date=Aug 4, 2009 |website=Java™ Platform, Standard Edition 6 API Specification |publisher=Oracle Help Center}}
In rounded values such as temperatures
Informally, one may use the notation "−0" for a negative value that was rounded to zero. This notation may be useful when a negative sign is significant; for example, when tabulating Celsius temperatures, where a negative sign means below freezing.
In statistical mechanics
In statistical mechanics, one sometimes uses negative temperatures to describe systems with population inversion, which can be considered to have a temperature greater than positive infinity, because the coefficient of energy in the population distribution function is −1/Temperature. In this context, a temperature of −0 is a (theoretical) temperature larger than any other negative temperature, corresponding to the (theoretical) maximum conceivable extent of population inversion, the opposite extreme to +0.{{cite book | author=Kittel, Charles and Herbert Kroemer | title=Thermal Physics (2nd ed.) | publisher=W. H. Freeman and Company | year=1980 | isbn=0-7167-1088-9|page=462}}
See also
References
{{Reflist}}
{{Refbegin}}
- {{cite web|url=http://msdn.microsoft.com/library/en-us/csspec/html/vclrfcsharpspec_4_1_6.asp|title=Floating point types|work=MSDN C# Language Specification|access-date=15 October 2005 |url-status=dead |archive-url= https://web.archive.org/web/20051206131520/http://msdn.microsoft.com/library/en-us/csspec/html/vclrfcsharpspec_4_1_6.asp |archive-date= Dec 6, 2005 }}
- {{cite web|url=http://msdn.microsoft.com/library/en-us/csspec/html/vclrfcsharpspec_7_7_2.asp|title=Division operator|work=MSDN C# Language Specification|access-date=15 October 2005 |url-status=dead |archive-url= https://web.archive.org/web/20051121104027/http://msdn.microsoft.com/library/en-us/csspec/html/vclrfcsharpspec_7_7_2.asp |archive-date= Nov 21, 2005 }}
- {{cite web |url=http://www.concentric.net/~Ttwang/tech/javafloat.htm|title=Java Floating-Point Number Intricacies|author=Thomas Wang|orig-date=March 2000|date=September 2000|url-status=dead|archive-url=https://web.archive.org/web/20050921090806/http://www.concentric.net/~Ttwang/tech/javafloat.htm|archive-date=2005-09-21}}
- {{cite web|url=https://speleotrove.com/decimal/decarith.html |website=Speleotrove |title=Decimal Arithmetic Specification, version 1.68|author=Mike Colishaw|date=28 July 2008|access-date=2008-08-14}}{{snd}}a decimal floating-point specification that includes negative zero
{{Refend}}
Further reading
{{refbegin}}
- {{cite web|url=http://developers.sun.com/prodtech/cc/articles/sign.html|title=Fortran 95 SIGN Change|author=Michael Ingrassia|work=Sun Developer Network|access-date=October 15, 2005}}{{snd}}the changes in the Fortran
SIGN
function in Fortran 95 to accommodate negative zero - {{cite web|url=http://msdn.microsoft.com/library/en-us/script56/html/js56jscondatatype.asp|title=JScript data types|work=MSDN JScript|access-date=October 16, 2005}}{{snd}}JScript's floating-point type with negative zero by definition
- {{cite web |last1=Venners |first1=Bill |date=1996-10-01 |df=mdy |url=https://www.infoworld.com/article/2077257/floating-point-arithmetic.html |title=Floating-point arithmetic |department=Learn Java |work=JavaWorld |series=Under the Hood |access-date=2020-07-14}}{{snd}}representation of negative zero in the Java virtual machine
- {{cite web|url=https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/|title=Comparing floating point numbers, 2012 Edition|author=Bruce Dawson|date=25 February 2012}}{{snd}}how to handle negative zero when comparing floating-point numbers
- {{cite web|url=http://www.fourmilab.ch/documents/univac/minuszero.html|title=Minus Zero|author=John Walker|work=UNIVAC Memories|access-date=October 17, 2005|author-link=John Walker (programmer)}}{{snd}}one's complement numbers on the UNIVAC 1100 family computers
{{refend}}