• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

What will this return

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
float f = 1.0f/3.0f;
if (A*3.0f == 1.0f) return true;else return false
What will be the answer?
Velu notes say it should return false but if you compile it returns true.
What should we say in the real exam if a question like this comes.
Thanks
Mahesh
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you are right, the code given will return true. (assuming that it declare float A, instead of float f)
This is correct answer, In velu's note it might have given in different context or mat be wrong.
 
Mahesh Bansal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know this. Can any of the senior guys give their inputs please on what should be the answer in exam false / true ?
Mahesh
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In case of arithmetic operations both the operands are converted to int before the operation.so, in this case both the float values will be converted to int resulting in the loss of presicion.
hence the both are not equal.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First I don't think this type of question will be asked in the real exam.
Second the answer is true.
1/3.0f results in 0.33333334. then timess 3.0f results in 1.00000002xxx and ... you know the answer.
--Howard
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rajani: Actually, operands are converted to int only if neither expression contains a long, float, or double. Since the expression here contains floats, they are left as floats. The general rule is, (to plagiarize the esteemed Mr. Brogden):
If either operand is double, the other converts to double, else
if either operand is float, the other converts to float, else
if either operand is long, the other converts to long, else
both convert to int.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a good question on precision. Try this cpde
class floatdouble{



public static void main(String[] args){


double f = 1.0f/9.0f;

if (f*9.0 == 1.0f)
System.out.println(" equal ");
else
System.out.println(" false ");

}

}

You will get false. Because 1.0f/9.0f is cast to a double. (Precision is increased!!). But when you do f*9.0f, it is cast to a double (precision increased) and the result is compared with a float (whose precision is also increased here), but it will not match with the precision in the LHS.
 
Honk if you love justice! And honk twice for tiny ads!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic