next up previous
Next: Strength reduction Up: Simplification or optimization Previous: Trichotomy

Equality

The existence of NaNs means that using memory equality for numeric equality is not valid. That is, one cannot assume that a single memory pattern is numerically equal to itself. Programming languages that use pointers to objects (Java, C, Lisp) must generally distinguish between numerical equality and object isomorphism. Lisp has several predicates: eq, equalp, =.

The existence of signed zeros means that two memory patterns that are different may nevertheless be numerically equal.

What does it mean to compare two numbers of different precision?

Mathematica says: two numbers are equal if they differ in about 2 decimal digits of the precision of the least precise of the two numbers.

There is a folk rule that you should not compare floats for equality. But making it impossible to compare them is unhelpful.

(for example, there is no problem in rapid comparisons of integers that are exactly representable as floats;)

It is hard to know what use can be made from arithmetic that behaves as given below

m=SetPrecision[123.0,5]
m1=m+1/10
m3=m+1/10000000000000000


m==m1==m3 is True

m1> m   is True     m3> m  is False
m1>=m   is True     m3>=m  is True 
m1==m   is True     m3==m  is True
m1=!=m  is True     m3=!=m is False
m1===m  is False    m3===m is True
m1<=m   is False    m3<=m  is True
m1< m   is False    m3< m  is False
m-m1==0 is False    m-m3==0 is True

m1^2-m^2==24.61   is True
m1^2-m^2==30      is True
m1^2==m^2         is True
But it is part of the Mathematica design, based on a bad idea called Significance arithmetic.



Richard J. Fateman
Thu Aug 13 13:55:33 PDT 1998