Calculating of PI from sin() function in Java

1.9k Views Asked by At

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.

Thank you in advance.

2

There are 2 best solutions below

3
On BEST ANSWER

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).

5
On

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"