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: help on this...

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default help on this...

    i have this statement

    pi = 4*(1-(1/3))

    then i print it out.. pi is a double set to 0..

    but it keeps printing out 4.0

    instead of 2.6666666667 help

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: help on this...

    You're using ints to do double calculations. With integers, 1/3 is 0. 1-0 is 1. 4 * 1 is 4, so you get 4.0.

    To see this better, do it in a few steps:
    int step1 = 1/3;
    System.out.println(step1);
    int step2 = 1 - step1;
    System.out.println(step2);
    int step3 = 4 * step2;
    System.out.println(step3);

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

    javapenguin (November 4th, 2010)

  4. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: help on this...

    so do i just create a double variable like

    double tmp = 0.3;

    to use 1/3?

  5. #4
    Junior Member
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: help on this...

    uhh ok nevermind i got it thanks