Results 1 to 9 of 9

Thread: [RESOLVED] Simple Casting

  1. #1

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Resolved [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

  2. #2
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    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.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.

  4. #4

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Simple Casting

    Quote 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

  5. #5

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Simple Casting

    Quote 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

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.

  7. #7

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    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

  8. #8
    Lively Member Sebouh's Avatar
    Join Date
    Jan 2005
    Posts
    73

    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);

  9. #9

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Simple Casting

    Quote 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
  •  



Click Here to Expand Forum to Full Width