Calculating of PI from sin() function in Java

Ok... So I'm trying to finish my school project with processing and I'm wondering if there is a way to calculate PI from sin() like this. But I don't know how to use sin() function with degrees in Java or how to write my own. The problem with radians is that I need to convert radians into degrees with PI, which I'm trying to calculate.

• I think, to get out of this circle, you need series converging to $\pi$. – Peter Mar 16 '17 at 22:22
• Do you have access to a sine inverse function? Then just put in $\frac{\sqrt{2}}{2}$ and multiply by 4. – theREALyumdub Mar 16 '17 at 22:23
• How about something like en.wikipedia.org/wiki/Newton%27s_method? – Andrew Mar 16 '17 at 22:24
• What about calculating $\zeta(4)$ and using $\zeta(4)=\frac{\pi^4}{90}$ ? The zeta-function can easily be approximated by summing up the first $n$ terms and converges very fast. But you would need a square-root as well to actually calculate $\pi$ this way. – Peter Mar 16 '17 at 22:26
• It's a somewhat inelegant approach, but you can just iteratively approximate $\pi$ using $sin(\pi)=0$. Just set a lower and upper bound ($3$ and $4$, for instance) and average them. If the $sin$ of the average is negative, that average becomes the new upper bound. If is positive, it becomes the new lower bound. Repeat and you will approach pi exponentially until you hit the numerical precision of this process. – Kajelad Mar 16 '17 at 22:26

Sorry, but this is not a good idea. The formula that you saw essentially expresses that $$\sin x\approx x$$ when $x$ is small, and the smaller $x$ the more exact the approximation. It is valid for angles in radians.

When the angles are in degrees, this relation becomes

$$\sin°x\approx \frac{\pi x}{180}$$ where $\sin°$ denotes the sine of an angle in radians. So you hope to evaluate

$$\pi\approx180\frac{\sin°x}x.$$

If the function $\sin°$ is not available, you will have to emulate it with an explicit conversion, using

$$\sin°x=\sin\frac{\pi x}{180},$$ so that

$$\pi\approx180\frac{\sin\dfrac{\pi x}{180}}x.$$

So, not only this does not allow you to compute $\pi$ as it requires preliminary knowlegde of $\pi$, but it will do that in a very inefficient and inaccurate way, actually replacing $cx/x$ by $\sin cx/x$. You will spend much energy to go round in circles.

Even when a $\sin°$ function is available, this approach is wrong because the $\sin°$ will do the conversion from degrees to radians anyway (using a hard-coded value of $\pi$), and you will have to use an angle so small that $\sin x=x$ numerically, and there is no more point computing the sine.

A less "schizophrenic" approach is using

$$\pi=4\arctan1$$ (in radians).

• Thank you. The PI isn't going to be used for anything, all I wanted to do, was to get a better understanding of different approaches to calculating PI and I just thought, that I could use sine as it is connected to PI. Because I'm from EU and we use degrees(no offense to non EU people), so it would be appropriate to use degrees, because everything is connected to the school project. In the project all I have to show is different approaches to calculating pi. I've already used Gregory-Leibinz, Nilakanths and frozen hot dog methods. I just needed one more. – MG lolenstine Mar 17 '17 at 13:35
• @MGlolenstine: I hope I made it clear that this is not a way to compute $\pi$. – Yves Daoust Mar 17 '17 at 13:43
• @MGlolenstine: for a reasonble way to compute $\pi$, use the Machin formula. en.wikipedia.org/wiki/Machin-like_formula – Yves Daoust Mar 17 '17 at 13:46

I am assuming your sin(x) function takes radians, as stated in the OP. Start by assigning x = 3 and then iterate the statement x = x + sin(x) a few times (three iterations should work well). When you are done, x will contain a good approximation to $\pi$.

How does it work? We are using "fixed point iteration" to approximate a root of $x = x + \sin(x)$ near $x = 3$. Reference: Wikipedia, "Fixed-point iteration"

• Thanks for answering my question, but the question was about degrees. I was thinking about doing it with radians, but because we in school use degrees not radians, I'd like to use degrees. I'll use your idea in my next project... It seems fast. Thank you. – MG lolenstine Mar 17 '17 at 23:19
• @MGlolenstine: I thought you asked how to calculate pi, having a sine function that takes radians? – awkward Mar 18 '17 at 0:02
• Because my first language isnt emglish and I'm still learning it in school, my question might've been unclear. Sorry for that. I was trying to make my own function of sine that used degrees or use a premade one, but now I see, that it's impossible, because sine needs to be calculated in radians... Thank you. – MG lolenstine Mar 18 '17 at 6:04
• @awkward: even though the sequence indeed converges very well to $\pi$, it is actually of little use for accurate computation because the evaluation of $\sin(x)$ is costly. – Yves Daoust Mar 18 '17 at 11:24
• It's clear I misinterpreted the question, but I will leave my answer because I still think it's an interesting computation. – awkward Mar 18 '17 at 12:48