Floating Point again again

Content:

Original link: I can prove 2x2 is 5 / LinkedIn.

It does print 5.

Demo and explanation:

public class TwoTimesTwo {
    public static void main(String[] args) {
        double c = -9007199254740992L;
        var r = c - 1 + 1 - c + (2 * 2);
        System.out.println((int)r);
        System.out.printf("%016X = %f\n", Double.doubleToRawLongBits(c), c);
        System.out.printf("%016X = %f\n", Double.doubleToRawLongBits(c - 1), c - 1);
        System.out.printf("%016X = %f\n", Double.doubleToRawLongBits(c - 1 + 1), c - 1 + 1);
        System.out.printf("%016X = %f\n", Double.doubleToRawLongBits(c - 1 + 1 - c), c - 1 + 1 - c);
    }
}
output:
5
C340000000000000 = -9007199254740992.000000
C340000000000000 = -9007199254740992.000000
C33FFFFFFFFFFFFF = -9007199254740991.000000
3FF0000000000000 = 1.000000

Comments: