I need to implement Newtons method for finding square roots with given count count signs after floating point in Delphi 7. I wrote this code:
program NewtonMethod
{$APPTYPE CONSOLE}
uses
SysUtils, Math;
const
signCount = 50; //Signs after floating point count
var
number, curRoot, prevRoot: Extended;
precision: Extended;
function input(): Extended;
begin
Write('x=');
try
Readln(number)
except
Writeln('Invalid input data, please try again');
Input();
end;
end;
begin
input();
curRoot := sqrt(number); //Íà÷àëüíàÿ
precision := 1 / power(10, signCount);
prevRoot := 0;
repeat
prevRoot := curRoot;
curRoot := prevRoot - (Power(prevRoot, 2) - number) / (2 * prevRoot);
until
Abs(curRoot - prevRoot) < precision;
Writeln(curRoot);
ReadLn;
end.
Please, answer to my questions:
- Is this code correct ?
- How to print result root in normal (not exponential) form with all signs ?
- Input values may be very large (up to 200 signs). Can this code work with them ?
And suggest me, how can I improve my code ?