Re: Floating-point arithmetic in CL

sketerpot_at_gmail.com
Date: 11/22/04


Date: 22 Nov 2004 13:24:29 -0800

Try this code:

(defmacro define-exact-binary-operation (name)
(let ((x (gensym))
(y (gensym)))
`(defun ,(intern (format nil "~AE" name)) (,x ,y)
(float (,name (rationalize ,x)
(rationalize ,y))))))

(define-exact-binary-operation +)
(define-exact-binary-operation -)
(define-exact-binary-operation *)
(define-exact-binary-operation /)
;; insert any more you want done with rationals inside

(- 0.9 0.5) ; inexact; darn floats
(-e 0.9 0.5) ; As exact as I can get it while still dealing with floats

If I made a mistake here, I invite anybody better-versed in
floating-point arithmetic to point it out to me. It's a definite
possibility.

-Peter Scott



Relevant Pages