unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Elisp manual, node "Comparison of Numbers"
@ 2006-05-29 13:20 Drew Adams
  2006-05-29 13:33 ` David Kastrup
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Drew Adams @ 2006-05-29 13:20 UTC (permalink / raw)


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))))

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2006-05-30  3:46 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-05-29 13:20 Elisp manual, node "Comparison of Numbers" Drew Adams
2006-05-29 13:33 ` David Kastrup
2006-05-29 13:42   ` Kim F. Storm
2006-05-29 13:54     ` Lennart Borgman
2006-05-29 14:08       ` David Kastrup
2006-05-29 14:18         ` Lennart Borgman
2006-05-29 14:23           ` David Kastrup
2006-05-29 14:40             ` Lennart Borgman
2006-05-29 21:28   ` Richard Stallman
2006-05-29 13:40 ` Lennart Borgman
2006-05-29 18:14   ` Drew Adams
2006-05-30  3:46     ` Richard Stallman
2006-05-29 19:23 ` Stefan Monnier

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).