Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How come 1 divide by 2 is less than 0.5 ?!

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Smile How come 1 divide by 2 is less than 0.5 ?!

    Hi,

    So basically this is a game that you have to hit targets on a grid. every time there's a "hit" the hitCounter is ++ .
    in the end of the game it display a message according to your results. (e.g if you hit 4 out of four - 100% the message is "perfect game", if you hit 3 out of four the message is "very good" .... etc.)

    well, that's my plan... but the code seems to take me every time to one place.

    Assume that in the end hitCounter is 1, NUMBER_OF_TARGETS is always 2 and FIFTY_PERCENT is always 0.5 it will go here:

     
    else if ((hitCounter/NUMBER_OF_TARGETS) < FIFTY_PERCENT) {
                System.out.println("\nSorry, you only hit "+hitCounter+" target(s) out of " + NUMBER_OF_TARGETS);
            }



    I tested it many times with different numbers. there were times when I hit nothing and it was ok because if(hitCounter== 0) works.
    But even if I have perfect game and more than 50% hits it will take me to the same place.

    it's probably something in the division, maybe i'm implementing it wrong.

    I hope i describe the problem good enough.

    Thanks ahead for your help


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: How come 1 divide by 2 is less than 0.5 ?!

    Hello raabhim!
    I'm assuming that hitCounter and NUMBER_OF_TARGETS are ints, therefore you're using integer division and floating point is not considered. There several ways to make it work. One of them is casting the division to double
    (double) (hitCounter/NUMBER_OF_TARGETS)

    EDIT: My mistake, instead of the division you need to cast one of the variables as Norm suggested.
    Last edited by andreas90; May 1st, 2012 at 09:16 AM.

  3. The Following User Says Thank You to andreas90 For This Useful Post:

    raabhim (May 1st, 2012)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How come 1 divide by 2 is less than 0.5 ?!

    Did you test that expression? The results of the division (0) will be cast to double: 0.0
    You need to cast one of variables before the division:
    ((double) hitCounter/NUMBER_OF_TARGETS)
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following 2 Users Say Thank You to Norm For This Useful Post:

    andreas90 (May 1st, 2012), raabhim (May 1st, 2012)

  6. #4
    Junior Member
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: How come 1 divide by 2 is less than 0.5 ?!

    Perfect!
    Thanks guys

Similar Threads

  1. How to divide elements in two different arrays
    By BuhRock in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 11th, 2011, 06:38 PM
  2. How can i divide a string?
    By noFear in forum Java Theory & Questions
    Replies: 9
    Last Post: September 1st, 2010, 08:45 AM