We don't bite newbies here... much PerlMonks

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

by syphilis (Chancellor)
 on Feb 20, 2013 at 02:23 UTC ( #1019685=note: print w/replies, xml ) Need Help??

He has log, but not exp.

Perhaps there's also something helpful in Math::BigFloat's _pow() sub:
```  # Taylor:       |   u    u^2    u^3         |
# x ** y  = 1 + |  --- + --- + ----- + ...  |
#               |_  1    1*2   1*2*3       _|
where (I gather)
```u = y * ln x
Cheers,
Rob

Replies are listed 'Best First'.
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

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://1019685]
help
Chatterbox?
 [MidLifeXis]: corelist Typo. Wow. Time to go, fingers are malfunctioning. [marioroy]: Thanks MidLifeXis. Leaned another thing about Perl. Wow. [marioroy]: Didn't know of corelist until today. Cool. [marioroy]: Lady_Aleena Which module?

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (11)
As of 2017-05-24 18:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favorite model of computation is ...

Results (186 votes). Check out past polls.