Is there a way that double d + 1 == d in java

Viewed 208

I'm just curious if there's a way that d + 1 == d. If d is a double. Maybe someone can share some knowledge with me.

2 Answers

Yes, certainly.

class Main {  
  public static void main(String args[]) { 
    System.out.println(1.0e52 + 1); 
  } 
}

prints

1.0E52

This will actually be true for numbers with absolute value at least 253, because the mantissa of a double has 52 bits of precision.

Absolutely. For very large (or small) doubles (or Double.POSITIVE_INFINITY), floating point precision won't be enough to store the 1 difference and d + 1 == d will be true.

Related