[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Elisp manual, node "Comparison of Numbers"
From: |
David Kastrup |
Subject: |
Re: Elisp manual, node "Comparison of Numbers" |
Date: |
Mon, 29 May 2006 15:33:31 +0200 |
User-agent: |
Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux) |
"Drew Adams" <address@hidden> writes:
> 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))))
The problem here is that fuzz is a _relative_ measure of equality, and
you employ it as an absolute measure here. I don't think it a good
idea at all that 1e-12 and 0.995e-12 are considered different, while
1e-8 and 0.0 are considered equal.
--
David Kastrup, Kriemhildstr. 15, 44793 Bochum
- Elisp manual, node "Comparison of Numbers", Drew Adams, 2006/05/29
- Re: Elisp manual, node "Comparison of Numbers",
David Kastrup <=
- Re: Elisp manual, node "Comparison of Numbers", Kim F. Storm, 2006/05/29
- Re: Elisp manual, node "Comparison of Numbers", Lennart Borgman, 2006/05/29
- Re: Elisp manual, node "Comparison of Numbers", David Kastrup, 2006/05/29
- Re: Elisp manual, node "Comparison of Numbers", Lennart Borgman, 2006/05/29
- Re: Elisp manual, node "Comparison of Numbers", David Kastrup, 2006/05/29
- Re: Elisp manual, node "Comparison of Numbers", Lennart Borgman, 2006/05/29
Re: Elisp manual, node "Comparison of Numbers", Richard Stallman, 2006/05/29
Re: Elisp manual, node "Comparison of Numbers", Lennart Borgman, 2006/05/29