Forum Discussion
Kevin_Raison_74
Nimbostratus
Dec 08, 2005Exponentiation, involution
Can someone tell me how to accomplish pow(x,y) in F5's TCL implementation?
Thanks.
Kevin Raison
5 Replies
- Colin_Walker_12Historic F5 AccountThis is done via the normal TCL operator "**". You can find more about it here:Click here - http://tmml.sourceforge.net/doc/tcl/expr.html
Basically, it would look like this:expr { $x**$y }
HTH,
-Colin - Kevin_Raison_74
Nimbostratus
I tried the ** operator, but my BigIP says,
parse error: PARSE syntax 1976 {syntax error in expression " $t**$e ": unexpected operator *}] [{ $t**$e }]
this is the code:
set y 2000
set e 9
set t 2
set m [ expr { $t**$e } ]
set x [ expr { int($y * $m) } ] - Not sure why the "**" operator doesn't work but it seems it doesn't and I've tested it in several stand-along tcl interpreters as well.
There is a builtin "pow" command that you could use but it looks like we've disabled the math commands within iRules for performance reasons.
Don't dispair though. Where there's a will, there's a way.
Here's a rough implementation of the logic behind pow that you can include in your iRule.set y 2000 set e 9 set t 2 if { $t == 0 } { if { $e > 0 } { set m 0 } elseif { $e < 0 } { log local0. "undefined exponentiation!" } elseif { $e == 0 } { set m 1 } } elseif { $t == 1 } { set m 1 } elseif { $e == 0 } { set m 1 } elseif { ($t < -1) && ($e < 0) } { set m 0 } elseif { $t == -1 } { if { [expr ($e & 1)] == 0 } { set m 1 } else { set m -1 } } else { for {set i 1; set m $t} {$i<$e} {incr i} { set m [expr $m*$t]; } } set x [ expr { int($y * $m) } ] puts $m puts $x
*disclaimer: this code is pseudo tested. Please test before using it.
There may be some more optimal way to approach this but this should work for you.
-Joe - Brian_Gupta_115
Nimbostratus
Raison,
Why do you need this math in an iRule?
Thanks,
Brian - Kevin_Raison_74
Nimbostratus
Thanks. I had implemented the same thing, but found the performance to be less than desireable. In the end, bit shifting solved my problem with acceptable performance results.
My thanks to everyone for their suggestions.
Cheers.
Kevin
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects
