### Re^4: [OT] Function to do x**y for |y|<1 w/o ** operator.

by BrowserUk (Pope)
 on Feb 20, 2013 at 02:49 UTC ( #1019693=note: print w/ replies, xml ) Need Help??

Here's the Taylor series version, but it converges much less quickly and achieved less accuracy (at least for very small x):

```#! perl -slw
use strict;

our \$N //= 32;

sub myExp2 {
my \$x = shift;
my( \$r, \$top, \$bot ) = ( 1, \$x, 1 );
\$r += \$top / \$bot, \$top *= \$x, \$bot *= \$_ for 2 .. \$N;
\$r;
}
printf "%30.20g : %30.20g \n", exp( \$_ ), myExp2( \$_ ) #, myExp2( \$_ )
+ - exp( \$_ )
for map log( 10**\$_ ), -10 .. 10;

__END__
C:\test>exp -N=227
9.9999999999999965e-011 :       -9.2662709999185665e-008
1.0000000000000007e-009 :       -8.6393006494132869e-009
9.999999999999982e-009 :        1.1100933780704416e-008
9.9999999999999943e-008 :        9.9888569074918635e-008
1.0000000000000004e-006 :        1.0000228347576172e-006
9.9999999999999974e-006 :        1.0000001331927489e-005
0.00010000000000000009 :         0.00010000000018884082
0.0010000000000000002 :          0.0010000000000062366
0.010000000000000005 :          0.0099999999999984095
0.10000000000000002 :           0.099999999999999867
1 :                              1
10.000000000000002 :             10.000000000000004
100.00000000000004 :                            100
999.99999999999977 :             999.99999999999955
10000.000000000009 :             10000.000000000007
100000.00000000001 :             100000.00000000004
999999.99999999953 :             999999.99999999977
10000000.000000006 :             10000000.000000009
100000000.00000018 :             100000000.00000022
999999999.99999928 :             999999999.99999964
10000000000.000004 :             10000000000.000002

Try going higher with the iterations and things turn to #inf.

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Create A New User
Node Status?
node history
Node Type: note [id://1019693]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2016-07-25 17:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What is your favorite alternate name for a (specific) keyboard key?

Results (224 votes). Check out past polls.