Welcome, Guest. Please Login
Tinderbox
  News:
IMPORTANT MESSAGE! This forum has now been replaced by a new forum at http://forum.eastgate.com and no further posting or member registration is allowed. The forum is still accessible via read-only access for reference purposes. If you wish to discuss content here, please use the new forum. N.B. - posting in the new forum requires a fresh registration in the new forum (sorry - member data can't be ported).
  HomeHelpSearchLogin  
 
Pages: 1
Send Topic Print
Macros and a 'loop' to create an exponent function (Read 7615 times)
Sumner Gerard
Full Member
*
Offline



Posts: 359

Macros and a 'loop' to create an exponent function
Aug 29th, 2011, 3:52pm
 
Unless it's hidden in plain sight, a mathematical operator for exponents is not included in TB. Logarithms 'yes', but exponents apparently 'no'. School math tells me that one is the inverse of the other, and maybe I could get an exponent using the log() operator. Or do something with runCommand(). But I don't know how.  So, until TB includes an exponent operator, I thought I'd try to "roll my own" using a macro. Actually it's two macros, the first of which calls the other automatically:

Macro 'Name': 'mExponent'
Macro 'Value':
Code:
$MyResult=0;$MyCtr=1;
$Base=$1;$MyTempNum=$1;$Exponent=$2;
action(do("mExp_loop")) 



Macro 'Name': 'mExp_loop'
Macro 'Value":
Code:
if($MyCtr<$Exponent {
$MyTempNum=$MyTempNum*$Base;$MyCtr=$MyCtr+1; action(do("mExp_loop"))
}
else {$MyResult=$MyTempNum} 



($MyResult, $MyCtr, $Base, $MyTempNum, and $Exponent need to be created as number attributes.)

Now, whenever I need an exponent, say $MyNum1 raised to the power $MyNum2, all I need to do is enter the following as a rule or run it from a stamp:

Code:
action(do("mExponent",$MyNum1,$MyNum2)) 



The result will appear in $MyResult of the note that contains the action() code or that has been stamped with it.

"Plain English" explanation:

'mExponent' initializes values for a loop and substitutes in the value of the base for $1 and the exponent for $2, then runs 'mExp_loop'.

'mExp_loop' runs itself, each time multiplying the previous $MyTempNum by $Base and incrementing the loop counter by 1.  When it's run itself enough times (as defined by the exponent) the 'if' is no longer satisfied, so it stops running itself and instead sets the final value for $MyResult.

This works for positive integer exponents, enough for many present value and future value type calculations.

If there's already a more "native" TB way to do this, I would be interested to know.  And maybe there's something easy via runCommand()? At some point it might be nice to have a "real" exponent function that can handle non-integers and negative exponents (assuming there isn't one already).

But meantime this does the job, and shows a bit of the power of macros and a simple loop.
Back to top
 
« Last Edit: Aug 29th, 2011, 5:06pm by Sumner Gerard »  
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Macros and a 'loop' to create an exponent function
Reply #1 - Aug 29th, 2011, 4:45pm
 
My schoolboy maths was a long time ago, but I understand the 'bc' CL tool will give exponentials. Put this in Terminal:

echo "e(4.7*l(2))" | bc -l (gives 25.99207668339953672244)

If that's what you're after, reverse engineer the CL example above  into a runCommand() call.

Back to top
 
 

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Sumner Gerard
Full Member
*
Offline



Posts: 359

Re: Macros and a 'loop' to create an exponent function
Reply #2 - Aug 29th, 2011, 5:17pm
 
The command line tools intimidate me, but will check out 'bc' now that I know that's the one to look at.  Thanks.
Back to top
 
 
  IP Logged
Mark Bernstein
YaBB Administrator
*
Offline

designer of
Tinderbox

Posts: 2871
Eastgate Systems, Inc.
Re: Macros and a 'loop' to create an exponent function
Reply #3 - Aug 29th, 2011, 5:56pm
 
Here's a command line that sets $Result to the square root of $Num:

Quote:
$Result=runCommand("ruby -e 'puts "+$Num+"**0.5'")


This works by assembly the command line ruby -e 'puts  (your number)**0.5.  You can change the exponent as you like, or grab it from an attribute.

If people would find an exponentiation operator handy, drop us some email. info@eastgate.com
Back to top
 
 
WWW   IP Logged
Sumner Gerard
Full Member
*
Offline



Posts: 359

Re: Macros and a 'loop' to create an exponent function
Reply #4 - Aug 29th, 2011, 10:43pm
 
Thanks! I was struggling mightily at the command line.

So, to make this more friendly to use, I created a macro "mExpon" with this in the 'Value' box:

  runCommand("ruby -e 'puts $1**$2'")

Now I no longer have to worry about command lines and Ruby and missing ' and such. Whenever I need an exponent, I can just put the following in my rule or stamp:

  $MyResult=eval(do("mExpon",$Base,$Exponent))

substituting whatever the relevant attributes names are for $Base and $Exponent (and $MyResult).

Would guess there won't be a general clamor for exponents.  They're really common in my field of finance, though, for calculating present value and future value and compound growth rates and such. I thought if they're easy enough (it seems they are, even without a built-in operator) I'd try putting them to use in TB.
Back to top
 
« Last Edit: Aug 29th, 2011, 10:44pm by Sumner Gerard »  
  IP Logged
Sumner Gerard
Full Member
*
Offline



Posts: 359

Re: Macros and a 'loop' to create an exponent function
Reply #5 - Aug 30th, 2011, 4:58pm
 
Think I managed to decipher MarkA's 'bc' example upthread #1. It calculates 2 to the power of 4.7 by:

Step 1: Taking the natural log of 2   That's the l(2) in the expression (lower case letter l, not the number 1).
Step 2: Multiplying the value from Step 1 that by the exponent 4.7
Step 3: Raising the number e to the power of the result of Step 2.  That's the e( ).

Command line oddities that at first puzzled me as a rank beginner: The -l (again a lower case l not a number 1) switch at the end of the bc' line is a different l from the natural log l earlier in the line. It tells 'bc' to use its math library.  And the -e in the ruby line has nothing to do with the number e. I think it's telling Ruby to look at the command line itself rather than load a file.  And the 'puts' in Ruby and 'echo' in 'bc' I think basically do the same thing: put the result on the command line for runCommand() to grab.

Not sure if I've got the math and command line stuff all straight. But the result in 'cl' is the same as with Ruby's more easily understood syntax (though the precision may be different).

The great news for me is how easy it is to make this friendly to use in Tinderbox even though I know about as much Ruby and 'cl' as I do Quenya. For the 'bc' command line I found I could create a macro 'mExponAlt' with 'Value':

   runCommand("echo 'e($2*l($1))' | bc -l")

Thereafter all that is needed to get an exponent is:

   $MyResult=eval(do("mExponAlt", $Base, $Exponent))

... again, in actual use, substituting the relevant attribute names corresponding to $Base, $Exponent, and $MyResult.
Back to top
 
« Last Edit: Aug 30th, 2011, 5:03pm by Sumner Gerard »  
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Macros and a 'loop' to create an exponent function
Reply #6 - Aug 30th, 2011, 5:10pm
 
Sumner, I've no Unix maven and find CL equally inscrutable, though I find Google is my friend.  'Man' pages help but don't expect them to be written with the learner in mind - I find them most useful for decrypting command lines I've found online.
Back to top
 
 

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Pages: 1
Send Topic Print