From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: storm@cua.dk (Kim F. Storm) Newsgroups: gmane.emacs.devel Subject: Re: Elisp manual, node "Comparison of Numbers" Date: Mon, 29 May 2006 15:42:00 +0200 Message-ID: References: <85fyitc5w4.fsf@lola.goethe.zz> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1148910282 29770 80.91.229.2 (29 May 2006 13:44:42 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Mon, 29 May 2006 13:44:42 +0000 (UTC) Cc: Drew Adams , Emacs-Devel Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Mon May 29 15:44:40 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 1Fki2w-0000NA-3j for ged-emacs-devel@m.gmane.org; Mon, 29 May 2006 15:44:39 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Fki2v-00058V-L6 for ged-emacs-devel@m.gmane.org; Mon, 29 May 2006 09:44:37 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Fki2h-00057S-60 for emacs-devel@gnu.org; Mon, 29 May 2006 09:44:23 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Fki2f-00057C-Et for emacs-devel@gnu.org; Mon, 29 May 2006 09:44:22 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Fki2f-000576-8B for emacs-devel@gnu.org; Mon, 29 May 2006 09:44:21 -0400 Original-Received: from [195.41.46.236] (helo=pfepb.post.tele.dk) by monty-python.gnu.org with esmtp (Exim 4.52) id 1Fki8C-0003OH-Oa; Mon, 29 May 2006 09:50:04 -0400 Original-Received: from kfs-l.imdomain.dk.cua.dk (unknown [80.165.4.124]) by pfepb.post.tele.dk (Postfix) with SMTP id 6E372A500AA; Mon, 29 May 2006 15:44:16 +0200 (CEST) Original-To: David Kastrup In-Reply-To: <85fyitc5w4.fsf@lola.goethe.zz> (David Kastrup's message of "Mon, 29 May 2006 15:33:31 +0200") 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:55427 Archived-At: David Kastrup writes: > "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. Agree! And: (approx-equal 0.0 -1.0e10) => t -- Kim F. Storm http://www.cua.dk