home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / tipi / sqrt.tpi < prev    next >
Text File  |  1993-11-07  |  668b  |  36 lines

  1. # SQRT.TPI
  2. # A fixed point square root routine
  3. # by Kent Peterson
  4.  
  5. defvar root
  6. defvar x
  7.  
  8. define guess
  9.  x fetch 2 / root store
  10. enddef
  11.  
  12. define sqrt
  13.  x fetch root fetch /
  14.  root fetch + 2 /
  15.  root fetch over
  16.  > if root store sqrt endif
  17. enddef
  18.  
  19. "Pick a number: " print$
  20. getnum dup
  21. "The square root of " print$
  22. print
  23. " is " print$
  24. 1000000 *    # Multiply the number up to get 3
  25.         # decimal place accuracy
  26. x store
  27. guess
  28. sqrt
  29. dup 1000 /     # Divide to get the integer part
  30. dup print       # print the integer part
  31. "." print$      # print the decimal point
  32. 1000 * -     # Get the fractional part
  33. print cr        # and print it.
  34. begin key until
  35.  
  36.