One simple mathematical function oddly missing from the Flash Expression Editor abilities is the Square Root function. Since one wasn't there, and I discovered that I HAD to have one, So back to school I went to learn how to make one.
Here is a site that shows 3 different methods for calculating a square root.
I chose use the Newtonian Method and made the following function:
Comment: ---------------------------------------------- Comment: Newton's Method for Finding Square Root Comment: ---------------------------------------------- Set Variable: "n" = 1 Set Variable: "counter" = 0 Loop While (counter<iterations) Set Variable: "n" = n - ((n*n - input) / (2*n)) Set Variable: "counter" = counter+1 End Loop Set Variable: "/:output" = n
HOW the function works is not as important as finding a formula that you can plug into Flash. Since this method estimates the square root of a number, by using a loop to refine the calculation, you may get funny results. To see what I mean try entering a "0". (the actual square root of 0 is 0). If you ever find some spare time you can play with this function and raise the number of iterations. You'll will find that a higher number of passes over the code will generate a value which is closer to true value of the square root. Still, for purposes in Flash, it gets close enough!
To reuse this function in your own calculations, simply copy the frame called "sqrt" into your document. You need to set the variable "iterations" in your file as well. Set it to 10 or 15. This is how many times Flash will cycle through the formula
The function ASSUMES you have a variable called "input". The function performs its magic on that variable, then finishes off by creating a variable called "output".
That's it : )
discuss this topic to forum
