diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/elisp/comparison-of-numbers.html | |
new repository
Diffstat (limited to 'devdocs/elisp/comparison-of-numbers.html')
| -rw-r--r-- | devdocs/elisp/comparison-of-numbers.html | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/devdocs/elisp/comparison-of-numbers.html b/devdocs/elisp/comparison-of-numbers.html new file mode 100644 index 00000000..a29689fa --- /dev/null +++ b/devdocs/elisp/comparison-of-numbers.html @@ -0,0 +1,45 @@ + <h3 class="section">Comparison of Numbers</h3> <p>To test numbers for numerical equality, you should normally use <code>=</code> instead of non-numeric comparison predicates like <code>eq</code>, <code>eql</code> and <code>equal</code>. Distinct floating-point and large integer objects can be numerically equal. If you use <code>eq</code> to compare them, you test whether they are the same <em>object</em>; if you use <code>eql</code> or <code>equal</code>, you test whether their values are <em>indistinguishable</em>. In contrast, <code>=</code> uses numeric comparison, and sometimes returns <code>t</code> when a non-numeric comparison would return <code>nil</code> and vice versa. See <a href="float-basics">Float Basics</a>. </p> <p>In Emacs Lisp, if two fixnums are numerically equal, they are the same Lisp object. That is, <code>eq</code> is equivalent to <code>=</code> on fixnums. It is sometimes convenient to use <code>eq</code> for comparing an unknown value with a fixnum, because <code>eq</code> does not report an error if the unknown value is not a number—it accepts arguments of any type. By contrast, <code>=</code> signals an error if the arguments are not numbers or markers. However, it is better programming practice to use <code>=</code> if you can, even for comparing integers. </p> <p>Sometimes it is useful to compare numbers with <code>eql</code> or <code>equal</code>, which treat two numbers as equal if they have the same data type (both integers, or both floating point) and the same value. By contrast, <code>=</code> can treat an integer and a floating-point number as equal. See <a href="equality-predicates">Equality Predicates</a>. </p> <p>There is another wrinkle: because floating-point arithmetic is not exact, it is often a bad idea to check for equality of floating-point values. Usually it is better to test for approximate equality. Here’s a function to do this: </p> <div class="example"> <pre class="example">(defvar fuzz-factor 1.0e-6) +(defun approx-equal (x y) + (or (= x y) + (< (/ (abs (- x y)) + (max (abs x) (abs y))) + fuzz-factor))) +</pre> +</div> <dl> <dt id="=">Function: <strong>=</strong> <em>number-or-marker &rest number-or-markers</em> +</dt> <dd><p>This function tests whether all its arguments are numerically equal, and returns <code>t</code> if so, <code>nil</code> otherwise. </p></dd> +</dl> <dl> <dt id="eql">Function: <strong>eql</strong> <em>value1 value2</em> +</dt> <dd><p>This function acts like <code>eq</code> except when both arguments are numbers. It compares numbers by type and numeric value, so that <code>(eql 1.0 1)</code> returns <code>nil</code>, but <code>(eql 1.0 1.0)</code> and <code>(eql 1 1)</code> both return <code>t</code>. This can be used to compare large integers as well as small ones. Floating-point values with the same sign, exponent and fraction are <code>eql</code>. This differs from numeric comparison: <code>(eql 0.0 -0.0)</code> returns <code>nil</code> and <code>(eql 0.0e+NaN 0.0e+NaN)</code> returns <code>t</code>, whereas <code>=</code> does the opposite. </p></dd> +</dl> <dl> <dt id="/=">Function: <strong>/=</strong> <em>number-or-marker1 number-or-marker2</em> +</dt> <dd><p>This function tests whether its arguments are numerically equal, and returns <code>t</code> if they are not, and <code>nil</code> if they are. </p></dd> +</dl> <dl> <dt id="<">Function: <strong><</strong> <em>number-or-marker &rest number-or-markers</em> +</dt> <dd><p>This function tests whether each argument is strictly less than the following argument. It returns <code>t</code> if so, <code>nil</code> otherwise. </p></dd> +</dl> <dl> <dt id="<=">Function: <strong><=</strong> <em>number-or-marker &rest number-or-markers</em> +</dt> <dd><p>This function tests whether each argument is less than or equal to the following argument. It returns <code>t</code> if so, <code>nil</code> otherwise. </p></dd> +</dl> <dl> <dt id=">">Function: <strong>></strong> <em>number-or-marker &rest number-or-markers</em> +</dt> <dd><p>This function tests whether each argument is strictly greater than the following argument. It returns <code>t</code> if so, <code>nil</code> otherwise. </p></dd> +</dl> <dl> <dt id=">=">Function: <strong>>=</strong> <em>number-or-marker &rest number-or-markers</em> +</dt> <dd><p>This function tests whether each argument is greater than or equal to the following argument. It returns <code>t</code> if so, <code>nil</code> otherwise. </p></dd> +</dl> <dl> <dt id="max">Function: <strong>max</strong> <em>number-or-marker &rest numbers-or-markers</em> +</dt> <dd> +<p>This function returns the largest of its arguments. </p> <div class="example"> <pre class="example">(max 20) + ⇒ 20 +(max 1 2.5) + ⇒ 2.5 +(max 1 3 2.5) + ⇒ 3 +</pre> +</div> </dd> +</dl> <dl> <dt id="min">Function: <strong>min</strong> <em>number-or-marker &rest numbers-or-markers</em> +</dt> <dd> +<p>This function returns the smallest of its arguments. </p> <div class="example"> <pre class="example">(min -4 1) + ⇒ -4 +</pre> +</div> </dd> +</dl> <dl> <dt id="abs">Function: <strong>abs</strong> <em>number</em> +</dt> <dd><p>This function returns the absolute value of <var>number</var>. </p></dd> +</dl><div class="_attribution"> + <p class="_attribution-p"> + Copyright © 1990-1996, 1998-2022 Free Software Foundation, Inc. <br>Licensed under the GNU GPL license.<br> + <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Comparison-of-Numbers.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Comparison-of-Numbers.html</a> + </p> +</div> |
