Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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

by swampyankee (Parson)
on Feb 19, 2013 at 19:18 UTC ( #1019647=note: print w/ replies, xml ) Need Help??


in reply to Re: [OT] Function to do x**y for |y|<1 w/o ** operator.
in thread [OT] Function to do x**y for |y|<1 w/o ** operator.

He has log, but not exp.


Information about American English usage here and here. Floating point issues? Please read this before posting. — emc


Comment on Re^2: [OT] Function to do x**y for |y|<1 w/o ** operator.
Re^3: [OT] Function to do x**y for |y|<1 w/o ** operator.
by BrowserUk (Pope) on Feb 19, 2013 at 19:47 UTC

    If you don't need fast or accurate:

    #! perl -slw use strict; sub myExp{ my $e = 1 + ( shift() / 10000000 ); my $r = $e; $r *= $e for 2 .. 10000000; $r; } printf "%30.20g : %30.20g\n", exp( $_ ), myExp( $_ ) for map log( 10**$_ ), -10 .. 10; __END__ C:\test>exp 9.9999999999999965e-011 : 9.9997349036884958e-011 1.0000000000000007e-009 : 9.9997852777366009e-010 9.999999999999982e-009 : 9.9998303391048947e-009 9.9999999999999943e-008 : 9.9998700988200641e-008 1.0000000000000004e-006 : 9.9999045679358269e-007 9.9999999999999974e-006 : 9.9999337241854084e-006 0.00010000000000000009 : 9.9999575897369183e-005 0.0010000000000000002 : 0.00099999761423453593 0.010000000000000005 : 0.0099999893930861859 0.10000000000000002 : 0.099999973530420908 1 : 1 10.000000000000002 : 9.9999973561653093 100.00000000000004 : 99.999893882330838 999.99999999999977 : 999.99761406302275 10000.000000000009 : 9999.9575911775646 100000.00000000001 : 99999.337176962814 999999.99999999953 : 999990.45646420098 10000000.000000006 : 9999870.1098560225 100000000.00000018 : 99998303.311974689 999999999.99999928 : 999978527.31280696 10000000000.000004 : 9999734913.5273266

    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.

      Can I post a link to your code? It's on cosmoquest.org, which used to be the Bad Astronomy & Universe Today forum.


      Information about American English usage here and here. Floating point issues? Please read this before posting. — emc

        Sure.


        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.
Re^3: [OT] Function to do x**y for |y|<1 w/o ** operator.
by BrowserUk (Pope) on Feb 19, 2013 at 20:08 UTC

    Update:Changed constants for slightly greater accuracy for the sake of another couple of iterations.

    Here's a somewhat more accurate, and much faster version:

    #! perl -slw use strict; sub myExp{ my $e = 1 + ( shift() / 4294967296 ); $e *= $e for 1 .. 32; $e; } printf "%30.20g : %30.20g\n", exp( $_ ), myExp( $_ ) for map log( 10**$_ ), -10 .. 10; __END__ C:\test>exp 9.9999999999999965e-011 : 1.0000000285332257e-010 1.0000000000000007e-009 : 1.0000000829163549e-009 9.999999999999982e-009 : 1.0000002130576865e-008 9.9999999999999943e-008 : 9.9999976329917627e-008 1.0000000000000004e-006 : 9.9999969112813618e-007 9.9999999999999974e-006 : 9.9999977582604716e-006 0.00010000000000000009 : 9.999998681099902e-005 0.0010000000000000002 : 0.00099999984556405617 0.010000000000000005 : 0.0099999993405499293 0.10000000000000002 : 0.099999996702749588 1 : 1 10.000000000000002 : 9.9999952742988558 100.00000000000004 : 100.00000084944983 999.99999999999977 : 999.99959325720954 10000.000000000009 : 10000.000169889967 100000.00000000001 : 99999.966690555739 999999.99999999953 : 1000000.1401405634 10000000.000000006 : 9999996.1589527112 100000000.00000018 : 100000003.39779937 999999999.99999928 : 999999662.81900096 10000000000.000004 : 10000002874.419859

    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.
Re^3: [OT] Function to do x**y for |y|<1 w/o ** operator.
by syphilis (Canon) on Feb 20, 2013 at 02:23 UTC
    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

      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.
Re^3: [OT] Function to do x**y for |y|<1 w/o ** operator.
by talexb (Canon) on Feb 20, 2013 at 15:25 UTC
      He has log, but not exp.

    Seriously. What kind of brain-dead platform has one but not the other. That's truly ridiculous.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2014-07-23 08:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (136 votes), past polls