Forum Discussion

5 Replies

  • 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
  • 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