|
-
Dec 10th, 2006, 11:08 PM
#1
Thread Starter
PowerPoster
[RESOLVED] Simple Casting
Hi all,
I do some testing on casting and confused with followings. First check the given code.
Code 1,
Code:
int x = 10;
float y = 15.2f;
int z = x + (int)y;
System.out.println(z);
Code 2,
Code:
int x = 10;
float y = 15.2f;
System.out.println(x + y);
Code 3,
Code:
int x = 10;
float y = 15.2f;
int z = x + y;
System.out.println(z);
My explanations on these are as follows.
On code one try to printout z, is an int type. So that it’s need to casting float into int. Result is 25. It’s ok.
On code two result is 25.2 because on println() method if any variable in a group of concatenated variables is a string, Java gets it as a single String.
On the last code I’m confusing. It is wrong. Why it is not possible automatic casting since int and float are 32bits long.
Can you guys make a note on this.
Thanks.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Dec 11th, 2006, 04:36 AM
#2
Re: Simple Casting
I imagine that example three causes a loss of precision but you as the developer have not explicitly stated that such a loss is ok.
-
Dec 11th, 2006, 08:18 AM
#3
Re: Simple Casting
Bit size has nothing do with it. float is a floating point number, which means that any conversion between it and int can result in loss of precision.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 12th, 2006, 02:14 AM
#4
Thread Starter
PowerPoster
Re: Simple Casting
 Originally Posted by DeadEyes
I imagine that example three causes a loss of precision but you as the developer have not explicitly stated that such a loss is ok.
Dear friend,
Don't worry about precision loosing. Just say that my explanation is ok.
Thanks
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Dec 12th, 2006, 02:18 AM
#5
Thread Starter
PowerPoster
Re: Simple Casting
 Originally Posted by CornedBee
Bit size has nothing do with it. float is a floating point number, which means that any conversion between it and int can result in loss of precision.
But I'm confusing with that bit size. Both float and int are in same length. Then why it is not possible.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Dec 12th, 2006, 04:58 AM
#6
Re: Simple Casting
Again: because bit size doesn't matter. float can represent the number 0.5, while int cannot. Thus, converting this float to an int causes loss of precision.
(The converse is true, too. float cannot exactly represent the number 2^31 - 1, which int can.)
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 12th, 2006, 09:36 PM
#7
Thread Starter
PowerPoster
Re: Simple Casting
Thanks CornedBee,
I got your point.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Dec 28th, 2006, 09:38 AM
#8
Lively Member
Re: Simple Casting
Actually it has nothing to do with the bit size.
Int is represented in 32 bits in 2's complement. So the whole 32 bits represent the number. Floats are represented in 32 bits but in a different way. Starting fromt he most significant bit, bit 31 is a sign bit. Then bits 30 to 23 represent the exponent to the power 2. And the remaining bits 22 to 0 represent the significand or numbers after the decimal point.
So float has more precision since it has a way of representing real numbers.
Code:
if (Jacques_Villeneuve != Number_One)
exit(1);
-
Jan 1st, 2007, 09:27 PM
#9
Thread Starter
PowerPoster
Re: Simple Casting
 Originally Posted by Sebouh
Int is represented in 32 bits in 2's complement. So the whole 32 bits represent the number. Floats are represented in 32 bits but in a different way.
So that in simply way all things depend on its way of construction.
Thanks
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|