jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes int x = (int)floatNumber Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "int x = (int)floatNumber" Watch "int x = (int)floatNumber" New topic
Author

int x = (int)floatNumber

Gagan Deep
Ranch Hand

Joined: Aug 23, 2005
Posts: 47
Q44.What will be the result of compiling and running the given program?
Select one correct answer.


1. Prints 0 and true.
2. Prints 0 and false.
3. Prints some number (other than 0) and true.
4. Prints some number (other than 0) and false.

Choice 4 is correct.It will print some number (other than 0) and false due to the loss of precision.

If intNumber =4 or any small number than answer 3

Please explain
[ January 01, 2006: Message edited by: Mark Spritzler ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Well, my question to you would be how big of a number can an int hold. and if it is larger than what it can hold, what happens.

Also, there are ten buttons below the Add Reply button. One of them is the CODE button which you can use to post code and keep its formatting, so that we can read the code easier.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Dave Butterwick
Greenhorn

Joined: Dec 30, 2005
Posts: 9
Gagan,
This is more about the limitations on the way a float is stored than it is about the limitations on an int I believe. If I remember correctly a float is stored in 32 bits as a characteristic and a mantissa with each alotted a certain number of bits.

Now if you try that code with a double instead of a float I think it will retain the precision when you go to reassign it back to an int. A double will alot more bits for the mantissa and characteristic.


Conformity is the jailer of freedom and the enemy of growth.
Gagan Deep
Ranch Hand

Joined: Aug 23, 2005
Posts: 47
Mark

Range for int is -2,147,483,648 to 2,147,483,647 & the number which i am assinging 123,456,789 to int very variable is very much with in this range. I don't know why you had asked me this ? Did you want to suggest anything else?
Also thanks for suggestion on how to keep the formating of the code. i did nt know that from now i will use these buttons.

Dave,
i got your point but any idea after what number it starts give this issue. Means till what number "System.out.println(intNumber == x );" will return 0.

Any one has more suggestion on this ?

Thanks
Gagan.
Dave Butterwick
Greenhorn

Joined: Dec 30, 2005
Posts: 9
That's the rub. It really depends how a float/double is stored. As I understand the requirements of the test we don't have to know how floats are stored. We do have to know their range of values. I have no clue at what number this starts to become a problem for floats or doubles. If this is a question on the test I'm calling my congressman.
Luciano Leite
Greenhorn

Joined: Mar 01, 2006
Posts: 15

Hi!

System.out.println(x); // at line 8 Prints 123456792 (nearest int divisible by 8)

For intNumber = 123456792, 1234567800, 1234567808... line 8 prints 0 and line 9 true

at line 7 floatNumber is round... why? I don't know

from what range float has this behavior?
John Russell
Greenhorn

Joined: Feb 07, 2006
Posts: 17
Originally posted by Luciano Santana Leite:

from what range float has this behavior?


If my analysis is correct, floats can exactly represent every int in the range -16777215 to +16777215. Outside that range they are susceptible to rounding error.

In a float, you have 32 bits. One bit is used for sign information. 8 bits are used for exponent information. The remaining 23 bits are used for mantissa information. 16777215 is the largest 23-bit unsigned integer.

For further study, you can have a look at the J2SE javadoc for the Float class, particularly the intBitsToFloat method. Also, you can play with this educational toy...



Sample run:

John Russell
Greenhorn

Joined: Feb 07, 2006
Posts: 17
Hold the phone...my analysis was sloppy. The largest 23-bit unsigned integer is 8388607. So, floats can exactly represent integers in the range -8388607 to +8388607. Outside that range they are susceptible to rounding error (like me.)
Luciano Leite
Greenhorn

Joined: Mar 01, 2006
Posts: 15

Tks John, don't worry... your explanation was perfect "32bits... left 23"
Krishnamoorthy Sethuraman
Greenhorn

Joined: Dec 12, 2005
Posts: 24
The first integer at which assigning to float and casting back to int loses precision is : 16777217. The following code when run, gives the output as:
=========================================================================

=========================================================================
i = 16777217
i = -16777217

Floating points are first converted to form : N = � (1.b1b2b3b4 ...)2 x 2+E
The left most one is not stored(always 1). The bits b1b2b3 etc.,(upto 23 bits) is what goes into the mantissa part. Hence, the first maximum bit for an integer can be 24 successive one's. (23 One's for mantissa and one implicit 1). 24 successive one corresponds to the number : 16777215. The next number is 16777216 for which the bit pattern is : 1000000000000000000000000
ie., one followed by 24 zero. So, even though the 24th zero is not stored,
precision is not lost. Because when we convert back it is restored. So, the maximum integer without losing precision that can be converted back from float is now : 16777216

Now, if we consider the next number (16777217) for which the bit pattern is : 1000000000000000000000001. This has 1 followed by 23 zeros and then a 1. Because of 1 in the 24th bit, the 23rd bit will be rounded to 1 and the first precision loss comes for this number. Thus, the number 16777217 is the first
integer number which when converted to float and converted back to integer will lose the precision as proved by the output of the above code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: int x = (int)floatNumber
 
Similar Threads
How is huge Integer value converted to float
Type Of Question in Exam
Plz clarify
transient
jtips mock1 #14