140 lines
5.1 KiB
Plaintext
140 lines
5.1 KiB
Plaintext
;; Function To Be Tested: ROUND
|
|
;;
|
|
;; Source: Guy L Steele's CLTL
|
|
;; Section: 12.6 Type Conversions and Component Extractions on Numbers
|
|
;; Page: 215
|
|
;;
|
|
;; Created By: Kelly Roach, John Park
|
|
;;
|
|
;; Creation Date: July 12,1986
|
|
;;
|
|
;; Last Update: July 12,1986
|
|
;;
|
|
;; Filed As: {ERIS}<LISPCORE>CML>TEST>12-6-ROUND.TEST
|
|
;;
|
|
;;
|
|
;; Syntax: (ROUND NUMBER &OPTIONAL DIVISOR)
|
|
;;
|
|
;; Function Description:
|
|
;; In the simple one-argument case,
|
|
;; each of these functions converts its argument NUMBER
|
|
;; (which must not be complex) to be an integer.
|
|
;; If the argument is already an integer, it is returned directly.
|
|
;; If the argument is a ratio or floating-point number, the functions use
|
|
;; different algorithms for the conversion.
|
|
;;
|
|
;; FLOOR converts its argument by truncating toward negative
|
|
;; infinity; that is, the result is the largest integer that is not larger
|
|
;; than the argument.
|
|
;;
|
|
;; CEILING converts its argument by truncating toward positive
|
|
;; infinity; that is, the result is the smallest integer that is not smaller
|
|
;; than the argument.
|
|
;;
|
|
;; TRUNCATE converts its argument by truncating toward zero;
|
|
;; that is, the result is the integer of the same sign as the argument
|
|
;; and which has the greatest integral
|
|
;; magnitude not greater than that of the argument.
|
|
;;
|
|
;; ROUND converts its argument by rounding to the nearest
|
|
;; integer; if NUMBER is exactly halfway between two integers
|
|
;; (that is, of the form INTEGER+0.5), then it is rounded to the one that
|
|
;; is even (divisible by two).
|
|
;;
|
|
;; The following table shows what the four functions produce when given
|
|
;; various arguments.
|
|
;;
|
|
;;
|
|
;;
|
|
;;
|
|
;;
|
|
;; 2.6 2 3 2 3
|
|
;; 2.5 2 3 2
|
|
;; 2
|
|
;; 2.4 2 3 2 2
|
|
;; 0.7 0 1 0 1
|
|
;; 0.3 0 1
|
|
;; 0 0
|
|
;; -0.3 -1 0 0 0
|
|
;; -0.7 -1 0 0 -1
|
|
;; -2.4 -3
|
|
;; -2 -2 -2
|
|
;; -2.5 -3 -2 -2 -2
|
|
;; -2.6 -3 -2 -2 -3
|
|
;;
|
|
;;
|
|
;; If a second argument DIVISOR is supplied, then the result
|
|
;; is the appropriate type of rounding or truncation applied to the
|
|
;; result of dividing the NUMBER by the DIVISOR.
|
|
;; For example, (FLOOR 5 2) = (FLOOR (/ 5 2)) but is potentially more
|
|
;; efficient. The DIVISOR may be any non-complex number.
|
|
;; The one-argument case is exactly like the two-argument case where the second
|
|
;; argument is 1.
|
|
;;
|
|
;;
|
|
;;
|
|
;;
|
|
;;
|
|
;; Each of the functions actually returns TWO values,
|
|
;; whether given one or two arguments. The second
|
|
;; result is the remainder and may be obtained using
|
|
;;
|
|
;; macro MULTIPLE-VALUE-BIND and related constructs.
|
|
;; If any of these functions is given two arguments X and Y
|
|
;; and produces results Q and R, then Q*Y+R=X.
|
|
;; The first result Q is always an integer.
|
|
;; The remainder R is an integer if both arguments are integers,
|
|
;; is rational if both arguments are rational,
|
|
;; and is floating-point if either argument is floating-point.
|
|
;; One consequence is that
|
|
;; in the one-argument case the remainder is always a number of the same type
|
|
;; as the argument.
|
|
;;
|
|
;; When only one argument is given, the two results are exact;
|
|
;; the mathematical sum of the two results is always equal to the
|
|
;; mathematical value of the argument.
|
|
;;
|
|
;; Compatibility note: The names of the functions FLOOR, CEILING,
|
|
;; TRUNCATE, and ROUND are more accurate than names like FIX
|
|
;; that have heretofore been used in various Lisp systems.
|
|
;; The names used here are compatible with standard mathematical
|
|
;; terminology (and with PL1, as it happens). In Fortran
|
|
;; IFIX means TRUNCATE. Algol 68 provides ROUND
|
|
;; and uses ENTIER to mean FLOOR.
|
|
;; In Maclisp, FIX and IFIX both
|
|
;; mean FLOOR (one is generic, the other flonum-in/fixnum-out).
|
|
;; In Interlisp, FIX means TRUNCATE.
|
|
;; In Zetalisp, FIX means FLOOR and FIXR means ROUND.
|
|
;; Standard Lisp provides a FIX function but does not
|
|
;; specify precisely what it does. The existing usage
|
|
;; of the name FIX is so confused that it seemed best to avoid it
|
|
;; altogether.
|
|
;;
|
|
;; The names and definitions given here have recently been adopted
|
|
;; by Zetalisp, and Maclisp and NIL seem likely to follow suit.
|
|
;;
|
|
;; Argument(s): NUMBER - a number
|
|
;; DIVISOR - a real
|
|
;;
|
|
;; Returns: a number
|
|
;;
|
|
|
|
(do-test-group round-setup
|
|
:before (progn
|
|
(setq arguments
|
|
'(2.6 2.5 2.4 0.7 0.3 -0.3 -0.7 -2.4 -2.5 -2.6))
|
|
(setq round-result1 '(3 2 2 1 0 0 -1 -2 -2 -3))
|
|
(setq arguments-option '((33 10) (35 10) (36 10)))
|
|
(setq round-result2 '(3 4 4)))
|
|
|
|
(do-test round-test
|
|
(and (setq round-test-result1 (mapcar #'round arguments))
|
|
(equal round-test-result1 round-result1)
|
|
(setq round-test-result2
|
|
(mapcar #'(lambda (x) (append '(round) x))
|
|
arguments-option))
|
|
(equal (mapcar #'eval round-test-result2)
|
|
round-result2))))
|
|
|
|
STOP
|