About Question com.enthuware.ets.scjp.v6.2.742 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Pierluigi
Posts: 8
Joined: Thu Feb 23, 2012 5:33 am
Contact:

About Question com.enthuware.ets.scjp.v6.2.742 :

Post by Pierluigi »

Hello,
I'd like to know more about the implicit casting occuring when converting from an "int" type to a "float" type. The book I'm studying on for the exam does not mention anything about the significant digits of a "float" type variable, it only states that an int type can be implicitly cast to a float type since it hasn't a greater bit length than float, so its value can "fit" in a float variable.
The Explanation of this question only says that "values of type float are not precise to nine significant digits".

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.742 :

Post by admin »

This is not required for the exam but if you really want to understand the how and why of casting between int and double/float, you should go through this : http://en.wikipedia.org/wiki/IEEE_754-1985

Basically, when you store a float/double in 32/64 bit space, you use some bits to store the starting digits of the actual number and some bit to store the exponent. While in case of int/long, you use all the bit to store the digits of the number and there is no exponent. Hence, you are able to store large numbers in float/double but at the cost of precision. For example, instead of storing all the digits of say 1234567890, you are storing, 1.2 and then 9, which means 1.2 X 10**9. (Of course, this actually happens in binary and not in decimal ). So you have lost the least significant digits 3456789. If you stored it in a int/long, you would store all the digits.

In case of smaller numbers (up to the point where the number of bits used by float are enough to store all the digits of a given number), both float and int will have the same value. But as you go for bigger numbers, int cannot store it because it doesn't have enough bit to store all the digits. float can store it but it will lose some of the digits.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Pierluigi
Posts: 8
Joined: Thu Feb 23, 2012 5:33 am
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.742 :

Post by Pierluigi »

admin wrote: In case of smaller numbers (up to the point where the number of bits used by float are enough to store all the digits of a given number), both float and int will have the same value.
...and that's just what I was aiming at: how do I know whether the integer value I'm casting to float will retain all of its precision after the cast ? What should I know to answer this question correctly ?
If I'm not wrong, isn't that point (i.e.the greatest integer value that, if cast to a float, will not lose any of its precision) the only thing you have to know of in order to correctly answer the question v6.2.742 ? If the value you are casting to float is strictly greater than that "boundary" integer, then you know for sure that your value will definitely lose precision, and that the answer will be "It will not print 0"; else, the answer will be "It will print 0".
Sorry, maybe I'm missing some crucial information about the "float" type or I'm confusing things, but unless I know that point I can't tell if an integer will lose precision after a cast to float ...

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.742 :

Post by admin »

Sorry, I understand your question now.

I don't think you need to know this for the exam. In fact, I think this question should be eliminated.
If you like our products and services, please help us by posting your review here.

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.742 :

Post by admin »

But the answer to your question is Java float uses 23 bits for precision and (32-23 = 9) bits for exponent. Therefore, any number that requires bits lower than 23rd bit to be represented in binary, will lose that bit.

For example, lets say your number is 1000000001. This is (1 x 10^9) + 1). The binary representation is 111011100110101100101000000001

Now, float will keep first 23 bits (111011100110101100101) but will lose last 9 bits. Therefore, it will essentially store 111011100110101100101000000000.
You can see that the last 1 has been lost.

So the point is, the limit is not the magnitude of the number but the precision required to store the number. float is unable to store 1000000001 but it will easily store a much higher number 2000000000 without any loss because 2000000000 does not require lower bits anyway.

But again, this is not required for the exam and this question should be eliminated.
If you like our products and services, please help us by posting your review here.

Pierluigi
Posts: 8
Joined: Thu Feb 23, 2012 5:33 am
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.742 :

Post by Pierluigi »

Thank you for your explanation. It helped me recall this topic I studied years back. Although this is not required for the exam I find it interesting and I think it makes you understand better how math actually work inside machines.

Tester
Posts: 34
Joined: Mon Oct 30, 2023 11:55 am
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.742 :

Post by Tester »

I think, its vivid example:

Code: Select all

      int i = 1234567890;
      float f = i;
      var c = (int)f;
      System.out.println(i);
      System.out.println(c);

Post Reply

Who is online

Users browsing this forum: No registered users and 69 guests