unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: David Kastrup <dak@gnu.org>
Cc: Emacs-Devel <emacs-devel@gnu.org>,
	Drew Adams <drew.adams@oracle.com>, "Kim F. Storm" <storm@cua.dk>
Subject: Re: Elisp manual, node "Comparison of Numbers"
Date: Mon, 29 May 2006 16:08:45 +0200	[thread overview]
Message-ID: <85bqtgditu.fsf@lola.goethe.zz> (raw)
In-Reply-To: <447AFCFD.6030405@student.lu.se> (Lennart Borgman's message of "Mon, 29 May 2006 15:54:05 +0200")

Lennart Borgman <lennart.borgman.073@student.lu.se> writes:

> Kim F. Storm wrote:
>> David Kastrup <dak@gnu.org> writes:
>>
>>   
>>> "Drew Adams" <drew.adams@oracle.com> 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.

It is merely the distance from 0, not the "relative distance".

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

  reply	other threads:[~2006-05-29 14:08 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-05-29 13:20 Elisp manual, node "Comparison of Numbers" Drew Adams
2006-05-29 13:33 ` David Kastrup
2006-05-29 13:42   ` Kim F. Storm
2006-05-29 13:54     ` Lennart Borgman
2006-05-29 14:08       ` David Kastrup [this message]
2006-05-29 14:18         ` Lennart Borgman
2006-05-29 14:23           ` David Kastrup
2006-05-29 14:40             ` Lennart Borgman
2006-05-29 21:28   ` Richard Stallman
2006-05-29 13:40 ` Lennart Borgman
2006-05-29 18:14   ` Drew Adams
2006-05-30  3:46     ` Richard Stallman
2006-05-29 19:23 ` Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=85bqtgditu.fsf@lola.goethe.zz \
    --to=dak@gnu.org \
    --cc=drew.adams@oracle.com \
    --cc=emacs-devel@gnu.org \
    --cc=storm@cua.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).