-4

The variable t keeps going to zero.

Algorithm

My Work

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Hack Delta
  • 13
  • 3
  • You really need to put **all** the relevant information into the question. Links are not enough. – Mark Ransom Oct 02 '19 at 17:22
  • 3
    Handy reading: [Why not upload images of code on SO when asking a question?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question) – user4581301 Oct 02 '19 at 17:23
  • Please post code, errors, sample data or textual output here as plain-text, not as images that can be hard to read, can’t be copy-pasted to help test code or use in answers, and are barrier to those who depend on screen readers. You can edit your question to add the code in the body of your question. For easy formatting use the `{}` button to mark blocks of code, or indent with four spaces for the same effect. The contents of a **screenshot can’t be searched, run as code, or copied and edited to create a solution.** – tadman Oct 02 '19 at 17:24
  • The value given for "true pi" is off. – Eljay Oct 02 '19 at 17:38

1 Answers1

3

The problem is here:

t *= ((n-1)/((2*n)-1))

n is integer. When you perform arithmetic operations on an integer, the result is also an integer (i.e. gets truncated).

To make the division into a float-point one, you need to convert one of the operands to a floating-point type, for example like so:

t *= ((n-1)/(float)((2*n)-1))
NPE
  • 486,780
  • 108
  • 951
  • 1,012