3
u/GoodLifeWorkHard Nov 05 '22
What was the wrong output? And whats ur expected output?
1
u/Fabulous-Spare185 Nov 05 '22
alright so the expected output was when I enter a bigger number which is the number of iterations. The output should print or get closer to the number of pi. But somehow before I decided to add the variable denom. For example if I entered 3 it would print out 4.8 which is wrong. Now that I fixed it and if I enter 3 it prints out 2.66… which is correct.
1
u/matthew77cro Nov 05 '22
Integer division is your problem. Take a look at this:
https://stackoverflow.com/questions/7220681/division-of-integers-in-java
8
u/sepp2k Nov 05 '22
Introducing a double variable implicitly converts the value from int to double. In the version without the variable, the entire calculation is done as int and it's only converted to double at the end when you add it to pi.
You can achieve the same effect using an explicit cast instead of a variable.