Re: square roots. I didn't see this actual algorithm in your post, so I thought I'd forward it to the group in case anyone else wanted to play around with square roots. I can generalize this into any arbitrary root (cube, 4'th, whatever) if anyone really wants it, but I've never seen anyone need anything like that for TA use.
I'm typing this in from memory, so excuse the inevitable syntax errors, but I did try this and it works fine. This should spit out today's close and its square root. Season to taste...
output = "root.lst";
float ans, guess, num, eps;
eps := 0.0001; // reduce this to get "closer" to the actual root.
float num; // the number you need the root of.
num := close(0); guess := num / 2.0; // first guess. ans := num / guess;
integer i; for i = 1 to 100 do // most will only take 5 to 12 iteratons. if (abs(guess - ans) >= eps) then // are we too far away? guess := (guess + ans) / 2; // average to refine guess... ans := num / guess; // ...for a more refined answer else i := 100; // we're done. Get out of loop endif; next i;
println symbol, " ", num, " ", ans; |