GARTHWILSON wrote:
For the acual calculation of the square root, I've used the r=(x+r^2)/2r iterative method, which converges rather quickly. I have not tried BitWise's method (above).
Um, your formula:
r = (x + r^2) / 2r
is the same as BitWise's:
n' = (n + x / n) / 2
since n and r are the same thing.
Anyway, for source code, there is:
1. The DP18 (a BCD FP package) square root routine in Apple Assembly Line, linked to in the Source Code Repository (the 11-1984 and 10-1984 issues discuss the square root routine).
2. The KIMATH square root routine, in Simple Microcomputers and Trainers -> MOS Technology KIM-1
3. The 6502 FP routines by Wozniak & Rankin in the Source Code Repository -> Floating Point Math
4. The EhBASIC square root function (SQR) starting at LAB_SQR, linked to in the Homebuilt Projects on The Web -> Homebuilt 6502 Software Projects
#1 & #2 use the Newton's method formula above (KIMATH calls it Heron's method, but it's the same thing).
#3 does not have a square root function, but you can calculate the square root with:
Square root of X = EXP (LOG (X) / 2)
EXP(number) is e^number. LOG is the natural log, not log base 10. In fact, any pair of N^number and log base N functions will work, as long as N is the same in both.
#4 uses a integer square root routine on the mantissa and divides the exponent by two, elegantly handling odd exponents. In other words:
SQRT (mantissa * base ^ exponent) = SQRT (mantissa) * base ^ (exponent / 2)
#4 is the fastest of the three techniques (a lot of cycles can be squeezed out of the actual routine, and the temporary RAM usage can be reduced as well), then Newton's method, and EXP(LOG) is the slowest. You have to pay attention to round-off errors with EXP(LOG), but it's the least amount of additional code when EXP and LOG are already available.