From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: Elisp manual, node "Comparison of Numbers" Date: Mon, 29 May 2006 15:23:00 -0400 Message-ID: References: NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1148930636 7891 80.91.229.2 (29 May 2006 19:23:56 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Mon, 29 May 2006 19:23:56 +0000 (UTC) Cc: Emacs-Devel Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Mon May 29 21:23:53 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 1FknKg-0001LH-5O for ged-emacs-devel@m.gmane.org; Mon, 29 May 2006 21:23:18 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1FknKf-0004g0-KV for ged-emacs-devel@m.gmane.org; Mon, 29 May 2006 15:23:17 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1FknKU-0004ff-0h for emacs-devel@gnu.org; Mon, 29 May 2006 15:23:06 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1FknKP-0004ch-OZ for emacs-devel@gnu.org; Mon, 29 May 2006 15:23:05 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1FknKP-0004ce-Jl for emacs-devel@gnu.org; Mon, 29 May 2006 15:23:01 -0400 Original-Received: from [209.226.175.4] (helo=tomts16-srv.bellnexxia.net) by monty-python.gnu.org with esmtp (Exim 4.52) id 1FknQ2-0006Jo-K0 for emacs-devel@gnu.org; Mon, 29 May 2006 15:28:50 -0400 Original-Received: from localhost ([70.55.143.78]) by tomts16-srv.bellnexxia.net (InterMail vM.5.01.06.13 201-253-122-130-113-20050324) with ESMTP id <20060529192300.WRDL27612.tomts16-srv.bellnexxia.net@localhost>; Mon, 29 May 2006 15:23:00 -0400 Original-Received: by localhost (Postfix, from userid 20848) id 2DD3D8232; Mon, 29 May 2006 15:23:00 -0400 (EDT) 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:55444 Archived-At: > 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)))) There is no generally good solution to this problem. Depending on the specific problem, one approach will work while that approach will fail miserably in another context. Welcome to the wonderful world of floating point and numerical analysis. I don't think the Elisp manual should try to tackle these kinds of problems, especially given how rarely floats are used in Emacs. Stefan