About Question enthuware.ocajp.i.v7.2.958 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.958 :
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.
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.
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.958 :
You might want to check this out.
Although, you are not required to know about this for the purpose of the exam.
HTH,
Paul.
Although, you are not required to know about this for the purpose of the exam.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.958 :
Well I think it's because if you do not use the typecast. The operation will automatically convert in float rather than 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
Code: Select all
(i - f)
Code: Select all
i
Code: Select all
f
The_Nick
Re: About Question enthuware.ocajp.i.v7.2.958 :
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
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.
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);
Thanks for the response in advance.
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.958 :
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.
NOTE: This is NOT required for the exam.
If you like our products and services, please help us by posting your review here.
-
- Posts: 22
- Joined: Wed Aug 21, 2013 12:29 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.958 :
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.
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.
-
- Posts: 28
- Joined: Mon Sep 25, 2017 8:16 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.958 :
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.
The topic is confusing and inconsistent.
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.958 :
Not sure why you think so. Can you show an example where assigning an int to float causes loss of precision?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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 28
- Joined: Mon Sep 25, 2017 8:16 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.958 :
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.
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.
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.958 :
Good point.
Paul.
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 7
- Joined: Tue Apr 16, 2019 5:40 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.958 :
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 .
-
- Posts: 32
- Joined: Mon Dec 29, 2014 4:14 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.958 :
Are this kind of question really on exam? because in the same time a greater int value result 0
what should we watch out for?
Code: Select all
int i =2_000_000_000;
float f = i;
System.out.println(i -(int)f);
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.958 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Thu Sep 03, 2020 5:10 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.958 :
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"?
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"?
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.958 :
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.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 160 guests