The Challenge: Precision and Speed
Calculating the square root of a floating-point number (Part 30) is required for precise calculations like distance, physics, or graphics shading. This requires an iterative algorithm that works across the multi-byte structure of the float.
Method: The Newton-Raphson Method is the preferred algorithm for floating-point square roots because it is computationally fast (it converges quadratically).
The Newton-Raphson Formula
The goal is to iteratively refine a guess ($x_{n+1}$) using the previous guess ($x_n$) until the guesses stop changing (convergence).
$$\text{New Guess } (x_{n+1}) = \frac{1}{2} \left( x_n + \frac{N}{x_n} \right)$$
Where $N$ is the input number (the float whose root you want).
Step 1: Initial Guess and Normalization
The calculation starts with normalizing the input float $N$ (adjusting its exponent so the mantissa is in a standard range). A good initial guess ($x_0$) is essential for fast convergence.
Guessing: The initial guess is typically derived quickly from the exponent of the input number. A rough lookup table (LUT) is often used to get a close starting mantissa.
Step 2: The Core Iteration
The routine requires several floating-point operations within the loop:
- Floating-Point Division: Calculate $N / x_n$ (Part 78).
- Floating-Point Addition: Calculate $x_n + (N / x_n)$ (Part 78).
- Floating-Point Division by 2: Calculate the final term ($\div 2$), which is simply a subtraction of 1 from the exponent field of the float.
Z80 Implementation Focus: This routine heavily relies on the availability of robust, multi-byte Floating-Point Library routines to perform the division and addition accurately. The Z80 code focuses on parameter passing (using HL/DE for addresses) and controlling the iteration loop.
Step 3: Convergence Check
The iteration loop repeats until the precision is sufficient.
The Check: Compare the new guess ($x_{n+1}$) to the previous guess ($x_n$). If the difference between the two is smaller than a tiny value (the epsilon value, representing the desired precision), the calculation stops.
Final Output: The function returns the final 32-bit floating-point number representing the square root of $N$.