aboutsummaryrefslogtreecommitdiffstats
path: root/doc/lispref/numbers.texi
diff options
context:
space:
mode:
Diffstat (limited to 'doc/lispref/numbers.texi')
-rw-r--r--doc/lispref/numbers.texi124
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
172not integral. The precise range of floating point numbers is 173not integral. The precise range of floating point numbers is
173machine-specific; it is the same as the range of the C data type 174machine-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
177supported 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
177point (with at least one digit following), an exponent, or both. For 180point (with at least one digit following), an exponent, or both. For
178example, @samp{1500.0}, @samp{15e2}, @samp{15.0e2}, @samp{1.5e3}, and 181example, @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
180value is 1500. They are all equivalent. You can also use a minus sign 183value is 1500. They are all equivalent. You can also use a minus
181to write negative floating point numbers, as in @samp{-1.0}. 184sign 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
187respect to @code{equal} and @code{=}), even though the two are
188distinguishable 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
189which provides for positive infinity and negative infinity as floating point 195infinity and negative infinity as floating point values. It also
190values. It also provides for a class of values called NaN or 196provides for a class of values called NaN or ``not-a-number'';
191``not-a-number''; numerical functions return such values in cases where 197numerical functions return such values in cases where there is no
192there is no correct answer. For example, @code{(/ 0.0 0.0)} returns a 198correct answer. For example, @code{(/ 0.0 0.0)} returns a NaN. (NaN
193NaN. For practical purposes, there's no significant difference between 199values can also carry a sign, but for practical purposes there's no
194different NaN values in Emacs Lisp, and there's no rule for precisely 200significant difference between different NaN values in Emacs Lisp.)
195which NaN value should be used in a particular case, so Emacs Lisp 201Here are the read syntaxes for these special floating point values:
196doesn't try to distinguish them (but it does report the sign, if you
197print it). Here are the read syntaxes for these special floating
198point 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
210itself using @code{=}. That returns @code{nil} for a NaN, and 213This predicate tests whether its argument is NaN, and returns @code{t}
211@code{t} for any other floating point value. 214if so, @code{nil} otherwise. The argument must be a number.
215@end defun
216
217 The following functions are specialized for handling floating point
218numbers:
219
220@defun frexp x
221This function returns a cons cell @code{(@var{sig} . @var{exp})},
222where @var{sig} and @var{exp} are respectively the significand and
223exponent 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 234This function returns a floating point number corresponding to the
215@code{=} consider them equal values. 235significand @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
218point number (or estimate the logarithm of an integer): 239This function copies the sign of @var{x2} to the value of @var{x1},
240and returns the result. @var{x1} and @var{x2} must be floating point
241numbers.
242@end defun
219 243
220@defun logb number 244@defun logb number
221This function returns the binary exponent of @var{number}. More 245This 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
234The mathematical constant @math{e} (2.71828@dots{}).
235@end defvar
236
237@defvar float-pi
238The 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
1125This is the exponential function; it returns 1141This 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
1132to the power @var{arg}.
1133@tex
1134@math{e}
1135@end tex
1136@ifnottex
1137@i{e}
1138@end ifnottex
1139is a fundamental mathematical constant also called the base of natural
1140logarithms.
1141@end defun 1143@end defun
1142 1144
1143@defun log arg &optional base 1145@defun log arg &optional base
1144This function returns the logarithm of @var{arg}, with base @var{base}. 1146This function returns the logarithm of @var{arg}, with base
1145If 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
1152is used. If @var{arg} is negative, it signals a @code{domain-error}
1153error.
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,
1185it signals a @code{domain-error} error. 1181it signals a @code{domain-error} error.
1186@end defun 1182@end defun
1187 1183
1184In addition, Emacs defines the following common mathematical
1185constants:
1186
1187@defvar float-e
1188The mathematical constant @math{e} (2.71828@dots{}).
1189@end defvar
1190
1191@defvar float-pi
1192The 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
1219If @var{limit} is @code{t}, it means to choose a new seed based on the 1226If @var{limit} is @code{t}, it means to choose a new seed based on the
1220current time of day and on Emacs's process @acronym{ID} number. 1227current time of day and on Emacs's process @acronym{ID} number.
1221@c "Emacs'" is incorrect usage!
1222 1228
1223On some machines, any integer representable in Lisp may be the result 1229On some machines, any integer representable in Lisp may be the result
1224of @code{random}. On other machines, the result can never be larger 1230of @code{random}. On other machines, the result can never be larger