Copyright (C) 2000-2012 |
GNU Info (elisp)Comparison of NumbersComparison of Numbers ===================== To test numbers for numerical equality, you should normally use `=', not `eq'. 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_. By contrast, `=' compares only the numeric values of the objects. At present, each integer value has a unique Lisp object in Emacs Lisp. 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 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. Sometimes it is useful to compare numbers with `equal'; it treats two numbers as equal if they have the same data type (both integers, or both floating point) and the same value. By contrast, `=' can treat an integer and a floating point number as equal. 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) (or (and (= x 0) (= y 0)) (< (/ (abs (- x y)) (max (abs x) (abs y))) fuzz-factor))) Common Lisp note: Comparing numbers in Common Lisp always requires `=' because Common Lisp implements multi-word integers, and two distinct integer objects can have the same numeric value. Emacs Lisp can have just one integer object for any given value because it has a limited range of integer values. - Function: = number-or-marker1 number-or-marker2 This function tests whether its arguments are numerically equal, and returns `t' if so, `nil' otherwise. - Function: /= number-or-marker1 number-or-marker2 This function tests whether its arguments are numerically equal, and returns `t' if they are not, and `nil' if they are. - 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. If any of the argument is floating-point, the value is returned as floating point, even if it was given as an integer. (max 20) => 20 (max 1 2.5) => 2.5 (max 1 3 2.5) => 3.0 - Function: min number-or-marker &rest numbers-or-markers This function returns the smallest of its arguments. If any of the argument is floating-point, the value is returned as floating point, even if it was given as an integer. (min -4 1) => -4 - Function: abs number This function returns the absolute value of NUMBER. automatically generated by info2www version 1.2.2.9 |