Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Deriving pi and e

by fredopalus (Friar)
on Oct 25, 2003 at 04:46 UTC ( [id://302025]=CUFP: print w/replies, xml ) Need Help??

A method of finding pi without actually using it.
Update: Added derivation of e.
#!/usr/bin/perl -w $x = 3; $y = 0; do { $y = $x; $x = $x - (sin($x)/cos($x)); } until ($x == $y); print "$x\n"; ######################## ######################## $x = 2; $y = 0; do { $y = $x; $x = $x - ((log($x) - 1) * $x); } until ($x == $y); print "$x\n";

Replies are listed 'Best First'.
Re: Deriving pi and e
by pg (Canon) on Oct 25, 2003 at 07:40 UTC

    Interesting... but in real life one would simply say:

    print atan2(1,1) * 4, "\n"; print exp(1);
      I'm not sure why people keep propogating the one where you have to multiply by 4. It's much simpler to say:
      my $pi = atan2(0,-1);
      In other words, don't create a vector that points at 45 degrees and multiply by 4... create a vector that points at 180 degrees!

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

        Personally I do it for the reason that theorbtwo stated. I know how to do it with atan (and make the well-known power series converge), and the easiest way is to do 45 degrees and multiply by 4. Then I try to convert it to Perl, notice that there is no atan function, and use atan2.

        That said, noting that I can do it directly with atan2 won't convince me not to reach for 4*atan2(1,1) for several reasons:

        1. I don't have this memorized, just the derivation of how it should work. I have more years of math than Perl, and so the derivation I know is the one that comes up immediately.
        2. Perl's documentation doesn't indicate whether atan(0,-1) will give you PI or -PI (or blow up because someone decided that "in the range of -PI to PI" means an open interval, not a half-closed one). I therefore don't know whether to trust it in future implementations of Perl.
        3. Even though I have had it pointed out to me that Perl's implementation gives you PI, I don't know whether I can trust that trivia in future implementations of atan2 that I may encounter in other languages. This goes doubly because I know enough about how one might try to implement atan2 to come up with plausible implementations which break at (0,-1).

        Er, I suspect the reason is that people forget that you can do that with atan2. (You can't do that with a normal atan, IIRC...)

        In any case, yes, it is a little silly.


        Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

        Well, your method is logical but one advantage of atan(1,1)*4 would be that there is reduced scope to get the argument order or signs wrong. (1,1) is easy to remember but (0,-1) could get mixed up with (1,0), (-1,0) or (0,1) by the sieve-headed amongst us. Never under-estimate sieve-headedness! :-D
Re: Deriving pi and e
by liz (Monsignor) on Oct 25, 2003 at 10:21 UTC
    Apart from pg's and Abigail-II's comments, I couldn't resist golfing these down to:
    1while($x||=3)-($x-=sin$x/cos$x);print"$x\n"
    and:
    1while($x||=2)-($x-=((log($x)-1)*$x));print"$x\n"

    I particularly like the fact that it only uses 1 variable ;-)

    Liz

    Update:
    Golfed some more by losing the seperate initial assignement of $x

      I think you've landed in a sand trap. When I run these in Perl 5.8.1 they perform one iteration and stop. The problem is that you're changing the value of $x in the while condition and the left-hand side of the subtraction can return either the new value or the old value. In my case it's returning the new value which causes the loop to terminate early.

      Update: These work for me. I've skipped printing the newline to lower the golf score by 6. I wasn't able to skip the initial assignment but that only saves 1 character anyway.

      $;=3;$_=$;while($;-=sin$;/cos$;)-$_;print $;=2;$_=$;while($;+=(1-log$;)*$;)-$_;print
Re: Deriving pi and e
by Abigail-II (Bishop) on Oct 25, 2003 at 09:11 UTC
    Given the rounding errors that occur when doing math with floating numbers, there's no guarantee the given algorithms will actually terminate.

    Abigail

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://302025]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2024-04-26 05:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found