Consider the following two codes in Java:
// This program prints true class Geeksforgeeks { public static void main(String args[]) { float f = 5.25f; double d = 5.25 System.out.println(f == d); } }
Output
true
// But this program prints false. class Geeksforgeeks { public static void main(String args[]) { float f = 5.1f; double d = 5.1; System.out.println(f == d); } }
Output
false
The output is true within the first example and false in second. we all know that precisions of float and double are different. Size of mantissa for float is 24 and 53 for double.
Let us consider the primary example of 5.25. Binary representation of integral part is 101 and binary representation of a 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 that the same. But binary representation of 0.1 is 1/16 + 1/32 + 1/64 + 1/128 ….. then on until we reach the top of mantissa or sum becomes quite 0.1. during this case, we reach the top of mantissa and thus the worth of 5.1 becomes different in float and double.
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.