From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Lennart Borgman Newsgroups: gmane.emacs.devel Subject: Re: Elisp manual, node "Comparison of Numbers" Date: Mon, 29 May 2006 15:54:05 +0200 Message-ID: <447AFCFD.6030405@student.lu.se> References: <85fyitc5w4.fsf@lola.goethe.zz> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: sea.gmane.org 1148910894 32038 80.91.229.2 (29 May 2006 13:54:54 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Mon, 29 May 2006 13:54:54 +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:54: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 1FkiCb-0002u7-S8 for ged-emacs-devel@m.gmane.org; Mon, 29 May 2006 15:54:38 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1FkiCb-00028v-GU for ged-emacs-devel@m.gmane.org; Mon, 29 May 2006 09:54:37 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1FkiCP-00028d-Oi for emacs-devel@gnu.org; Mon, 29 May 2006 09:54:25 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1FkiCM-00027c-2s for emacs-devel@gnu.org; Mon, 29 May 2006 09:54:25 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1FkiCL-00027Z-T4 for emacs-devel@gnu.org; Mon, 29 May 2006 09:54:21 -0400 Original-Received: from [81.228.8.164] (helo=pne-smtpout2-sn2.hy.skanova.net) by monty-python.gnu.org with esmtp (Exim 4.52) id 1FkiHv-0004fq-1q; Mon, 29 May 2006 10:00:07 -0400 Original-Received: from [192.168.123.121] (83.249.218.244) by pne-smtpout2-sn2.hy.skanova.net (7.2.072.1) id 447AF0DC000047BE; Mon, 29 May 2006 15:54:07 +0200 User-Agent: Thunderbird 1.5.0.2 (Windows/20060308) Original-To: "Kim F. Storm" In-Reply-To: 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:55428 Archived-At: Kim F. Storm wrote: > 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! > Hm. It is the relative distance from 0.