-1

Question maybe very simple. Checked the equality of two Primitive types, then got some mistakes. One of the double, second one long.

public class TesterPrimitive {

public static void main(String[] args) {
    System.out.println("Equality - " + (5.0 == 5)); // Return true
    System.out.println("Equality - " + (5.000000000000001D == 5L)); // Return false
    System.out.println("Equality - " + (5.0000000000000001D == 5L)); // Return true
}}

Why for third equality, for double and long, I got true? Is it means, long 0 numbers after (.), change any value to absolute 0? Or is it changing bits and then we can get 5.0 for double?

Yeras_QazaQ
  • 77
  • 11

2 Answers2

2

As you can see here https://www.geeksforgeeks.org/difference-float-double-c-cpp/ double has exactly 15 digits precision. 16th digit gets rounded.

Another thing that is perhaps important to understand - when performing operations on different primitives like here (5.0 == 5), int gets promoted to double for the sake of equality operation. More here https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html

J Asgarov
  • 2,526
  • 1
  • 8
  • 18
0

double variables have a precision of about 15 digits, which is enough for most purposes.double data type can occupy 8 bytes of space in the computer memory and stores 15 to 16 numbers after the decimal point, you can check this article, hope this will help you