Go to the previous, next section.

Comparison of Numbers

Floating point numbers in Emacs Lisp actually take up storage, and there can be many distinct floating point number objects with the same numeric value. If you use eq to compare them, then you test whether two values are the same object. If you want to compare just the numeric values, use =.

If you use eq to compare two integers, it always returns t if they have the same value. This is sometimes useful, because eq accepts arguments of any type and never causes an error, whereas = signals an error if the arguments are not numbers or markers. However, it is a good idea to use = if you can, even for comparing integers, just in case we change the representation of integers in a future Emacs version.

There is another wrinkle: because floating point arithmetic is not exact, it is often a bad idea to check for equality of two floating point values. Usually it is better to test for approximate equality. Here's a function to do this:

(defvar fuzz-factor 1.0e-6)

(defun approx-equal (x y)
  (< (/ (abs (- x y))
        (max (abs x) (abs y)))
     fuzz-factor))

Common Lisp note: because of the way numbers are implemented in Common Lisp, you generally need to use `=' to test for equality between numbers of any kind.

Function: = number-or-marker1 number-or-marker2

This function tests whether its arguments are the same number, and returns t if so, nil otherwise.

Function: /= number-or-marker1 number-or-marker2

This function tests whether its arguments are not the same number, and returns t if so, nil otherwise.

Function: < number-or-marker1 number-or-marker2

This function tests whether its first argument is strictly less than its second argument. It returns t if so, nil otherwise.

Function: <= number-or-marker1 number-or-marker2

This function tests whether its first argument is less than or equal to its second argument. It returns t if so, nil otherwise.

Function: > number-or-marker1 number-or-marker2

This function tests whether its first argument is strictly greater than its second argument. It returns t if so, nil otherwise.

Function: >= number-or-marker1 number-or-marker2

This function tests whether its first argument is greater than or equal to its second argument. It returns t if so, nil otherwise.

Function: max number-or-marker &rest numbers-or-markers

This function returns the largest of its arguments.

(max 20)
     => 20
(max 1 2)
     => 2
(max 1 3 2)
     => 3

Function: min number-or-marker &rest numbers-or-markers

This function returns the smallest of its arguments.

Go to the previous, next section.