3 (defun rationalize (val) ; hopefully readable conversion
4 (unless (typep val
'flonum
)
5 (if (typep val
'rational
)
6 (return-from rationalize val
)
7 (error "~s is invalid type" val
)))
8 (let ((fraction (abs (rem val
1.0))))
11 (let ((limit (expt 10 (- (+ 7 (truncate (log fraction
10)))
12 (max 0 (truncate (log (abs val
) 10))))))
14 (cond ((>= limit
10000) ; allow primes 3 3 7 11 13
15 (setq limit
(* 9009 (/ limit
10000))))
16 ((>= limit
1000) ; allow primes 3 3 7 11
17 (setq limit
(* 693 (/ limit
1000))))
18 ((>= limit
100) ; allow primes 3 3 7
19 (setq limit
(* 63 (/ limit
100)))))
20 (setq divisor
(round (/ limit fraction
)))
22 (round val
) ; Doesn't fit
23 (/ (round (* val divisor
)) divisor
))))))