home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 13 / 13.iso / p / p024 / 5.img / R11SUPP.EXE / SQR.LSP < prev    next >
Encoding:
Lisp/Scheme  |  1990-07-30  |  1.7 KB  |  53 lines

  1. ;;; --------------------------------------------------------------------------;
  2. ;;; SQR.LSP
  3. ;;;   Copyright (C) 1990 by Autodesk, Inc.
  4. ;;;  
  5. ;;;   Permission to use, copy, modify, and distribute this software and its
  6. ;;;   documentation for any purpose and without fee is hereby granted.  
  7. ;;;
  8. ;;;   THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY. 
  9. ;;;   ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF 
  10. ;;;   MERCHANTABILITY ARE HEREBY DISCLAIMED.
  11. ;;;
  12. ;;; --------------------------------------------------------------------------;
  13. ;;; DESCRIPTION
  14. ;;;
  15. ;;;   This is a programming example.
  16. ;;;
  17. ;;;   This is an implementation of a square root function in
  18. ;;;   LISP using the Newton-Raphson method as used in AutoCAD.
  19. ;;;   It is intended as a test of floating point arithmetic in
  20. ;;;   our LISP, as you can check accuracy with the statement:
  21. ;;;      (- (sqr 2) (sqrt 2))
  22. ;;;   which will compare the built-in function with this one.
  23. ;;;   
  24. ;;;   John Walker  12/17/84
  25. ;;;
  26. ;;; --------------------------------------------------------------------------;
  27.  
  28. (defun sqr (x / y c cl) 
  29.   (if (or (= 'REAL(type x)) (= 'INT(type x))) 
  30.     (progn
  31.       (cond ((minusp x) 'Negative-argument) 
  32.         ((zerop x) 0.0) 
  33.         (t (setq y (/ (+ 0.154116 (* x 1.893872)) (+ 1.0 (* x 1.047988))))
  34.            (setq c (/ (- y (/ x y)) 2.0))
  35.            (setq cl 0.0)
  36.            (while (not (equal c cl)) 
  37.              (setq y (- y c))
  38.              (setq cl c)
  39.              (setq c (/ (- y (/ x y)) 2.0))
  40.            ) y
  41.         )
  42.       )
  43.     ) 
  44.     (progn
  45.       (princ "Invalid argument.") 
  46.       (princ)
  47.     )
  48.   )
  49.  
  50. ;;; --------------------------------------------------------------------------;
  51.  
  52.