IEEE 754-1985 - Comparing Floating-point Numbers

Comparing Floating-point Numbers

Every possible bit combination is either a NaN or a number with a unique value in the affinely extended real number system with its associated order, except for the two bit combinations negative zero and positive zero, which sometimes require special attention (see below). The binary representation has the special property that, excluding NaNs, any two numbers can be compared like sign and magnitude integers (although with modern computer processors this is no longer directly applicable): if the sign bit is different, the negative number precedes the positive number (except that negative zero and positive zero should be considered equal), otherwise, relative order is the same as lexicographical order but inverted for two negative numbers; endianness issues apply.

Floating-point arithmetic is subject to rounding that may affect the outcome of comparisons on the results of the computations.

Although negative zero and positive zero are generally considered equal for comparison purposes, some programming language relational operators and similar constructs might or do treat them as distinct. According to the Java Language Specification, comparison and equality operators treat them as equal, but Math.min and Math.max distinguish them (officially starting with Java version 1.1 but actually with 1.1.1), as do the comparison methods equals, compareTo and even compare of classes Float and Double.

Read more about this topic:  IEEE 754-1985

Famous quotes containing the words comparing and/or numbers:

    We cannot think of a legitimate argument why ... whites and blacks need be affected by the knowledge that an aggregate difference in measured intelligence is genetic instead of environmental.... Given a chance, each clan ... will encounter the world with confidence in its own worth and, most importantly, will be unconcerned about comparing its accomplishments line-by-line with those of any other clan. This is wise ethnocentricism.
    Richard Herrnstein (1930–1994)

    Publishers are notoriously slothful about numbers, unless they’re attached to dollar signs—unlike journalists, quarterbacks, and felony criminal defendents who tend to be keenly aware of numbers at all times.
    Hunter S. Thompson (b. 1939)