aspose file tools*
The moose likes Beginning Java and the fly likes jtips mock1 #14 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "jtips mock1 #14" Watch "jtips mock1 #14" New topic
Author

jtips mock1 #14

Hung Chang
Greenhorn

Joined: Oct 21, 2001
Posts: 16
i need help with this question.
14. Given:
class Q14 {
public static void main(String[] args) {
int intNumber = 123456789;
float floatNumber = intNumber;
int x = (int)floatNumber;
System.out.println(intNumber - x);
System.out.println(intNumber == x );
}
}
Choose 2 answers.
1.Prints 0 and true .
2.Prints 0 and false.
3.Prints (some number due to loss of precision) and true.
4.Prints some integer due to loss of precision and false.
when it compiles, it prints ans 4.. but when i change intNumber =2147483648 or other numbers, it compiles and prints ans 1.
can anyone explain it to me plz.. thanx in advance
Argm Mastoi
Ranch Hand

Joined: Oct 16, 2001
Posts: 35
hi,
This program is always printing option 4. whether i changed the values also.
i used this code to see what actually is going on :
class Q14 {
public static void main(String[] args) {
int intNumber = 213456789;
float floatNumber = intNumber;
int x = (int)floatNumber;
System.out.println("floatNumber " +floatNumber);
System.out.println("intNumber" +intNumber);
System.out.println("x" +x);
System.out.println(intNumber - x);
System.out.println(intNumber == x );
}
}
Hung Chang
Greenhorn

Joined: Oct 21, 2001
Posts: 16
hi,Argm
it doesnt always print ans4.
if i pick x=5.. it prints ans4
Hung Chang
Greenhorn

Joined: Oct 21, 2001
Posts: 16
sorry.. ans1
Argm Mastoi
Ranch Hand

Joined: Oct 16, 2001
Posts: 35
have u checked that what's actually going on inside, the way i told you
Argm Mastoi
Ranch Hand

Joined: Oct 16, 2001
Posts: 35
also this ans4 is for this perticular value given in the mock not for all values :-)
Michael Bruesch
Ranch Hand

Joined: Sep 23, 2001
Posts: 158
just as Argm did, I put in a few extra print lines to see what was actually going on. When the intNumber is cast up to a float there is a slight loss of precision. floatNumber actually become 1.23456792E8 when assigned the cast value. When it's cast back to int x, x becomes 123456792. When you subtract these numbers, it does not give 0, and the second part is false. It is because of this loss of precision from casting from intNumber up to floatNumber that the answer is not exactly what you may expect it to be.
------------------
Michael J Bruesch
Codito, ergo sum...
I code, therefore I am.
My Java Games, I'm quite proud


Michael J Bruesch<br /><i>I code, therefore I am.</i>
Akshayan Venkatesh
Greenhorn

Joined: Apr 13, 2006
Posts: 26
Originally posted by Hung Chang:
i need help with this question.
14. Given:

class Q14 {
public static void main(String[] args) {
int intNumber = 123456789;
float floatNumber = intNumber;
int x = (int)floatNumber;
System.out.println(intNumber - x);
System.out.println(intNumber == x );
}
}
Choose 2 answers.
1.Prints 0 and true .
2.Prints 0 and false.
3.Prints (some number due to loss of precision) and true.
4.Prints some integer due to loss of precision and false.

when it compiles, it prints ans 4.. but when i change intNumber =2147483648 or other numbers, it compiles and prints ans 1.
can anyone explain it to me plz.. thanx in advance
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jtips mock1 #14