home *** CD-ROM | disk | FTP | other *** search
/ Jason Aller Floppy Collection / 168.img / ACAD3.ZIP / SQR.LSP < prev    next >
Lisp/Scheme  |  1988-05-25  |  1KB  |  38 lines

  1. ;  This is a programming example.
  2. ;
  3. ;  This is an implementation of a square root function in
  4. ;  LISP using the Newton-Raphson method as used in AutoCAD.
  5. ;  It is intended as a test of floating point arithmetic in
  6. ;  our LISP, as you can check accuracy with the statement:
  7. ;     (- (sqr 2) (sqrt 2))
  8. ;  which will compare the built-in function with this one.
  9. ;
  10. ;  John Walker  12/17/84
  11.  
  12. (defun sqr (x / y c cl)
  13.     (if (or (= 'REAL (type x)) (= 'INT (type x)))
  14.         (progn
  15.             (cond ((minusp x) 'Negative-argument)
  16.                   ((zerop x) 0.0)
  17.                   (t (setq y (/ (+ 0.154116 (* x 1.893872))
  18.                                        (+ 1.0 (* x 1.047988))
  19.                              )
  20.                      )
  21.                      (setq c (/ (- y (/ x y)) 2.0))
  22.                      (setq cl 0.0)
  23.                      (while (not (equal c cl))
  24.                         (setq y (- y c))
  25.                         (setq cl c)
  26.                         (setq c (/ (- y (/ x y)) 2.0))
  27.                      )
  28.                      y
  29.                   )
  30.             )
  31.         )
  32.         (progn
  33.             (princ "Invalid argument.")
  34.             (princ)
  35.         )
  36.     )
  37. )
  38.