* SQ-ROOT.CMD 29 Jun 83 r/11 2739 BYTES
* Verne Kurisu (714) 495 6256

SET TALK off

DO WHILE T
* infinite DO loop.
ERASE
	
STORE 0.0000001 to DELTA
* memory variable to decide when to terminate 
* calculation of SQUARE ROOT and used to
* polish final result.

STORE ' ' to ACTION
* memory variable initialization insures that the
* program will not bomb when ACTION is 
* later released.
	
STORE "Press <RETURN>." to ACTION
* memory variable to reduce repetitious typing.

@ 10,1 SAY ' '
* blank line at line 10, so next line prints at line 11.

INPUT "   Derive SQUARE ROOT of what number ? " to NUMBER
ERASE
* inputs the number into the program

IF NUMBER<0
  @ 10,10 SAY "IMAGINARY square root."
ENDIF
* traps input of negative number.

  IF NUMBER=0
@ 10,10 SAY "Square root of ZERO is ZERO."
  ENDIF
* traps input of ZERO.

IF NUMBER<=0 
 @ 13,15 SAY ACTION
  SET CONSOLE off
   WAIT
   SET CONSOLE on
  LOOP
 ENDIF
* LOOPS to data entry.

@ 10,20 SAY 'WORKING.'
* message on screen for reassurance.

STORE NUMBER to TEMPORARY
STORE 0 to EXPONENT

DO WHILE TEMPORARY<1
  STORE TEMPORARY*100 to TEMPORARY
  STORE 1+EXPONENT to EXPONENT
ENDDO
* traps inputs less than 1.

STORE TEMPORARY*.5 to SQ:ROOT
* 1st approximation.

STORE 0 to ITERATION
* initializing memory variable to count the loops, or iterations.
 
DO WHILE ITERATION<15
* 15, or less loops, will yield excellent approximations.
     STORE TEMPORARY/SQ:ROOT to Z
* series named (Sir Isaac) NEWTONS METHOD OF APPROXIMATION.
         STORE 1+ITERATION to ITERATION
* counting the loops.

     IF Z*Z-TEMPORARY>DELTA .OR. TEMPORARY-Z*Z>DELTA
     STORE (1.0/2)*(Z+SQ:ROOT) to SQ:ROOT
     ELSE
     STORE 15 to ITERATION
     ENDIF
* test the approximation.

ENDDO

IF SQ:ROOT-INT(SQ:ROOT)<DELTA
  STORE INT(SQ:ROOT) to SQ:ROOT
    ELSE
      STORE (SQ:ROOT*10000000) to SQ:ROOT
    STORE INT(SQ:ROOT) to SQ:ROOT
   STORE SQ:ROOT*DELTA to SQ:ROOT
  ENDIF
* cleans up decimal zeros.

STORE 1 to FACTOR

DO WHILE EXPONENT>0
     STORE EXPONENT-1 to EXPONENT
     STORE .1*FACTOR to FACTOR
ENDDO
* establish decimal point for
* square root of number less than 1.

STORE SQ:ROOT*FACTOR to SQ:ROOT

RELEASE FACTOR,EXPONENT,TEMPORARY,ITERATION,AGAIN,DELTA,Z,ACTION
ERASE

@ 10,10 SAY ' '
DISPLAY MEMORY
* unorthodox way of displaying results necessitated
* by glitch in dBASE II which does not always
* properly report the result.


?
?
?"     Another SQUARE ROOT? -- Y(es) or N(o)?" 

SET CONSOLE off
WAIT to AGAIN
SET CONSOLE on

IF !(AGAIN)='Y'
  LOOP
    ELSE
    SET TALK on
  CANCEL
ENDIF	
* decision to repeat sequence or exit program.

ENDDO
