Page 1 of 1

About Question enthuware.ocajp.i.v7.2.958 :

Posted: Tue Dec 11, 2012 12:33 pm
by ETS User
Hi,
In this eg. when I try
System.out.println(i - f); i.e w/o the typecast, the value returned is 0.0

But when I do try it as (i-(int)f) it returns -46, so can you help me understand whats happening?
Thanks.

Re: About Question enthuware.ocajp.i.v7.2.958 :

Posted: Tue Dec 11, 2012 7:20 pm
by admin
You might want to check this out.

Although, you are not required to know about this for the purpose of the exam.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.958 :

Posted: Wed Dec 12, 2012 12:03 pm
by Guest
Thanks Paul!

Re: About Question enthuware.ocajp.i.v7.2.958 :

Posted: Sat Mar 02, 2013 5:35 am
by The_Nick
Well I think it's because if you do not use the typecast. The operation

Code: Select all

(i - f)
will automatically convert

Code: Select all

i
in float rather than

Code: Select all

f
in int. Therefore it will be something like "IntegerWithLossPrecision - IntegerWithLossPrecision" since the loss of precision it's regular and not random it will give back 0.0 as result.

The_Nick

Re: About Question enthuware.ocajp.i.v7.2.958 :

Posted: Sat Mar 02, 2013 6:15 am
by The_Nick
Hi,
Do you know exactly how many digits does the floating point support ( I am not talking about interpolating).
I read in the link above mentioned that is 7. but actually also

Code: Select all

int i = 12345678 float f = i; System.out.println(i-(int)f);
Gives back 0. So it is 8 or there is a fixed number of digits that we can be sure it is supported by the typecasting from float towards int.

Thanks for the response in advance.

Re: About Question enthuware.ocajp.i.v7.2.958 :

Posted: Sat Mar 02, 2013 10:22 am
by admin
Yes, there is a fixed number of bits (not digits) that are used to store the most significant digits of a number. You need to go through Section 4.2.3 on JLS: http://docs.oracle.com/javase/specs/jls ... jls-4.html

NOTE: This is NOT required for the exam.

Re: About Question enthuware.ocajp.i.v7.2.958 :

Posted: Fri Nov 08, 2013 6:43 pm
by javanaut
Anyways...

It prints 0.0 which is basically 0 if the explicit cast is not inserted.

This question is tough and I still do not fully grasp when the loss of precision will occur even after reading the stackoverflow article. :( :shock:

Re: About Question enthuware.ocajp.i.v7.2.958 :

Posted: Mon Sep 25, 2017 2:27 pm
by JuergGogo
Normally the compiler complains about a possible loss of precision. For example a conversion from float to int needs an explicit cast. In the opposite direction - int to float, or long to double - there also could be a possible loss of precison and we don't need an explicit cast: float f = i;
The topic is confusing and inconsistent.

Re: About Question enthuware.ocajp.i.v7.2.958 :

Posted: Tue Sep 26, 2017 10:08 am
by admin
JuergGogo wrote:In the opposite direction - int to float, or long to double - there also could be a possible loss of precison and we don't need an explicit cast: float f = i;
The topic is confusing and inconsistent.
Not sure why you think so. Can you show an example where assigning an int to float causes loss of precision?

Re: About Question enthuware.ocajp.i.v7.2.958 :

Posted: Mon Oct 02, 2017 12:05 pm
by JuergGogo
https://stackoverflow.com/questions/119 ... -float-why

In summary they say, the range matters not the precision. So implicit casting from int to float may result in loss of precision, but float has the higher range than int.

Re: About Question enthuware.ocajp.i.v7.2.958 :

Posted: Mon Oct 02, 2017 11:48 pm
by admin
Good point.
Paul.

Re: About Question enthuware.ocajp.i.v7.2.958 :

Posted: Tue Apr 16, 2019 5:45 pm
by vilasa
For float I understood from above explanation that 9 digit or greater value of int assigned to float causes loss of precision . Please give example of long to float and long double where loss of precision occurs .I am trying couple of values for long to double it prints 0 .

Re: About Question enthuware.ocajp.i.v7.2.958 :

Posted: Thu Jul 16, 2020 4:22 am
by Dreamweaver
Are this kind of question really on exam? because in the same time a greater int value result 0

Code: Select all

        int i =2_000_000_000;
        float f = i;
        System.out.println(i -(int)f);
what should we watch out for?

Re: About Question enthuware.ocajp.i.v7.2.958 :

Posted: Thu Jul 16, 2020 4:46 am
by admin
You may not get exactly this but yes, you may get questions that require you to understand comparison of float and int for whole numbers.

Re: About Question enthuware.ocajp.i.v7.2.958 :

Posted: Thu Sep 03, 2020 5:12 pm
by anam_m
I am confuse about the choices that are given for this question.
Actual answer is -46, and it is not one of the options, so when I chose none of the above, why is it telling that correct answer is "It will not print 0"?

Re: About Question enthuware.ocajp.i.v7.2.958 :

Posted: Thu Sep 03, 2020 10:49 pm
by admin
Well, you need tp select the best option. Out of "None of the above" and "It will not print 0", "It will not print 0 is better. It is also factually correct.