Consider the following two codes in Java:
The output is true in 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 5.25. Binary representation of integral part is 101 and binary representation of part of dot is 0.01 (needs only two bits)
Let us consider second example 5.1. Binary representation of integral part is same. But binary representation of 0.1 is 1/16 + 1/32 + 1/64 + 1/128 ….. and so on until we reach end of mantissa or sum becomes more than 0.1. In this case we reach end of mantissa and therefore value of 5.1 becomes different in float and double.
- How to get slice of a Primitive Array in Java
- Widening Primitive Conversion in Java
- Primitive Wrapper Classes are Immutable in Java
- Is an array a primitive type or an object in Java?
- Program to convert Primitive Array to Stream in Java
- Java.Lang.Float class in Java
- Float floatValue() in Java with examples
- Float longValue() in Java with Examples
- Float shortValue() method in Java with examples
- Float equals() method in Java with examples
- Float doubleValue() method in Java with examples
- Float compareTo() method in Java with examples
- Float compare() Method in Java with Examples
- Float floatToIntBits() method in Java with examples
- Float isNaN() method in Java with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.