From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: David Kastrup Newsgroups: gmane.emacs.devel Subject: Re: Elisp manual, node "Comparison of Numbers" Date: Mon, 29 May 2006 15:33:31 +0200 Message-ID: <85fyitc5w4.fsf@lola.goethe.zz> References: NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1148909661 27349 80.91.229.2 (29 May 2006 13:34:21 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Mon, 29 May 2006 13:34:21 +0000 (UTC) Cc: Emacs-Devel Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Mon May 29 15:34:18 2006 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by ciao.gmane.org with esmtp (Exim 4.43) id 1Fkhsu-0006yz-5W for ged-emacs-devel@m.gmane.org; Mon, 29 May 2006 15:34:17 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Fkhst-0006Ly-Kl for ged-emacs-devel@m.gmane.org; Mon, 29 May 2006 09:34:15 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Fkhsf-0006LX-B5 for emacs-devel@gnu.org; Mon, 29 May 2006 09:34:01 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Fkhsd-0006LL-RC for emacs-devel@gnu.org; Mon, 29 May 2006 09:34:00 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Fkhsd-0006LI-NI for emacs-devel@gnu.org; Mon, 29 May 2006 09:33:59 -0400 Original-Received: from [199.232.76.164] (helo=fencepost.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.52) id 1FkhyD-0002GS-EB for emacs-devel@gnu.org; Mon, 29 May 2006 09:39:45 -0400 Original-Received: from localhost ([127.0.0.1] helo=lola.goethe.zz) by fencepost.gnu.org with esmtp (Exim 4.34) id 1Fkhsd-0004SL-14; Mon, 29 May 2006 09:33:59 -0400 Original-Received: by lola.goethe.zz (Postfix, from userid 1002) id C43881C4D3B1; Mon, 29 May 2006 15:33:31 +0200 (CEST) Original-To: "Drew Adams" In-Reply-To: (Drew Adams's message of "Mon, 29 May 2006 06:20:20 -0700") User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:55425 Archived-At: "Drew Adams" 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