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
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.
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
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);
javapenguin (November 4th, 2010)
so do i just create a double variable like
double tmp = 0.3;
to use 1/3?
uhh ok nevermind i got it thanks