home *** CD-ROM | disk | FTP | other *** search
/ Share Gallery 1 / share_gal_1.zip / share_gal_1 / GR / GR505.ZIP / LSP.EXE / ROUND2.LSP < prev    next >
Lisp/Scheme  |  1989-09-10  |  3KB  |  74 lines

  1. ;   ROUND2.LSP
  2. ;
  3. ;   (round <num> <prec> )
  4. ;
  5. ;   Rounds numbers off to nearest fraction, decimal places, or whole number
  6. ;   in accordance with the following:
  7. ;
  8. ;       If PREC is ZERO, then NUM is rounded to the nearest WHOLE NUMBER.
  9. ;
  10. ;       If PREC is less than 1, then NUM is rounded to the nearest PREC,
  11. ;       where PREC represents the decimal eqivalent of the desired fraction.
  12. ;
  13. ;       If PREC is >= 1, then NUM is rounded to PREC decimal places.
  14. ;
  15. ;   The first argument <num> must be a real!
  16. ;
  17. ;  Examples:  (ROUND 15.2 0.25)      returns 15.25          (nearest 1/4)
  18. ;             (ROUND 15.1 0.25)      returns 15.00          ( "       " )
  19. ;             (ROUND 1.66666666 4)   returns 1.66670000     (4 decimal places)
  20. ;             (ROUND 3.14159265 6)   returns 3.14159300     (6   "       "   )
  21. ;             (ROUND 43.6667 0)      returns 44.00000000    (whole number)
  22. ;             (ROUND 31.87765 1)     returns 31.90000000    (1 decimal place)
  23.  
  24. ;  By Duff Kurland - Autodesk, Inc.   August 22, 1986
  25.  
  26. ;  Modified by A. Tanzillo,  January 7, 1987
  27.  
  28. (defun round (num prec / over half)
  29.   (setq
  30.     prec  (cond
  31.             ((zerop prec) 1 )                    ; round to whole number
  32.             ((>= prec 1) (expt 10.0 (- prec)) )  ; round to decimal places
  33.             (T prec )                            ; round to fraction
  34.           )
  35.     half (/ prec 2.0)
  36.     over (rem (setq num (float num)) prec) ; Get remainder
  37.   )
  38.   (if (>= over half)
  39.     (+ num prec (- over))     ; Round up
  40.     (- num over)              ; Round down
  41.   )
  42. )
  43.  
  44. ; fround.lsp  by A. Tanzillo, January 7, 1987
  45.  
  46. ; rounds fractions to current precision of architectural units
  47.  
  48. ; (fround <num> )
  49.  
  50. ; returns num rounded to nearest fraction or whole number as determined
  51. ; by current unit precision setting (sys. var. "luprec")
  52. ; requires round.lsp
  53. ; (fround) will return a whole number if system variable "luprec" = 0
  54. ;
  55. ;
  56. ; Examples:
  57. ;
  58. ;
  59. ;  Arch. unit precision
  60. ;  (getvar "luprec")        expression
  61. ;  ------------------------------------------------------------
  62. ;    0 (no fractions)   (fround 44.3364)   returns   44.000000
  63. ;    1 (1/2")           (fround 4.3275)    returns   4.500000
  64. ;    3 (1/8")           (fround 12.3667)   returns   12.375000
  65. ;    5 (1/32")          (fround 0.85534)   returns   0.843750
  66.  
  67.  
  68.  
  69. (defun fround(num)
  70.   (round num
  71.     (/ 1.0 (cadr (assoc (getvar "luprec")
  72.      '((0 0) (1 2) (2 4) (3 8) (4 16) (5 32) (6 64))))))
  73. )
  74.