Re: Deriving pi and e
by pg (Canon) on Oct 25, 2003 at 07:40 UTC
|
print atan2(1,1) * 4, "\n";
print exp(1);
| [reply] [d/l] |
|
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!
| [reply] [d/l] |
|
| [reply] [d/l] [select] |
|
|
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).
| [reply] |
|
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
| [reply] |
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 | [reply] [d/l] [select] |
|
$;=3;$_=$;while($;-=sin$;/cos$;)-$_;print
$;=2;$_=$;while($;+=(1-log$;)*$;)-$_;print
| [reply] [d/l] |
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 | [reply] |