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 >
Wrap
Lisp/Scheme
|
1989-09-10
|
3KB
|
74 lines
; ROUND2.LSP
;
; (round <num> <prec> )
;
; Rounds numbers off to nearest fraction, decimal places, or whole number
; in accordance with the following:
;
; If PREC is ZERO, then NUM is rounded to the nearest WHOLE NUMBER.
;
; If PREC is less than 1, then NUM is rounded to the nearest PREC,
; where PREC represents the decimal eqivalent of the desired fraction.
;
; If PREC is >= 1, then NUM is rounded to PREC decimal places.
;
; The first argument <num> must be a real!
;
; Examples: (ROUND 15.2 0.25) returns 15.25 (nearest 1/4)
; (ROUND 15.1 0.25) returns 15.00 ( " " )
; (ROUND 1.66666666 4) returns 1.66670000 (4 decimal places)
; (ROUND 3.14159265 6) returns 3.14159300 (6 " " )
; (ROUND 43.6667 0) returns 44.00000000 (whole number)
; (ROUND 31.87765 1) returns 31.90000000 (1 decimal place)
; By Duff Kurland - Autodesk, Inc. August 22, 1986
; Modified by A. Tanzillo, January 7, 1987
(defun round (num prec / over half)
(setq
prec (cond
((zerop prec) 1 ) ; round to whole number
((>= prec 1) (expt 10.0 (- prec)) ) ; round to decimal places
(T prec ) ; round to fraction
)
half (/ prec 2.0)
over (rem (setq num (float num)) prec) ; Get remainder
)
(if (>= over half)
(+ num prec (- over)) ; Round up
(- num over) ; Round down
)
)
; fround.lsp by A. Tanzillo, January 7, 1987
; rounds fractions to current precision of architectural units
; (fround <num> )
; returns num rounded to nearest fraction or whole number as determined
; by current unit precision setting (sys. var. "luprec")
; requires round.lsp
; (fround) will return a whole number if system variable "luprec" = 0
;
;
; Examples:
;
;
; Arch. unit precision
; (getvar "luprec") expression
; ------------------------------------------------------------
; 0 (no fractions) (fround 44.3364) returns 44.000000
; 1 (1/2") (fround 4.3275) returns 4.500000
; 3 (1/8") (fround 12.3667) returns 12.375000
; 5 (1/32") (fround 0.85534) returns 0.843750
(defun fround(num)
(round num
(/ 1.0 (cadr (assoc (getvar "luprec")
'((0 0) (1 2) (2 4) (3 8) (4 16) (5 32) (6 64))))))
)