Q:

When does (1.0/x)*x - 1.0 not evalute to 0.0?

I'm searching for an example when the expression
(1.0/x)*x - 1.0

does not evaluate to 0.0, assuming that x is a double value in Java.
I'm also interested in a reason for that.

A:

Some obvious examples include:

x = 0.0
x = Double.NaN
x = Double.POSITIVE_INFINITY
x = Double.NEGATIVE_INFINITY

Less obvious examples:

x = 0x1p-1050 (a denormalized double)
x = -1.0 / Double.POSITIVE_INFINITY (negative zero)

There may be examples where there occurs loss of precision, but I am yet to find one.

A:

Here's a program that will list all values for which this is the case.
public static final void main(String[] args){
    for(long i=Long.MIN_VALUE; i<Long.MAX_VALUE; i++){
        double d = Double.longBitsToDouble(i);
        if(0.0d != (1.0d/d)*d - 1.0d){
            System.out.println(d);
        }
    }
}

Turns out there's lots and lots and lots of them - the program starts with a large range of denormalized numbers, and it's the case for all of them.
To get numbers in a more familiar range:
public static final void main(String[] args){
    for(long i=Double.doubleToLongBits(1.0d); i<Long.MAX_VALUE; i++){
        double d = Double.longBitsToDouble(i);
        if(0.0d != (1.0d/d)*d - 1.0d){
            System.out.println(d);
        }
    }
}

Still lots and lots of hits.
The reason? double has limited precision, and therefore it's fundamentally unable to represent all numbers. If your calculation has an intermediate result that cannot be exactly represented by  double, your algebraic equalities cannot be expected to hold.

