Issue
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?
Solution
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
Answered By - J Asgarov
Answer Checked By - Clifford M. (JavaFixing Volunteer)