Consider the following two codes in Java:
The output is true in the first example and false in second. We know that precisions of float and double are different. Size of mantissa for float is 24 and 53 for double.
Let us consider the first example of 5.25. Binary representation of integral part is 101 and binary representation of part of the dot is 0.01 (needs only two bits)
Let us consider the second example 5.1. The binary representation of an integral part is the same. But binary representation of 0.1 is 1/16 + 1/32 + 1/64 + 1/128 ….. and so on until we reach the end of mantissa or sum becomes more than 0.1. In this case, we reach the end of mantissa and therefore the value of 5.1 becomes different in float and double.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.