[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [RFC] bytecomp: simple source-level optimization

From: David Kastrup
Subject: Re: [RFC] bytecomp: simple source-level optimization
Date: Mon, 23 Jun 2014 11:56:57 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4.50 (gnu/linux)

Dmitry Antipov <address@hidden> writes:

> Source forms like:
> (if (= (point) (point-min)) ...)
> or:
> (if (eq (point-max) (point)) ...)
> are widely used,

The latter is a bad idea.  You cannot reliably compare integers with
eq in Common Lisp, cf

    However, numbers with the same value need not be eq [...]

The current Elisp implementation in Emacs (though possibly not in
XEmacs, and definitely not in GUILE) _does_ implement all integers as
immediate values, but that may well change in future:

    GNU Guile 2.0.9
    Copyright (C) 1995-2013 Free Software Foundation, Inc.

    Guile comes with ABSOLUTELY NO WARRANTY; for details type `,show w'.
    This program is free software, and you are welcome to redistribute it
    under certain conditions; type `,show c' for details.

    Enter `,help' for help.
    scheme@(guile-user)> (eq? 5 5)
    $1 = #t
    scheme@(guile-user)> (eq? 1234567890123 1234567890123)
    $2 = #f
    scheme@(guile-user)> (let ((x 1234567890123)) (eq? x x))
    $3 = #t

Even the Elisp manual states that using '=' is "better programming
practice".  When one wants to avoid the error cases of '=', eql might be
the better choice.

3.4 Comparison of Numbers

To test numbers for numerical equality, you should normally use ‘=’, not
‘eq’.  There can be many distinct floating-point objects with the same
numeric value.  If you use ‘eq’ to compare them, then you test whether
two values are the same _object_.  By contrast, ‘=’ compares only the
numeric values of the objects.

   In Emacs Lisp, each integer is a unique Lisp object.  Therefore, ‘eq’
is equivalent to ‘=’ where integers are concerned.  It is sometimes
convenient to use ‘eq’ for comparing an unknown value with an integer,
because ‘eq’ does not report an error if the unknown value is not a
number—it accepts arguments of any type.  By contrast, ‘=’ signals an
error if the arguments are not numbers or markers.  However, it is
better programming practice to use ‘=’ if you can, even for comparing

David Kastrup

reply via email to

[Prev in Thread] Current Thread [Next in Thread]