         Edinburgh IMP77 Compiler - Version 8.4

    1  %begin
    2  
    3  %constreal M SQRT2 = 1.4
    4  
    5  %real %fn sqrt(%long %real X)
    6  
    7    %integer %fn isqrt(%integer X)
    8      %integer k = 1, result = 1
    9      k = k+1 %and result = k*k %while result <= X
   10      %result = k-1
   11    %end
   12    
   13    %integer I
   14    %long %real A prev, A
   15    %long %real epsilon = X / 100000000.0
   16    %result = M SQRT2 %if X = 2.0
   17    I = isqrt(INT PT(X))
   18    %result = I %if X = I*I { exact root }
   19    ! Newton's method: A{n} = (A{n-1} + X/A{n-1})/2
   20    A prev = I ; A = A PREV
   21    %cycle
   22      A prev = A
   23      A = (A + X/A)/2.0
   24    %repeat %until A prev - epsilon <= A <= A prev + epsilon
   25    %result = A
   26  %end
   27  
   28  %endofprogram
?SQRT unused

   23 Statements compiled
