Question ID :958083608604 The following code will print 'false'. True or False float f = 1.0F/3.0F; System.out.println( (f * 3.0F) == 1.0F ); The given answer is False! I thought it was true, since 1.0/3.0 is given by 0.333334, an approximation which when multiplied by 3.0 does NOT yield 1.0. Also, after reading the explanation, one feels that this can be extended to a f even if were declared as double. However the explanation given by JQ+is follows:
If you add System.out.println( f ); and System.out.println( f *3.0f ); You'll see, 0.33333334 1.0 Note that. actually 1/3 is .33333333..... but it shows f as .33...4, So f*3 is 1.00...X (a little more than one.) but it is too precise to be represented by a float so we get 1.0. So, finally the code prints true. Had f been a double, it would have printed false. The reason is how floating point numbers are stored in the memory. The point to understand here is precision is different than capacity. A float can store a very large number but it stores it in a different format. Eg. consider a number having 100 digits: 123456789................234..........234 Now, in memory it the whole hundred digits are not stored. What is stored is something like 1.23456 * 10 ^ 10 Note that, the digits after a certain position are lost because when you convert the floating point number back to integral form it will be.. 12345600000000.........00000..........000. This is what is meant by precision. Now, why double can store more precisely than float is simple. Because as the bytes used by a double are more than float, double can give more space to store the digits of the number. So it'll be something like: 1.23456789 x 10 ^ 10. Note that, double is storing the digits up to 9, and float stored only upto 7. So you can see that double is more precise than float.
Hi Rahul, Thanks for sharing the question. Though, I didnt quite get what was the question you wanted to ask. Cheers, Ravindra Mohan. ------------------
Joined: Feb 27, 2001
Hi Ravindra, What I wanted to say is that Float f1 = (1.0f/3.0f); Float f2 = 3.0f; Float f3 = f1*f2; boolean b=(f3==1.0); System.out.println(b); I think that the value false will be printed. JQ+ thinks otherwise! Thanks Rahul P.S. This is what I remember seeing in some other sites and books too.
Joined: Mar 16, 2001
Hi Rahul, You are right I too am getting the same result with my compiler, i.e., the result indeed prints "true" for both float as well as double. May be it is an errata of the JQ+plus mock test. Please keep me posted regarding any clarification on this. Ravindra Mohan.
The given answer and explanation are correct. It prints true for float and false for double (atleast on JDK1.2.2) Are you sure you ran the exact code given above? -Paul. ------------------ Get Certified, Guaranteed! (Now Revised for the new Pattern) www.enthuware.com/jqplus
Hi Paul, I did ran the code in jdk1.2.2 and the code is as follows:
Hope this is fine now. Regards, Ravindra Mohan.
Enthuware Software Support
Joined: Sep 23, 2000
But that's not what the question says. Original was: float f = 1.0F/3.0F; System.out.println( (f * 3.0F) == 1.0F ); //This prints true. If you change f to double it will be: double f = 1.0F/3.0F; System.out.println( (f * 3.0F) == 1.0F ); //And this prints false.
If you look at the following lines you'll see why... double f = 1.0F/3.0F; System.out.println(f); //prints 0.3333333432674408 double d = 1.0/3.0; System.out.println(d); //prints 0.3333333333333333 -Paul. ------------------ Get Certified, Guaranteed! (Now Revised for the new Pattern) www.enthuware.com/jqplus