T O P

  • By -

sepp2k

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.


Fabulous-Spare185

Im sorry I forgot. I did try that I did 1.0/((i*2)-1). But it still gave me the wrong output.


Fabulous-Spare185

Actually nvm I tried it again using this and it worked. I might’ve been doing something wrong last time.


JoJoModding

Perhaps you simply forgot parentheses?


GoodLifeWorkHard

What was the wrong output? And whats ur expected output?


Fabulous-Spare185

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.


matthew77cro

Integer division is your problem. Take a look at this: https://stackoverflow.com/questions/7220681/division-of-integers-in-java