[A83] Re: some none-programming questions
[Prev][Next][Index][Thread]
[A83] Re: some none-programming questions
Interesting formulas can be found at http://gj.mit.edu/pi/formulas/
On Sat, 24 Nov 2001, David Phillips wrote:
> Well, yes and no. I did some Google searches, and while I didn't spend too
> long on it, I didn't see anything that explained how to accurately calculate
> pi to a certain number of places. You see a listing with the first ten
> million digits of pi. I don't know how that is calculated, or verified.
>
> The problem comes from being able to do calculations accurately. If you've
> done any kind of scientific work, you'll understand that accuracy is a huge
> problem. Remember significant figures, or sig figs? This is a similiar
> type of problem. You have to be sure that you aren't getting more out of a
> calculation than you are putting into it. If you add 0.2 and 0.34, what do
> you get? No, 0.54 is not the correct answer. You get 0.5. Adding 0.20 and
> 0.34 would get you 0.54. 0.2 only has one decimal digit of significance, so
> it cannot yield a result with a more significance.
>
> This sort of thing is what made me hate chemistry in high school. Physics
> was nice, because everything is just an estimate, so anything close enough
> worked.
>
> Using the following formula, you run into an accuracy problem:
>
> pi = 4 * (1/1 - 1/3 + 1/5 - 1/7 ...)
>
> Those divisions cannot be represented accurately. Somewhere, you have to
> start dropping digits. I decided to write up a small PHP script to
> calculate pi using that formula. I chose PHP because it has BCMath
> arbitrary precision mathematics functions. PHP is nice for quick scripts to
> test things out, similiar to how QuickBASIC used to be used, since it has a
> lot of built in stuff. Then again, it's nice for a lot of other stuff too.
> The BCMath functions require a scale factor, that sets the number of digits
> after the decimal place in the result. Obviously, you would want this to be
> infinite, to provide the most accurate results. Of course, this is
> impossible. There might be a good ratio for the scale that relates to the
> number of interations, but I have no idea what this would be.
>
> The script can be tested on a Unix box that has the CGI version of the PHP
> interpreter by making the script executable (chmod +x). Many Linux
> distributions might place it in /usr/bin instead of /usr/local/bin. Debian
> does it this way. I put a symlink in /usr/local/bin, so that scripts will
> work on FreeBSD without any changes. You might have to change the location
> of the script interpreter. Windows users can download precompiled versions
> of PHP:
>
> #!/usr/local/bin/php -q
> <?
> set_time_limit(0);
> $scale = 10000;
> $pi = 0;
> $sign = 1;
> for($n = 0; $n < 10000; $n++)
> {
> $term = bcdiv(1, (2 * $n) + 1, $scale);
> $term = bcmul($term, $sign, $scale);
> $pi = bcadd($pi, $term, $scale);
> $sign *= -1;
> }
> $pi = bcmul($pi, 4, $scale);
> echo substr($pi, 0, 50) . "\n";
> ?>
>
> The script should be relatively self explanatory. The last line prints the
> result, truncated to 50 characters. Feel free to play with the number of
> iterations, and the scale. As can be seen, it is not very accurate:
>
> electrum@tourian:~$ ./pi.php
> 3.14149265359004323845951838337481
>
> Something that could be tried is to only do the division once, as the final
> step. Simplify everything down into one giant fraction, then divide at the
> end. The advantage here is that due to the nature of multiplication, you
> know exactly how many decimal places are needed to hold the result of an
> operation. I don't know how many places would be accurate in the result
> after a given number of iterations. Since the only way that I know of to
> get a common denominator is to multiply every number in the series by each
> other, you would end up with some very large numbers very quickly. It would
> probably be (2n+1)! digits where n is the iteration.
>
> Feel free to experiment and share the results, or any good links that you
> find.
>
> > Yeah, that's the simplest I've seen. I don't understand arctan
> > right now. It
> > should be quite simple to do something with it (making a pi calculator
> > (JavaScript?) or so)
References: