Feeds

Unequal equivalence

When is a number not a number?

  • alert
  • submit to reddit

Choosing a cloud hosting partner with confidence

kevlin henney headshotColumn In my previous column I put the contract for object equality under the microscope in most detail for Object.equals in Java but also with a brief look at Object.Equals in C#.

The idea that equality can also be assessed by relational comparison between two objects was also examined, looking again in most detail at the contract for Comparable.compareTo in Java and then briefly at some differences in IComparable.CompareTo in C#. Certain obvious similarities between the languages, in this respect, make their direct comparison easy.

However, one point that was mentioned briefly, but otherwise glossed over, deserves more attention. The idea of determining equality from a total ordering is essentially based on the idea that if two objects are neither greater than nor less than one another they can be considered equal, i.e. when compareTo in Java returns a value of zero rather than a positive or negative value. What may come as a surprise is that this does not necessarily — and is not required to — give the same measure of equality as the direct notion of equality comparison embodied in the equals method.

When are equivalent objects not equal?

The leeway on allowing equality according to equals to be inconsistent with equality determined by compareTo may seem strange at first, but there are cases when a strict total ordering and a notion of exact equality do not necessarily reach the same conclusion. Of course, the wording of Sun's JDK documentation strongly encourages the two concepts to be consistent, but that is not a hard and fast contractual binding.

And you don't have to look very far for an everyday example. In Britain, an alphabetically sorted listing of names, such as a telephone directory, should order names beginning "Mc" and "Mac" together and as the latter. However, although they are equivalent with respect to ordering there is no mistaking "McDonald" as equal to "MacDonald".

A similar case can be made for case-insensitive ordering of strings: "Mongoose" should order after "aardvark" but before "ZEBRA". However, "mongoose", "MONGOOSE" and "Mongoose" should be considered equivalent with respect to ordering even though they are not strictly equal.

A practical application of this concept can be seen in the syntax of identifiers in CORBA's Interface Definition Language. Because of its intended role as an interoperability standard, IDL cannot reasonably favour either case-sensitive naming, as found in the C family of languages, or case-free naming, as found in Pascal, Fortran and other languages. The compromise is to ensure that spelling must be unique — so "Mongoose" has the same spelling as "mongoose", and in a sorted table would map to the same place — but case is preserved — so "Mongoose" is not considered equal to "mongoose", so you cannot redeclare one as the other or use one as the other. This is a good compromise that works with both case-sensitive and case-free languages, and a middle path that should be considered by more language designers.

A further example can be found in the java.math package. In contrast to floating-point numbers, a BigDecimal holds a scaled, arbitrary precision integer, where the scale indicates the number of digits to the right of the decimal point. A strict interpretation of the notion of equality between two objects suggests that a representation of 2.5 (25 with a scale of 1) and one of 2.50 (250 with a scale of 2) are not truly equal, and therefore equals returns false. However, no matter what the representation, the values represented by BigDecimal are reasonably subject to a total and natural strict ordering. In that case, 2.5 is neither greater nor less than 2.50, so they are considered equivalent and compareTo returns 0.

A similar model exists in C++. Equality is expressed through the == operator and bound by the EqualityComparable requirements, which require conforming implementations of == to be reflexive, symmetric and transitive. Reasonably enough, inequality is normally considered to be defined with respect to equality, so a != b is equivalent to !(a == b). This is not just a good logical relationship: it's good implementation advice as well. Rather than duplicating the concept of equality comparison in both operator== and operator!= functions, it exists in only a single place and therefore, should it need to be changed to correct a defect or to modify representation, the change is needed in only one place. This leads to a more stable design with fewer hidden dependencies.

The C++ standard also defines LessThanComparable requirements to govern the < operator. To satisfy the LessThanComparable contract an implementation of the operator must define an ordering on its arguments and it must be irreflexive, i.e. !(a < a) must be true. Comparison in the C++ standard library is based solely on this operator. However, one would also expect — in the name of consistency, convention and reason [2, 3] — the other relational operators to be defined for a given type, and defined according to logical relationships in terms of operator< and not operator==, e.g. a <= b is defined as !(b < a) . Therefore, again, only a single piece of code defines the concept of ordering rather than having it subtly duplicated over four functions.

The LessThanComparable requirements are also used to define an equivalence relation along the lines of two values being considered equivalent if neither is less than the other, i.e. !(a < b) && !(b < a) . And, as we have seen with Java and C#, this notion of equivalence with respect to ordering does not imply consistency with equivalence defined in terms of EqualityComparable. Such consistency may be recommended but, as we have seen, making it a rule may sometimes be too strong an imposition.

Intelligent flash storage arrays

More from The Register

next story
Netscape Navigator - the browser that started it all - turns 20
It was 20 years ago today, Marc Andreeesen taught the band to play
Sign off my IT project or I’ll PHONE your MUM
Honestly, it’s a piece of piss
Return of the Jedi – Apache reclaims web server crown
.london, .hamburg and .公司 - that's .com in Chinese - storm the web server charts
Chrome 38's new HTML tag support makes fatties FIT and SKINNIER
First browser to protect networks' bandwith using official spec
UNIX greybeards threaten Debian fork over systemd plan
'Veteran Unix Admins' fear desktop emphasis is betraying open source
Admins! Never mind POODLE, there're NEW OpenSSL bugs to splat
Four new patches for open-source crypto libraries
Torvalds CONFESSES: 'I'm pretty good at alienating devs'
Admits to 'a metric ****load' of mistakes during work with Linux collaborators
prev story

Whitepapers

Forging a new future with identity relationship management
Learn about ForgeRock's next generation IRM platform and how it is designed to empower CEOS's and enterprises to engage with consumers.
Cloud and hybrid-cloud data protection for VMware
Learn how quick and easy it is to configure backups and perform restores for VMware environments.
Three 1TB solid state scorchers up for grabs
Big SSDs can be expensive but think big and think free because you could be the lucky winner of one of three 1TB Samsung SSD 840 EVO drives that we’re giving away worth over £300 apiece.
Reg Reader Research: SaaS based Email and Office Productivity Tools
Read this Reg reader report which provides advice and guidance for SMBs towards the use of SaaS based email and Office productivity tools.
Security for virtualized datacentres
Legacy security solutions are inefficient due to the architectural differences between physical and virtual environments.