diff options
| author | Paul Eggert | 2018-07-19 13:29:28 -0700 |
|---|---|---|
| committer | Paul Eggert | 2018-07-19 13:30:43 -0700 |
| commit | 96d77f9eb882b68e994e187ed9c2156a23e3279d (patch) | |
| tree | 51866456027141aaa234c5180c1d74b985fe2fdb /doc | |
| parent | 3a91c5e4a2684b04b86356a32b7ec57145ceba95 (diff) | |
| download | emacs-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.texi | 31 |
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 |
| 226 | with respect to @code{equal} and @code{=}. This follows the | 226 | with 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 |
| 229 | distinguish them. | 229 | distinguish 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 |
| 238 | infinity and negative infinity as floating-point values. It also | 236 | infinity and negative infinity as floating-point values. It also |
| 239 | provides for a class of values called NaN, or ``not a number''; | 237 | provides for a class of values called NaN, or ``not a number''; |
| 240 | numerical functions return such values in cases where there is no | 238 | numerical functions return such values in cases where there is no |
| 241 | correct answer. For example, @code{(/ 0.0 0.0)} returns a NaN@. | 239 | correct answer. For example, @code{(/ 0.0 0.0)} returns a NaN@. |
| 242 | A NaN is never numerically equal to any value, not even to itself. | 240 | A NaN is never numerically equal to any value, not even to itself. |
| 243 | NaNs carry a sign and a significand, and non-numeric functions like | 241 | NaNs 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 | 242 | two NaNs as equal when their |
| 245 | signs and significands agree. Significands of NaNs are | 243 | signs and significands agree. Significands of NaNs are |
| 246 | machine-dependent and are not directly visible to Emacs Lisp. | 244 | machine-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 | ||
| 249 | whether 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}; | ||
| 252 | conversely, @code{(equal 0.0 -0.0)} returns @code{nil} whereas | ||
| 253 | @code{(= 0.0 -0.0)} returns @code{t}. | ||
| 254 | |||
| 248 | Here are read syntaxes for these special floating-point values: | 255 | Here 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}, |
| 363 | objects with the same numeric value. If you use @code{eq} to | 370 | @code{eql} and @code{equal}. Distinct floating-point objects can be |
| 364 | compare them, then you test whether two values are the same | 371 | numerically equal. If you use @code{eq} to compare them, you test |
| 365 | @emph{object}. By contrast, @code{=} compares only the numeric values | 372 | whether they are the same @emph{object}; if you use @code{eql} or |
| 366 | of the objects. | 373 | @code{equal}, you test whether their values are |
| 374 | @emph{indistinguishable}. In contrast, @code{=} uses numeric | ||
| 375 | comparison, and sometimes returns @code{t} when a non-numeric | ||
| 376 | comparison would return @code{nil} and vice versa. @xref{Float | ||
| 377 | Basics}. | ||
| 367 | 378 | ||
| 368 | In Emacs Lisp, each integer is a unique Lisp object. | 379 | In Emacs Lisp, each integer is a unique Lisp object. |
| 369 | Therefore, @code{eq} is equivalent to @code{=} where integers are | 380 | Therefore, @code{eq} is equivalent to @code{=} where integers are |