emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Elisp manual, node "Comparison of Numbers"


From: Lennart Borgman
Subject: Re: Elisp manual, node "Comparison of Numbers"
Date: Mon, 29 May 2006 15:40:08 +0200
User-agent: Thunderbird 1.5.0.2 (Windows/20060308)

Drew Adams wrote:
I blindly got bit by this one. The Elisp manual gives this as an example of
how to test near equality of floating-point numbers:

  (defvar fuzz-factor 1.0e-6)
  (defun approx-equal (x y)
    (or (and (= x 0) (= y 0))
        (< (/ (abs (- x y))
              (max (abs x) (abs y)))
           fuzz-factor)))

When either x or y is 0.0, but not both, this gives nil no matter how close
the other number is to zero. I think this is more like what is needed:

  (defun approx-equal (x y &optional fuzz)
    (setq fuzz (or fuzz 1.0e-8))
    (cond ((= x 0.0) (< y fuzz))
          ((= y 0.0) (< x fuzz))
          (t (< (/ (abs (- x y)) (max (abs x) (abs y))) fuzz))))

Looks reasonable, but you have to use

   (< (abs y) fuzz)

etc.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]