diff options
Diffstat (limited to 'doc/lispref/numbers.texi')
| -rw-r--r-- | doc/lispref/numbers.texi | 124 |
1 files changed, 65 insertions, 59 deletions
diff --git a/doc/lispref/numbers.texi b/doc/lispref/numbers.texi index 6768ecece9c..77db0f86c26 100644 --- a/doc/lispref/numbers.texi +++ b/doc/lispref/numbers.texi | |||
| @@ -168,34 +168,37 @@ character codepoint. | |||
| 168 | @node Float Basics | 168 | @node Float Basics |
| 169 | @section Floating Point Basics | 169 | @section Floating Point Basics |
| 170 | 170 | ||
| 171 | @cindex @acronym{IEEE} floating point | ||
| 171 | Floating point numbers are useful for representing numbers that are | 172 | Floating point numbers are useful for representing numbers that are |
| 172 | not integral. The precise range of floating point numbers is | 173 | not integral. The precise range of floating point numbers is |
| 173 | machine-specific; it is the same as the range of the C data type | 174 | machine-specific; it is the same as the range of the C data type |
| 174 | @code{double} on the machine you are using. | 175 | @code{double} on the machine you are using. Emacs uses the |
| 176 | @acronym{IEEE} floating point standard where possible (the standard is | ||
| 177 | supported by most modern computers). | ||
| 175 | 178 | ||
| 176 | The read-syntax for floating point numbers requires either a decimal | 179 | The read syntax for floating point numbers requires either a decimal |
| 177 | point (with at least one digit following), an exponent, or both. For | 180 | point (with at least one digit following), an exponent, or both. For |
| 178 | example, @samp{1500.0}, @samp{15e2}, @samp{15.0e2}, @samp{1.5e3}, and | 181 | example, @samp{1500.0}, @samp{15e2}, @samp{15.0e2}, @samp{1.5e3}, and |
| 179 | @samp{.15e4} are five ways of writing a floating point number whose | 182 | @samp{.15e4} are five ways of writing a floating point number whose |
| 180 | value is 1500. They are all equivalent. You can also use a minus sign | 183 | value is 1500. They are all equivalent. You can also use a minus |
| 181 | to write negative floating point numbers, as in @samp{-1.0}. | 184 | sign to write negative floating point numbers, as in @samp{-1.0}. |
| 185 | |||
| 186 | Emacs Lisp treats @code{-0.0} as equal to ordinary zero (with | ||
| 187 | respect to @code{equal} and @code{=}), even though the two are | ||
| 188 | distinguishable in the @acronym{IEEE} floating point standard. | ||
| 182 | 189 | ||
| 183 | @cindex @acronym{IEEE} floating point | ||
| 184 | @cindex positive infinity | 190 | @cindex positive infinity |
| 185 | @cindex negative infinity | 191 | @cindex negative infinity |
| 186 | @cindex infinity | 192 | @cindex infinity |
| 187 | @cindex NaN | 193 | @cindex NaN |
| 188 | Most modern computers support the @acronym{IEEE} floating point standard, | 194 | The @acronym{IEEE} floating point standard supports positive |
| 189 | which provides for positive infinity and negative infinity as floating point | 195 | infinity and negative infinity as floating point values. It also |
| 190 | values. It also provides for a class of values called NaN or | 196 | provides for a class of values called NaN or ``not-a-number''; |
| 191 | ``not-a-number''; numerical functions return such values in cases where | 197 | numerical functions return such values in cases where there is no |
| 192 | there is no correct answer. For example, @code{(/ 0.0 0.0)} returns a | 198 | correct answer. For example, @code{(/ 0.0 0.0)} returns a NaN. (NaN |
| 193 | NaN. For practical purposes, there's no significant difference between | 199 | values can also carry a sign, but for practical purposes there's no |
| 194 | different NaN values in Emacs Lisp, and there's no rule for precisely | 200 | significant difference between different NaN values in Emacs Lisp.) |
| 195 | which NaN value should be used in a particular case, so Emacs Lisp | 201 | Here are the read syntaxes for these special floating point values: |
| 196 | doesn't try to distinguish them (but it does report the sign, if you | ||
| 197 | print it). Here are the read syntaxes for these special floating | ||
| 198 | point values: | ||
| 199 | 202 | ||
| 200 | @table @asis | 203 | @table @asis |
| 201 | @item positive infinity | 204 | @item positive infinity |
| @@ -206,16 +209,37 @@ point values: | |||
| 206 | @samp{0.0e+NaN} or @samp{-0.0e+NaN}. | 209 | @samp{0.0e+NaN} or @samp{-0.0e+NaN}. |
| 207 | @end table | 210 | @end table |
| 208 | 211 | ||
| 209 | To test whether a floating point value is a NaN, compare it with | 212 | @defun isnan number |
| 210 | itself using @code{=}. That returns @code{nil} for a NaN, and | 213 | This predicate tests whether its argument is NaN, and returns @code{t} |
| 211 | @code{t} for any other floating point value. | 214 | if so, @code{nil} otherwise. The argument must be a number. |
| 215 | @end defun | ||
| 216 | |||
| 217 | The following functions are specialized for handling floating point | ||
| 218 | numbers: | ||
| 219 | |||
| 220 | @defun frexp x | ||
| 221 | This function returns a cons cell @code{(@var{sig} . @var{exp})}, | ||
| 222 | where @var{sig} and @var{exp} are respectively the significand and | ||
| 223 | exponent of the floating point number @var{x}: | ||
| 224 | |||
| 225 | @smallexample | ||
| 226 | @var{x} = @var{sig} * 2^@var{exp} | ||
| 227 | @end smallexample | ||
| 228 | |||
| 229 | @var{sig} is a floating point number between 0.5 (inclusive) and 1.0 | ||
| 230 | (exclusive). If @var{x} is zero, the return value is @code{(0 . 0)}. | ||
| 231 | @end defun | ||
| 212 | 232 | ||
| 213 | The value @code{-0.0} is distinguishable from ordinary zero in | 233 | @defun ldexp sig &optional exp |
| 214 | @acronym{IEEE} floating point, but Emacs Lisp @code{equal} and | 234 | This function returns a floating point number corresponding to the |
| 215 | @code{=} consider them equal values. | 235 | significand @var{sig} and exponent @var{exp}. |
| 236 | @end defun | ||
| 216 | 237 | ||
| 217 | You can use @code{logb} to extract the binary exponent of a floating | 238 | @defun copysign x1 x2 |
| 218 | point number (or estimate the logarithm of an integer): | 239 | This function copies the sign of @var{x2} to the value of @var{x1}, |
| 240 | and returns the result. @var{x1} and @var{x2} must be floating point | ||
| 241 | numbers. | ||
| 242 | @end defun | ||
| 219 | 243 | ||
| 220 | @defun logb number | 244 | @defun logb number |
| 221 | This function returns the binary exponent of @var{number}. More | 245 | This function returns the binary exponent of @var{number}. More |
| @@ -230,14 +254,6 @@ down to an integer. | |||
| 230 | @end example | 254 | @end example |
| 231 | @end defun | 255 | @end defun |
| 232 | 256 | ||
| 233 | @defvar float-e | ||
| 234 | The mathematical constant @math{e} (2.71828@dots{}). | ||
| 235 | @end defvar | ||
| 236 | |||
| 237 | @defvar float-pi | ||
| 238 | The mathematical constant @math{pi} (3.14159@dots{}). | ||
| 239 | @end defvar | ||
| 240 | |||
| 241 | @node Predicates on Numbers | 257 | @node Predicates on Numbers |
| 242 | @section Type Predicates for Numbers | 258 | @section Type Predicates for Numbers |
| 243 | @cindex predicates for numbers | 259 | @cindex predicates for numbers |
| @@ -1122,35 +1138,15 @@ angle in radians between the vector @code{[@var{x}, @var{y}]} and the | |||
| 1122 | @end defun | 1138 | @end defun |
| 1123 | 1139 | ||
| 1124 | @defun exp arg | 1140 | @defun exp arg |
| 1125 | This is the exponential function; it returns | 1141 | This is the exponential function; it returns @math{e} to the power |
| 1126 | @tex | 1142 | @var{arg}. |
| 1127 | @math{e} | ||
| 1128 | @end tex | ||
| 1129 | @ifnottex | ||
| 1130 | @i{e} | ||
| 1131 | @end ifnottex | ||
| 1132 | to the power @var{arg}. | ||
| 1133 | @tex | ||
| 1134 | @math{e} | ||
| 1135 | @end tex | ||
| 1136 | @ifnottex | ||
| 1137 | @i{e} | ||
| 1138 | @end ifnottex | ||
| 1139 | is a fundamental mathematical constant also called the base of natural | ||
| 1140 | logarithms. | ||
| 1141 | @end defun | 1143 | @end defun |
| 1142 | 1144 | ||
| 1143 | @defun log arg &optional base | 1145 | @defun log arg &optional base |
| 1144 | This function returns the logarithm of @var{arg}, with base @var{base}. | 1146 | This function returns the logarithm of @var{arg}, with base |
| 1145 | If you don't specify @var{base}, the base | 1147 | @var{base}. If you don't specify @var{base}, the natural base |
| 1146 | @tex | 1148 | @math{e} is used. If @var{arg} is negative, it signals a |
| 1147 | @math{e} | 1149 | @code{domain-error} error. |
| 1148 | @end tex | ||
| 1149 | @ifnottex | ||
| 1150 | @i{e} | ||
| 1151 | @end ifnottex | ||
| 1152 | is used. If @var{arg} is negative, it signals a @code{domain-error} | ||
| 1153 | error. | ||
| 1154 | @end defun | 1150 | @end defun |
| 1155 | 1151 | ||
| 1156 | @ignore | 1152 | @ignore |
| @@ -1185,6 +1181,17 @@ This returns the square root of @var{arg}. If @var{arg} is negative, | |||
| 1185 | it signals a @code{domain-error} error. | 1181 | it signals a @code{domain-error} error. |
| 1186 | @end defun | 1182 | @end defun |
| 1187 | 1183 | ||
| 1184 | In addition, Emacs defines the following common mathematical | ||
| 1185 | constants: | ||
| 1186 | |||
| 1187 | @defvar float-e | ||
| 1188 | The mathematical constant @math{e} (2.71828@dots{}). | ||
| 1189 | @end defvar | ||
| 1190 | |||
| 1191 | @defvar float-pi | ||
| 1192 | The mathematical constant @math{pi} (3.14159@dots{}). | ||
| 1193 | @end defvar | ||
| 1194 | |||
| 1188 | @node Random Numbers | 1195 | @node Random Numbers |
| 1189 | @section Random Numbers | 1196 | @section Random Numbers |
| 1190 | @cindex random numbers | 1197 | @cindex random numbers |
| @@ -1218,7 +1225,6 @@ nonnegative and less than @var{limit}. | |||
| 1218 | 1225 | ||
| 1219 | If @var{limit} is @code{t}, it means to choose a new seed based on the | 1226 | If @var{limit} is @code{t}, it means to choose a new seed based on the |
| 1220 | current time of day and on Emacs's process @acronym{ID} number. | 1227 | current time of day and on Emacs's process @acronym{ID} number. |
| 1221 | @c "Emacs'" is incorrect usage! | ||
| 1222 | 1228 | ||
| 1223 | On some machines, any integer representable in Lisp may be the result | 1229 | On some machines, any integer representable in Lisp may be the result |
| 1224 | of @code{random}. On other machines, the result can never be larger | 1230 | of @code{random}. On other machines, the result can never be larger |