aboutsummaryrefslogtreecommitdiffstats
path: root/doc
diff options
context:
space:
mode:
authorPaul Eggert2018-07-19 13:29:28 -0700
committerPaul Eggert2018-07-19 13:30:43 -0700
commit96d77f9eb882b68e994e187ed9c2156a23e3279d (patch)
tree51866456027141aaa234c5180c1d74b985fe2fdb /doc
parent3a91c5e4a2684b04b86356a32b7ec57145ceba95 (diff)
downloademacs-96d77f9eb882b68e994e187ed9c2156a23e3279d.tar.gz
emacs-96d77f9eb882b68e994e187ed9c2156a23e3279d.zip
Improve doc for floating point ‘=’ vs ‘eql’
* doc/lispref/numbers.texi (Float Basics, Comparison of Numbers): Improve documentation of ‘=’ vs ‘eq’, ‘eql’ and ‘equal’ when NaNs and signed zeros are involved.
Diffstat (limited to 'doc')
-rw-r--r--doc/lispref/numbers.texi31
1 files changed, 21 insertions, 10 deletions
diff --git a/doc/lispref/numbers.texi b/doc/lispref/numbers.texi
index 6c51b849d35..70bb1030411 100644
--- a/doc/lispref/numbers.texi
+++ b/doc/lispref/numbers.texi
@@ -223,7 +223,7 @@ least one digit after any decimal point in a floating-point number;
223@samp{1500.} is an integer, not a floating-point number. 223@samp{1500.} is an integer, not a floating-point number.
224 224
225 Emacs Lisp treats @code{-0.0} as numerically equal to ordinary zero 225 Emacs Lisp treats @code{-0.0} as numerically equal to ordinary zero
226with respect to @code{equal} and @code{=}. This follows the 226with respect to numeric comparisons like @code{=}. This follows the
227@acronym{IEEE} floating-point standard, which says @code{-0.0} and 227@acronym{IEEE} floating-point standard, which says @code{-0.0} and
228@code{0.0} are numerically equal even though other operations can 228@code{0.0} are numerically equal even though other operations can
229distinguish them. 229distinguish them.
@@ -232,19 +232,26 @@ distinguish them.
232@cindex negative infinity 232@cindex negative infinity
233@cindex infinity 233@cindex infinity
234@cindex NaN 234@cindex NaN
235@findex eql
236@findex sxhash-eql
237 The @acronym{IEEE} floating-point standard supports positive 235 The @acronym{IEEE} floating-point standard supports positive
238infinity and negative infinity as floating-point values. It also 236infinity and negative infinity as floating-point values. It also
239provides for a class of values called NaN, or ``not a number''; 237provides for a class of values called NaN, or ``not a number'';
240numerical functions return such values in cases where there is no 238numerical functions return such values in cases where there is no
241correct answer. For example, @code{(/ 0.0 0.0)} returns a NaN@. 239correct answer. For example, @code{(/ 0.0 0.0)} returns a NaN@.
242A NaN is never numerically equal to any value, not even to itself. 240A NaN is never numerically equal to any value, not even to itself.
243NaNs carry a sign and a significand, and non-numeric functions like 241NaNs carry a sign and a significand, and non-numeric functions treat
244@code{eql} and @code{sxhash-eql} treat two NaNs as equal when their 242two NaNs as equal when their
245signs and significands agree. Significands of NaNs are 243signs and significands agree. Significands of NaNs are
246machine-dependent and are not directly visible to Emacs Lisp. 244machine-dependent and are not directly visible to Emacs Lisp.
247 245
246 When NaNs and signed zeros are involved, non-numeric functions like
247@code{eql}, @code{equal}, @code{sxhash-eql}, @code{sxhash-equal} and
248@code{gethash} determine whether values are indistinguishable, not
249whether they are numerically equal. For example, when @var{x} and
250@var{y} are the same NaN, @code{(equal x y)} returns @code{t} whereas
251@code{(= x y)} uses numeric comparison and returns @code{nil};
252conversely, @code{(equal 0.0 -0.0)} returns @code{nil} whereas
253@code{(= 0.0 -0.0)} returns @code{t}.
254
248Here are read syntaxes for these special floating-point values: 255Here are read syntaxes for these special floating-point values:
249 256
250@table @asis 257@table @asis
@@ -359,11 +366,15 @@ if so, @code{nil} otherwise. The argument must be a number.
359@cindex comparing numbers 366@cindex comparing numbers
360 367
361 To test numbers for numerical equality, you should normally use 368 To test numbers for numerical equality, you should normally use
362@code{=}, not @code{eq}. There can be many distinct floating-point 369@code{=} instead of non-numeric comparison predicates like @code{eq},
363objects with the same numeric value. If you use @code{eq} to 370@code{eql} and @code{equal}. Distinct floating-point objects can be
364compare them, then you test whether two values are the same 371numerically equal. If you use @code{eq} to compare them, you test
365@emph{object}. By contrast, @code{=} compares only the numeric values 372whether they are the same @emph{object}; if you use @code{eql} or
366of the objects. 373@code{equal}, you test whether their values are
374@emph{indistinguishable}. In contrast, @code{=} uses numeric
375comparison, and sometimes returns @code{t} when a non-numeric
376comparison would return @code{nil} and vice versa. @xref{Float
377Basics}.
367 378
368 In Emacs Lisp, each integer is a unique Lisp object. 379 In Emacs Lisp, each integer is a unique Lisp object.
369Therefore, @code{eq} is equivalent to @code{=} where integers are 380Therefore, @code{eq} is equivalent to @code{=} where integers are