Changeset 325
- Timestamp:
- Jan 17, 2004, 7:55:58 PM (21 years ago)
- File:
-
- 1 edited
-
trunk/ccl/lib/numbers.lisp (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/ccl/lib/numbers.lisp
r273 r325 363 363 364 364 #| 365 (defun rationalize (number) 366 (cond ((not (floatp number)) (rational number)) 367 ((minusp number) (- (rationalize (- number)))) 368 ((zerop number) 0) 369 (t (let ((onum 1) (oden 0) num (den 1) rem) 370 (multiple-value-setq (num rem) (truncate number)) 371 (until (<= (abs (/ (- number (/ (float num) den)) number)) 372 single-float-epsilon) 373 (multiple-value-bind (q r) (truncate 1.0 rem) 374 (setq rem (/ r rem)) 375 (let ((nnum (+ (* q num) onum))) 376 (setq onum num num nnum)) 377 (let ((nden (+ (* q den) oden))) 378 (setq oden den den nden)))) 379 (/ num den))))) 380 ;Rationalize failed. Input 1.9998229581607005 Rational 70903515/35454896 Float 1.9998229581607008 381 ; also gets overflow and underflow sometimes 365 (defun rationalize (x) 366 (etypecase x 367 (rational x) 368 (real 369 (cond ((minusp x) (- (rationalize (- x)))) 370 ((zerop x) 0) 371 (t 372 (let ((eps (etypecase x 373 (single-float single-float-epsilon) 374 (double-float double-float-epsilon))) 375 (y ()) 376 (a ())) 377 (do ((xx x (setq y (/ (float 1.0 x) (- xx (float a x))))) 378 (num (setq a (truncate x)) 379 (+ (* (setq a (truncate y)) num) onum)) 380 (den 1 (+ (* a den) oden)) 381 (onum 1 num) 382 (oden 0 den)) 383 ((and (not (zerop den)) 384 (not (> (abs (/ (- x (/ (float num x) 385 (float den x))) 386 x)) 387 eps))) 388 (integer-/-integer num den))))))))) 382 389 |# 383 ; Kalman's more better one 390 384 391 (defun rationalize (number) 385 392 (if (floatp number) … … 398 405 (values (+ den (* (1- term) num)) num))))) 399 406 (multiple-value-bind (fraction exponent sign) (integer-decode-float number) 400 ; the first 2 tests may be unnecessary - I think the check for denormalized 401 ; is compensating for a bug in 3.0 re floating a rational (in order to pass tests in ppc-test-arith). 407 ;; the first 2 tests may be unnecessary - I think the check 408 ;; for denormalized is compensating for a bug in 3.0 re 409 ;; floating a rational (in order to pass tests in 410 ;; ppc-test-arith). 402 411 (if (or (and (typep number 'double-float) ; is it denormalized 403 412 (eq exponent #.(nth-value 1 (integer-decode-float least-positive-double-float)))) ; aka -1074)) 404 (and nil ;(typep number 'short-float) ; was needed to pass tests but bug was elsewhere 405 ; this won't cross compile but I assume we don't worry about that any more. 406 (eq exponent #.(nth-value 1 (integer-decode-float least-positive-short-float)))) ; aka -149)) 413 (eq exponent #.(nth-value 1 (integer-decode-float least-positive-short-float))) ; aka -149)) 407 414 (zerop (logand fraction (1- fraction)))) ; or a power of two 408 415 (rational number) … … 526 533 (and (>= op 0) 527 534 (<= op 15)))) 528 (report-bad-arg op '(integer 0 16))) 529 (funcall (%svref *boole-ops* op) integer1 integer2)) 535 (report-bad-arg op '(integer 0 15))) 536 (funcall (%svref *boole-ops* op) 537 (require-type integer1 'integer) 538 (require-type integer2 'integer))) 530 539 531 540
Note:
See TracChangeset
for help on using the changeset viewer.
