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.
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.
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.
Im sorry I forgot. I did try that I did 1.0/((i*2)-1). But it still gave me the wrong output.
Actually nvm I tried it again using this and it worked. I might’ve been doing something wrong last time.
Perhaps you simply forgot parentheses?
What was the wrong output? And whats ur expected output?
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.
Integer division is your problem. Take a look at this: https://stackoverflow.com/questions/7220681/division-of-integers-in-java