65 lines
2.0 KiB
Plaintext
65 lines
2.0 KiB
Plaintext
;; Function To Be Tested: RATIONALIZE
|
|
;;
|
|
;; Source: Guy L Steele's CLTL
|
|
;; Section: 12.6 Type Conversions and Component Extractions on Numbers
|
|
;; Page: 214
|
|
;;
|
|
;; Created By: Kelly Roach, John Park
|
|
;;
|
|
;; Creation Date: July 12,1986
|
|
;;
|
|
;; Last Update: July 12,1986
|
|
;;
|
|
;; Filed As: {ERIS}<LISPCORE>CML>TEST>12-6-RATIONALIZE.TEST
|
|
;;
|
|
;;
|
|
;; Syntax: (RATIONALIZE NUMBER)
|
|
;;
|
|
;; Function Description:
|
|
;; Each of these functions converts any non-complex number to be a rational
|
|
;; number. If the argument is already rational, it is returned.
|
|
;; The two functions differ in their treatment of floating-point numbers.
|
|
;;
|
|
;; RATIONAL assumes that the floating-point number is completely accurate
|
|
;; and returns a rational number mathematically equal to the precise
|
|
;; value of the floating-point number.
|
|
;;
|
|
;; RATIONALIZE assumes that the
|
|
;; floating-point number is accurate only to the precision of the
|
|
;; floating-point representation, and may return any rational number for
|
|
;; which the floating-point number is the best available approximation of
|
|
;; its format; in doing this it attempts to keep both numerator and
|
|
;; denominator small.
|
|
;;
|
|
;; It is always the case that
|
|
;;
|
|
;; (FLOAT (RATIONAL X) X) = X
|
|
;;
|
|
;; and
|
|
;;
|
|
;; (FLOAT (RATIONALIZE X) X) = X
|
|
;;
|
|
;; That is, rationalizing a floating-point number by either method
|
|
;; and then converting it back
|
|
;; to a floating-point number of the same format produces the original number.
|
|
;; What distinguishes the two functions is that RATIONAL typically
|
|
;; has a simple, inexpensive implementation, whereas RATIONALIZE goes
|
|
;; to more trouble to produce a result that is more pleasant to view and
|
|
;; simpler for some purposes to compute with.
|
|
;;
|
|
;; Argument(s): NUMBER - a number
|
|
;;
|
|
;; Returns: a number
|
|
;;
|
|
|
|
(do-test rationalize-test
|
|
(and (eql (rationalize 10) 10)
|
|
(eql (rationalize 3.1) (/ 31 10))
|
|
(rationalp (rationalize 3.1))
|
|
(eql (float (rationalize 3.1)) 3.1)
|
|
(eql (rationalize (/ 10 5)) 2)
|
|
(rationalp (rationalize (/ 10 5)))
|
|
(eql (rationalize 3E3) 3000)))
|
|
|
|
STOP
|