(lispref.info)Comparison of Numbers


Next: Numeric Conversions Prev: Predicates on Numbers Up: Numbers

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.


automatically generated by info2www