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:40:08 +0200 Message-ID: <447AF9B8.2080801@student.lu.se> References: 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 1148910033 28908 80.91.229.2 (29 May 2006 13:40:33 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Mon, 29 May 2006 13:40:33 +0000 (UTC) Cc: Emacs-Devel Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Mon May 29 15:40:30 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 1Fkhyu-00088i-If for ged-emacs-devel@m.gmane.org; Mon, 29 May 2006 15:40:28 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Fkhyu-0002AR-3V for ged-emacs-devel@m.gmane.org; Mon, 29 May 2006 09:40:28 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Fkhyg-0002AG-9R for emacs-devel@gnu.org; Mon, 29 May 2006 09:40:14 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Fkhye-0002A4-OO for emacs-devel@gnu.org; Mon, 29 May 2006 09:40:14 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Fkhye-0002A1-KN for emacs-devel@gnu.org; Mon, 29 May 2006 09:40:12 -0400 Original-Received: from [81.228.11.98] (helo=pne-smtpout1-sn1.fre.skanova.net) by monty-python.gnu.org with esmtp (Exim 4.52) id 1Fki4D-0002qD-TQ for emacs-devel@gnu.org; Mon, 29 May 2006 09:45:58 -0400 Original-Received: from [192.168.123.121] (83.249.218.244) by pne-smtpout1-sn1.fre.skanova.net (7.2.072.1) id 447AEF070000405F; Mon, 29 May 2006 15:40:09 +0200 User-Agent: Thunderbird 1.5.0.2 (Windows/20060308) Original-To: Drew Adams 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:55426 Archived-At: Drew Adams wrote: > 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)))) > > Looks reasonable, but you have to use (< (abs y) fuzz) etc.