This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
public class Test { public static void main(String[] args) { double d = Integer.MAX_VALUE; float f= Integer.MAX_VALUE; if (f == d) System.out.println("equal"); else System.out.println(" not equal "); } } it prints not equal however if i take float f = 10; and double d =10 its shows equal please explain thanks NAveen
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
Originally posted by Naveen Sharma: public class Test { public static void main(String[] args) { double d = Integer.MAX_VALUE; float f= Integer.MAX_VALUE; if (f == d) System.out.println("equal"); else System.out.println(" not equal "); } } it prints not equal however if i take float f = 10; and double d =10 its shows equal please explain thanks NAveen
Add lines:
which should give you an idea why... ------------------ Antti Barck It Solutions Consultant, NSD Oy
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12268
1
posted
0
Although both float and int use 32 bits, float has to use some bits for the exponent, therefore it can't maintain the low significance bits in Integer.MAX_VALUE. However, double with 64 bits to play with can. Bill ------------------ author of:
In simple terms double becomes more precise with 64 bit info than that of float with 32 bit. Hence whenever u do a calculation on RHS and assign the same to double and float they won't be equal. Java Gurus pls correct me if the interpretation is wrong.