Archive:
Subtopics:
Comments disabled |
Sun, 07 Jul 2019 [ I wrote this in 2007 and forgot to publish it. Or maybe I was planning to finish it first. But if so I have no idea what I was originally planning to say, so here we are. ] In computer programs, it's quite common to need a numerical value for π. Often you see something like: #define PI 3.141592654 This has the drawback of not representing π as exactly as possible. But to do that in C probably requires putting in 16 digits after the decimal point, and most people don't have so much memorized. And anyway, you don't really know at compile time what the floating-point precision will be; some platforms support quad-width floats. So you can do better, maybe, by using the math library to calculate π. And people do: static double pi = 4*atan2(1,1); The atan2(y, x) function produces the (almost-)unique value θ from the range !![-\pi, \pi]!! such that a ray from the origin, passing through point (x, y), makes angle θ with the x-axis. Note that the arguments have y first and x second.
For example, You can use But this is a bit strange. Why is this so well-known? Why calculate 4*atan2(1,1) when $$\pi = {\operatorname{atan}}2(0,-1)$$ produces the same result and is simpler? (Obligatory IEEE 754 complaining: [ Addendum: Leah Neukirchen suggests that the [Other articles in category /prog] permanent link |