------------------------------------------------------------------ -- -- NAME: SQUARE_ROOT - BODY -- DISCREPANCY REPORTS: -- ------------------------------------------------------------------ -- file: square_root_b.ada -- level: all levels package body SQUARE_ROOT is function SQRT (VALUE : float) return float is -- The function SQRT uses the Newton-Raphson method of finding -- the square root. -- -- VALUE - The value used to find the square root. R1 : float; -- Check for thrashing. R0 : float := 1.0; -- Initial guess. RESULT : float := ( VALUE + (R0*R0) ) / (2.0 * R0); -- The final square root. PRECISION : float := 1.0 * 10.0 ** (- float'digits); --float'safe_small; -- The most precision expected in the answer. begin if VALUE < 0.0 then raise numeric_error; elsif VALUE = 0.0 then return(0.0); end if; loop R1 := R0; R0 := RESULT; RESULT := ( VALUE + (R0*R0) ) / (2.0*R0); if (abs ((RESULT-R0)/R0) <= PRECISION) or (abs (R1 - RESULT) <= PRECISION) then exit; end if; end loop; return RESULT; end SQRT; end SQUARE_ROOT;