1

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.

  • 1
    I think, to get out of this circle, you need series converging to $\pi$. – Peter Mar 16 '17 at 22:22
  • 1
    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 https://en.wikipedia.org/wiki/Newton%27s_method? – Eman Yalpsid 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
  • 1
    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
  • @Kajelad But the sine-function has to be calculated in radians, leading to the crux of the problem. – Peter Mar 16 '17 at 22:29
  • @Peter It's conceptually simple, but if you're aiming for speed and/or numerical precision then a fast-converging series or another iterative method (of which there are several) would be preferred to using the built-in $\sin$ function. – Kajelad Mar 16 '17 at 22:30
  • @MG lolenstine How accurate do you need to calculate $\pi$ ? – Peter Mar 16 '17 at 22:33
  • @Kajelad: the issue is not about computing the sine with precision, as this is done for arbitrarily small arguments such that the function is linear (and actually computing the sine is irrelevant). The issue is about converting between degrees and radians, which requires the knowledge of $\pi$ (the series for the sine in degrees has coefficients that involve powers of $\pi$). –  Mar 16 '17 at 23:17
  • Everyone. Thanks for commenting. @Peter my PI doesn't have to be accurate, because it's just a proof of concept, and I'm using other formulas for calculating pi as well... Just so I can compare them in accuracy and efficiency. I was just wondering if there was a way to make my own sin°() function, that would take degrees as arguments. Thank all of you again. – MG lolenstine Mar 17 '17 at 13:30
  • @Kajelad where would I put lower and upper limit? I cant see them anywhere... Sin($\pi$)=0 – MG lolenstine Mar 18 '17 at 06:08
  • Why not just write your own sin_of_degrees function, and have it return Math.sin(Math.toDegrees(angle_in_degrees))? – awkward Mar 19 '17 at 12:44

2 Answers2

1

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$. –  Mar 17 '17 at 13:43
  • @MGlolenstine: for a reasonble way to compute $\pi$, use the Machin formula. https://en.wikipedia.org/wiki/Machin-like_formula –  Mar 17 '17 at 13:46
0

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"

awkward
  • 14,736
  • 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 00: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 06: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. –  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