diff options
| author | Paul Eggert | 2019-11-10 15:01:06 -0800 |
|---|---|---|
| committer | Paul Eggert | 2019-11-10 15:04:20 -0800 |
| commit | b6942c0c37a504e00c717c8c74bfa9dcd208c931 (patch) | |
| tree | 40565eb305e75021b6d96e90fd678c87a9de10be | |
| parent | 6ad5eb97940b07bf8d28f8517608351b3af1221c (diff) | |
| download | emacs-b6942c0c37a504e00c717c8c74bfa9dcd208c931.tar.gz emacs-b6942c0c37a504e00c717c8c74bfa9dcd208c931.zip | |
Document Lisp floats a bit better
* doc/lispref/numbers.texi (Float Basics):
* doc/misc/cl.texi (Implementation Parameters):
* lisp/emacs-lisp/cl-lib.el (cl-most-positive-float)
(cl-least-positive-float)
(cl-least-positive-normalized-float, cl-float-epsilon)
(cl-float-negative-epsilon):
Document IEEE floating point better. Don’t suggest that Emacs
might use some floating-point format other than IEEE format, as
Emacs currently assumes IEEE in several places and there seems
little point in removing those assumptions.
| -rw-r--r-- | doc/lispref/numbers.texi | 8 | ||||
| -rw-r--r-- | doc/misc/cl.texi | 54 | ||||
| -rw-r--r-- | lisp/emacs-lisp/cl-lib.el | 16 |
3 files changed, 37 insertions, 41 deletions
diff --git a/doc/lispref/numbers.texi b/doc/lispref/numbers.texi index 0c71387a8a7..939ad5c85a1 100644 --- a/doc/lispref/numbers.texi +++ b/doc/lispref/numbers.texi | |||
| @@ -218,8 +218,12 @@ considered to be valid as a character. @xref{Character Codes}. | |||
| 218 | Floating-point numbers are useful for representing numbers that are | 218 | Floating-point numbers are useful for representing numbers that are |
| 219 | not integral. The range of floating-point numbers is | 219 | not integral. The range of floating-point numbers is |
| 220 | the same as the range of the C data type @code{double} on the machine | 220 | the same as the range of the C data type @code{double} on the machine |
| 221 | you are using. On all computers currently supported by Emacs, this is | 221 | you are using. On all computers supported by Emacs, this is |
| 222 | double-precision @acronym{IEEE} floating point. | 222 | @acronym{IEEE} binary64 floating point format, which is standardized by |
| 223 | @url{https://standards.ieee.org/standard/754-2019.html,,IEEE Std 754-2019} | ||
| 224 | and is discussed further in David Goldberg's paper | ||
| 225 | ``@url{https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html, | ||
| 226 | What Every Computer Scientist Should Know About Floating-Point Arithmetic}''. | ||
| 223 | 227 | ||
| 224 | The read syntax for floating-point numbers requires either a decimal | 228 | The read syntax for floating-point numbers requires either a decimal |
| 225 | point, an exponent, or both. Optional signs (@samp{+} or @samp{-}) | 229 | point, an exponent, or both. Optional signs (@samp{+} or @samp{-}) |
diff --git a/doc/misc/cl.texi b/doc/misc/cl.texi index 246f86bfd1a..cfdbc7c41b2 100644 --- a/doc/misc/cl.texi +++ b/doc/misc/cl.texi | |||
| @@ -3113,48 +3113,42 @@ function that must be called before the parameters can be used. | |||
| 3113 | @defun cl-float-limits | 3113 | @defun cl-float-limits |
| 3114 | This function makes sure that the Common Lisp floating-point parameters | 3114 | This function makes sure that the Common Lisp floating-point parameters |
| 3115 | like @code{cl-most-positive-float} have been initialized. Until it is | 3115 | like @code{cl-most-positive-float} have been initialized. Until it is |
| 3116 | called, these parameters will be @code{nil}. | 3116 | called, these parameters have unspecified values. |
| 3117 | @c If this version of Emacs does not support floats, the parameters will | ||
| 3118 | @c remain @code{nil}. | ||
| 3119 | If the parameters have already been initialized, the function returns | 3117 | If the parameters have already been initialized, the function returns |
| 3120 | immediately. | 3118 | immediately. |
| 3121 | |||
| 3122 | The algorithm makes assumptions that will be valid for almost all | ||
| 3123 | machines, but will fail if the machine's arithmetic is extremely | ||
| 3124 | unusual, e.g., decimal. | ||
| 3125 | @end defun | 3119 | @end defun |
| 3126 | 3120 | ||
| 3127 | Since true Common Lisp supports up to four different kinds of floating-point | 3121 | Since true Common Lisp supports up to four different kinds of floating-point |
| 3128 | numbers, it has families of constants like | 3122 | numbers, it has families of constants like |
| 3129 | @code{most-positive-single-float}, @code{most-positive-double-float}, | 3123 | @code{most-positive-single-float}, @code{most-positive-double-float}, |
| 3130 | @code{most-positive-long-float}, and so on. Emacs has only one | 3124 | @code{most-positive-long-float}, and so on. This package uses just |
| 3131 | kind of floating-point number, so this package just uses single constants. | 3125 | one set of constants because Emacs has only one kind of |
| 3126 | floating-point number, namely the IEEE binary64 floating-point format. | ||
| 3127 | @xref{Float Basics,,,elisp,GNU Emacs Lisp Reference Manual}. | ||
| 3132 | 3128 | ||
| 3133 | @defvar cl-most-positive-float | 3129 | @defvar cl-most-positive-float |
| 3134 | This constant equals the largest value a Lisp float can hold. | 3130 | This constant equals the largest finite value a Lisp float can hold. |
| 3135 | For those systems whose arithmetic supports infinities, this is | 3131 | For IEEE binary64 format, this equals @code{(- (expt 2 1024) (- 2 |
| 3136 | the largest @emph{finite} value. For IEEE machines, the value | 3132 | 971))}, which equals @code{1.7976931348623157e+308}. |
| 3137 | is approximately @code{1.79e+308}. | ||
| 3138 | @end defvar | 3133 | @end defvar |
| 3139 | 3134 | ||
| 3140 | @defvar cl-most-negative-float | 3135 | @defvar cl-most-negative-float |
| 3141 | This constant equals the most negative value a Lisp float can hold. | 3136 | This constant equals the most negative finite value a Lisp float can hold. |
| 3142 | (It is assumed to be equal to @code{(- cl-most-positive-float)}.) | 3137 | For IEEE binary64 format, this equals @code{(- cl-most-positive-float)}. |
| 3143 | @end defvar | 3138 | @end defvar |
| 3144 | 3139 | ||
| 3145 | @defvar cl-least-positive-float | 3140 | @defvar cl-least-positive-normalized-float |
| 3146 | This constant equals the smallest Lisp float value greater than zero. | 3141 | This constant equals the smallest positive Lisp float that is |
| 3147 | For IEEE machines, it is about @code{4.94e-324} if denormals are | 3142 | @dfn{normalized}, i.e., that has full precision. |
| 3148 | supported or @code{2.22e-308} if not. | 3143 | For IEEE binary64 format, this equals @code{(expt 2 -1022)}, |
| 3144 | which equals @code{2.2250738585072014e-308}. | ||
| 3149 | @end defvar | 3145 | @end defvar |
| 3150 | 3146 | ||
| 3151 | @defvar cl-least-positive-normalized-float | 3147 | @defvar cl-least-positive-float |
| 3152 | This constant equals the smallest @emph{normalized} Lisp float greater | 3148 | This constant equals the smallest Lisp float value greater than zero. |
| 3153 | than zero, i.e., the smallest value for which IEEE denormalization | 3149 | For IEEE binary64 format, this equals @code{5e-324} (which equals |
| 3154 | will not result in a loss of precision. For IEEE machines, this | 3150 | @code{(expt 2 -1074)}) if subnormal numbers are supported, and |
| 3155 | value is about @code{2.22e-308}. For machines that do not support | 3151 | @code{cl-least-positive-normalized-float} otherwise. |
| 3156 | the concept of denormalization and gradual underflow, this constant | ||
| 3157 | will always equal @code{cl-least-positive-float}. | ||
| 3158 | @end defvar | 3152 | @end defvar |
| 3159 | 3153 | ||
| 3160 | @defvar cl-least-negative-float | 3154 | @defvar cl-least-negative-float |
| @@ -3169,14 +3163,14 @@ This constant is the negative counterpart of | |||
| 3169 | @defvar cl-float-epsilon | 3163 | @defvar cl-float-epsilon |
| 3170 | This constant is the smallest positive Lisp float that can be added | 3164 | This constant is the smallest positive Lisp float that can be added |
| 3171 | to 1.0 to produce a distinct value. Adding a smaller number to 1.0 | 3165 | to 1.0 to produce a distinct value. Adding a smaller number to 1.0 |
| 3172 | will yield 1.0 again due to roundoff. For IEEE machines, epsilon | 3166 | will yield 1.0 again due to roundoff. For IEEE binary64 format, this |
| 3173 | is about @code{2.22e-16}. | 3167 | equals @code{(expt 2 -52)}, which equals @code{2.220446049250313e-16}. |
| 3174 | @end defvar | 3168 | @end defvar |
| 3175 | 3169 | ||
| 3176 | @defvar cl-float-negative-epsilon | 3170 | @defvar cl-float-negative-epsilon |
| 3177 | This is the smallest positive value that can be subtracted from | 3171 | This is the smallest positive value that can be subtracted from |
| 3178 | 1.0 to produce a distinct value. For IEEE machines, it is about | 3172 | 1.0 to produce a distinct value. For IEEE binary64 format, this |
| 3179 | @code{1.11e-16}. | 3173 | equals @code{(expt 2 -53)}, which equals @code{1.1102230246251565e-16}. |
| 3180 | @end defvar | 3174 | @end defvar |
| 3181 | 3175 | ||
| 3182 | @node Sequences | 3176 | @node Sequences |
diff --git a/lisp/emacs-lisp/cl-lib.el b/lisp/emacs-lisp/cl-lib.el index ff096918173..7d0df27e14b 100644 --- a/lisp/emacs-lisp/cl-lib.el +++ b/lisp/emacs-lisp/cl-lib.el | |||
| @@ -299,7 +299,7 @@ If true return the decimal value of digit CHAR in RADIX." | |||
| 299 | (defconst cl-most-positive-float nil | 299 | (defconst cl-most-positive-float nil |
| 300 | "The largest value that a Lisp float can hold. | 300 | "The largest value that a Lisp float can hold. |
| 301 | If your system supports infinities, this is the largest finite value. | 301 | If your system supports infinities, this is the largest finite value. |
| 302 | For IEEE machines, this is approximately 1.79e+308. | 302 | For Emacs, this equals 1.7976931348623157e+308. |
| 303 | Call `cl-float-limits' to set this.") | 303 | Call `cl-float-limits' to set this.") |
| 304 | 304 | ||
| 305 | (defconst cl-most-negative-float nil | 305 | (defconst cl-most-negative-float nil |
| @@ -309,8 +309,8 @@ Call `cl-float-limits' to set this.") | |||
| 309 | 309 | ||
| 310 | (defconst cl-least-positive-float nil | 310 | (defconst cl-least-positive-float nil |
| 311 | "The smallest value greater than zero that a Lisp float can hold. | 311 | "The smallest value greater than zero that a Lisp float can hold. |
| 312 | For IEEE machines, it is about 4.94e-324 if denormals are supported, | 312 | For Emacs, this equals 5e-324 if subnormal numbers are supported, |
| 313 | or 2.22e-308 if they are not. | 313 | `cl-least-positive-normalized-float' if they are not. |
| 314 | Call `cl-float-limits' to set this.") | 314 | Call `cl-float-limits' to set this.") |
| 315 | 315 | ||
| 316 | (defconst cl-least-negative-float nil | 316 | (defconst cl-least-negative-float nil |
| @@ -320,10 +320,8 @@ Call `cl-float-limits' to set this.") | |||
| 320 | 320 | ||
| 321 | (defconst cl-least-positive-normalized-float nil | 321 | (defconst cl-least-positive-normalized-float nil |
| 322 | "The smallest normalized Lisp float greater than zero. | 322 | "The smallest normalized Lisp float greater than zero. |
| 323 | This is the smallest value for which IEEE denormalization does not lose | 323 | This is the smallest value that has full precision. |
| 324 | precision. For IEEE machines, this value is about 2.22e-308. | 324 | For Emacs, this equals 2.2250738585072014e-308. |
| 325 | For machines that do not support the concept of denormalization | ||
| 326 | and gradual underflow, this constant equals `cl-least-positive-float'. | ||
| 327 | Call `cl-float-limits' to set this.") | 325 | Call `cl-float-limits' to set this.") |
| 328 | 326 | ||
| 329 | (defconst cl-least-negative-normalized-float nil | 327 | (defconst cl-least-negative-normalized-float nil |
| @@ -334,12 +332,12 @@ Call `cl-float-limits' to set this.") | |||
| 334 | (defconst cl-float-epsilon nil | 332 | (defconst cl-float-epsilon nil |
| 335 | "The smallest positive float that adds to 1.0 to give a distinct value. | 333 | "The smallest positive float that adds to 1.0 to give a distinct value. |
| 336 | Adding a number less than this to 1.0 returns 1.0 due to roundoff. | 334 | Adding a number less than this to 1.0 returns 1.0 due to roundoff. |
| 337 | For IEEE machines, epsilon is about 2.22e-16. | 335 | For Emacs, this equals 2.220446049250313e-16. |
| 338 | Call `cl-float-limits' to set this.") | 336 | Call `cl-float-limits' to set this.") |
| 339 | 337 | ||
| 340 | (defconst cl-float-negative-epsilon nil | 338 | (defconst cl-float-negative-epsilon nil |
| 341 | "The smallest positive float that subtracts from 1.0 to give a distinct value. | 339 | "The smallest positive float that subtracts from 1.0 to give a distinct value. |
| 342 | For IEEE machines, it is about 1.11e-16. | 340 | For Emacs, this equals 1.1102230246251565e-16. |
| 343 | Call `cl-float-limits' to set this.") | 341 | Call `cl-float-limits' to set this.") |
| 344 | 342 | ||
| 345 | 343 | ||