Forum Discussion
mhupman_60939
Nov 07, 2011Nimbostratus
iRule Math - Square Root?
I hope I'm simply doing something wrong, but I cannot figure out how to calculate the square root of a number inside an iRule. I tried the sqrt() and pow() TCL functions, but it doesn't seem like th...
George_Watkins_
Nov 17, 2011Historic F5 Account
Here is a solution using Newton's method to calculate a square root:
when HTTP_REQUEST {
set input [URI::query [HTTP::uri] "input"]
if { ([HTTP::path] equals "/sqrt") && ($input ne "") } {
if { $input == 0 } {
set sqrt 0
} else {
set x [expr {abs($input)}]
while { 1 } {
set y [expr (($x+($input/$x))/2)]
if { [expr (abs($x-$y))] < 0.01 } break
set x $y
}
set sqrt $y
}
HTTP::respond 200 content "The square root of $input is $sqrt"
}
}
http://test-http-virtual/sqrt?input=144 => "The square root of 144 is 12"
http://test-http-virtual/sqrt?input=225.25 => "The square root of 225.25 is 15.0083321641"
http://test-http-virtual/sqrt?input=67108864.0 => "The square root of 67108864.09 is 8192.00000549"
Hope that helps,
George
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