Results 1 to 4 of 4
  1. #1
    Tzoshirzup is offline Member
    Join Date
    Nov 2008
    Posts
    2
    Rep Power
    0

    Default Rediculous problem?! Assigning a fraction to a double variable

    Tzo here,

    I'm having trouble with a rediculous problem; an extrodinarily simple assignment and or creation statement.

    Basically i can't get my type double variable to equal a simple fraction like 1/6,
    however rediculous that may sound even worse so is the fact that there's a working double using more complex variables just above it in the code.

    I've tried just about every work round i could think of to make this go but i'm still getting the same stupid results.


    Java Code:
    double temp = 0;
    for (int i = 0; i < intDrawn; i++)
    {
        temp += 1/(Double.valueOf(Integer.toString(intBalls))-i);
    }//end for loop
    dblWin = 1-temp/intDrawn;
    System.out.println(temp);
    double temp2 = 1/6;		//1/dblWin;
    System.out.println("temp2 " + temp2);
    dblLose = Math.pow(temp2, (1/6));
    This outputs:

    0.15246253168108626
    temp2 0.0

    As u can see in the for loop i had already had some trouble with the double variable befor however i found a work around that worked.

    Any insight into the problem and or solution would be appreciated.

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    i think that 1 and 6 are by default integers. stick in '.0' to fix that. i honestly don't know the "correct" way to do this, but if there is a proper method to do this, i'm sure someone will say.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You're running into the int division wall. Please note that an int/int equals an int and that this is rounded down, just as if you were casting. So 1/6 is 0. To get around this cast one of these numbers as a double:
    Java Code:
    double temp2 = (double)1/6;

  4. #4
    Tzoshirzup is offline Member
    Join Date
    Nov 2008
    Posts
    2
    Rep Power
    0

    Default

    Thanks for the help i'll give these a shot although i did try some form of casting the numbers to double.

    The int division wall you mentioned makes alot of sence in that context now actually after i think about it :)

Similar Threads

  1. Double Value problem
    By sakthivel123 in forum New To Java
    Replies: 2
    Last Post: 07-10-2008, 04:18 PM
  2. Double Buffering problem
    By aprenz in forum Java Applets
    Replies: 0
    Last Post: 05-28-2008, 04:26 AM
  3. how to use session variable in my problem
    By Arif Baig in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-27-2008, 07:23 AM
  4. Big Fraction 1.00
    By JavaBean in forum Java Software
    Replies: 1
    Last Post: 03-26-2008, 04:24 AM
  5. Replies: 3
    Last Post: 01-19-2008, 08:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •