• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with Conversion

 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Problem

Consider the following code:



Options

Select 1 correct option.



Ans: It will not print 0.

Doubt: how is that happening? I compiled and checked it is giving -46, but how?

Source: (Question ID: 1118) Enthuware

Thanks,
Gitesh
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try the following:
int i = 1234567890;
float f = i;
System.out.println(i);
System.out.println(f);
System.out.println((int)f);

output: 1234567890
1.23456794E9
1234567936

When you do System.out.println(i-(int)f) it will print -46.Since the value of the integer falls out of the range of Integers, it looses its precision from widening and shortening conversions.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I figured this one out

1234567890 in binary = 1001001100101100000001011010010
truncated to fit 8-bit exponent for floating point representation = 11010010
negative so flip bits and add 1 = 00101110

et voila -46.

Is this correct? I'm studying for SCJP5 so I haven't covered this stuff in any detail this is an educated guess...
 
Paul Brabban
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get it thanks Sri Karr
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My concern here is that, how to get the value of f, without compiling?

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

output: 1234567890
1.23456794E9
1234567936

Any help?

Regards,
Gitesh
 
Sri Karr
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You do not need to worry about the exact answer for this. As the exam doesn't test our mathematical skills.
We knew that when we convert a large integer (which has the value out of its range -32768 to 32767)to float the value is going to change slightly.And when we convert the float to integer for the same it is going to loose its precision.(Narrowing conversion).

When we look at the answers for the above question:

1) It will print 0.


2) It will not print 0.


3) It will not compile.


4) It will throw an exception at runtime.


5) None of the above.

we knew that definitely it won't print "0" as the number is large, the conversion changes the number slightly, eliminate 1st option, the code is perfectly fine, so eliminate 3 and 4 th options. You need to choose from options 2nd and 5th.

You knew that if code compiles and doen't throw runtime exception, it will print some number and in this case it won't be "0".so please select 2nd as your answer.
(I guess this question is kind of logical thinking question rather than pure technical questions).
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sri Karr
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic