aboutsummaryrefslogtreecommitdiffstats
path: root/doc
diff options
context:
space:
mode:
authorPaul Eggert2014-03-17 21:03:59 -0700
committerPaul Eggert2014-03-17 21:03:59 -0700
commit1917cf46bba74cdd0bcd1d0545cbd688db4e76f9 (patch)
tree35cb5f9bc976ddd97283d32480e69f747a3bcc43 /doc
parent53e84c5f280e75f7f3a624b01d298f48ea3105aa (diff)
downloademacs-1917cf46bba74cdd0bcd1d0545cbd688db4e76f9.tar.gz
emacs-1917cf46bba74cdd0bcd1d0545cbd688db4e76f9.zip
Improve documentation for integer and floating-point basics.
* numbers.texi (Numbers, Integer Basics, Float Basics): Document the basics a bit more precisely. Say more clearly that Emacs floating-point numbers are IEEE doubles on all current platforms. Give more details about frexp. Say more clearly that '1.' is an integer. (Predicates on Numbers): Fix wholenump typo. * objects.texi (Integer Type): Adjust to match numbers.texi.
Diffstat (limited to 'doc')
-rw-r--r--doc/lispref/ChangeLog11
-rw-r--r--doc/lispref/numbers.texi158
-rw-r--r--doc/lispref/objects.texi14
3 files changed, 111 insertions, 72 deletions
diff --git a/doc/lispref/ChangeLog b/doc/lispref/ChangeLog
index 639d7e2dc9c..8bf25baeee4 100644
--- a/doc/lispref/ChangeLog
+++ b/doc/lispref/ChangeLog
@@ -1,3 +1,14 @@
12014-03-18 Paul Eggert <eggert@cs.ucla.edu>
2
3 Improve documentation for integer and floating-point basics.
4 * numbers.texi (Numbers, Integer Basics, Float Basics):
5 Document the basics a bit more precisely. Say more clearly
6 that Emacs floating-point numbers are IEEE doubles on all
7 current platforms. Give more details about frexp.
8 Say more clearly that '1.' is an integer.
9 (Predicates on Numbers): Fix wholenump typo.
10 * objects.texi (Integer Type): Adjust to match numbers.texi.
11
12014-03-18 Stefan Monnier <monnier@iro.umontreal.ca> 122014-03-18 Stefan Monnier <monnier@iro.umontreal.ca>
2 13
3 * functions.texi (Advising Functions): Try and improve the text. 14 * functions.texi (Advising Functions): Try and improve the text.
diff --git a/doc/lispref/numbers.texi b/doc/lispref/numbers.texi
index d202877e8ad..1758a44baab 100644
--- a/doc/lispref/numbers.texi
+++ b/doc/lispref/numbers.texi
@@ -9,13 +9,14 @@
9@cindex numbers 9@cindex numbers
10 10
11 GNU Emacs supports two numeric data types: @dfn{integers} and 11 GNU Emacs supports two numeric data types: @dfn{integers} and
12@dfn{floating point numbers}. Integers are whole numbers such as 12@dfn{floating-point numbers}. Integers are whole numbers such as
13@minus{}3, 0, 7, 13, and 511. Their values are exact. Floating-point 13@minus{}3, 0, 7, 13, and 511. Floating-point numbers are numbers with
14numbers are numbers with fractional parts, such as @minus{}4.5, 0.0, or 14fractional parts, such as @minus{}4.5, 0.0, and 2.71828. They can
152.71828. They can also be expressed in exponential notation: 1.5e2 15also be expressed in exponential notation: @samp{1.5e2} is the same as
16equals 150; in this example, @samp{e2} stands for ten to the second 16@samp{150.0}; here, @samp{e2} stands for ten to the second power, and
17power, and that is multiplied by 1.5. Floating point values are not 17that is multiplied by 1.5. Integer computations are exact, though
18exact; they have a fixed, limited amount of precision. 18they may overflow. Floating-point computations often involve rounding
19errors, as the numbers have a fixed amount of precision.
19 20
20@menu 21@menu
21* Integer Basics:: Representation and range of integers. 22* Integer Basics:: Representation and range of integers.
@@ -34,7 +35,7 @@ exact; they have a fixed, limited amount of precision.
34@section Integer Basics 35@section Integer Basics
35 36
36 The range of values for an integer depends on the machine. The 37 The range of values for an integer depends on the machine. The
37minimum range is @minus{}536870912 to 536870911 (30 bits; i.e., 38minimum range is @minus{}536,870,912 to 536,870,911 (30 bits; i.e.,
38@ifnottex 39@ifnottex
39@minus{}2**29 40@minus{}2**29
40@end ifnottex 41@end ifnottex
@@ -61,7 +62,8 @@ Emacs range is treated as a floating-point number.
61 1. ; @r{The integer 1.} 62 1. ; @r{The integer 1.}
62+1 ; @r{Also the integer 1.} 63+1 ; @r{Also the integer 1.}
63-1 ; @r{The integer @minus{}1.} 64-1 ; @r{The integer @minus{}1.}
64 1073741825 ; @r{The floating point number 1073741825.0.} 65 9000000000000000000
66 ; @r{The floating-point number 9e18.}
65 0 ; @r{The integer 0.} 67 0 ; @r{The integer 0.}
66-0 ; @r{The integer 0.} 68-0 ; @r{The integer 0.}
67@end example 69@end example
@@ -148,15 +150,43 @@ value is a marker, its position value is used and its buffer is ignored.
148@cindex largest Lisp integer 150@cindex largest Lisp integer
149@cindex maximum Lisp integer 151@cindex maximum Lisp integer
150@defvar most-positive-fixnum 152@defvar most-positive-fixnum
151The value of this variable is the largest integer that Emacs Lisp 153The value of this variable is the largest integer that Emacs Lisp can
152can handle. 154handle. Typical values are
155@ifnottex
1562**29 @minus{} 1
157@end ifnottex
158@tex
159@math{2^{29}-1}
160@end tex
161on 32-bit and
162@ifnottex
1632**61 @minus{} 1
164@end ifnottex
165@tex
166@math{2^{61}-1}
167@end tex
168on 64-bit platforms.
153@end defvar 169@end defvar
154 170
155@cindex smallest Lisp integer 171@cindex smallest Lisp integer
156@cindex minimum Lisp integer 172@cindex minimum Lisp integer
157@defvar most-negative-fixnum 173@defvar most-negative-fixnum
158The value of this variable is the smallest integer that Emacs Lisp can 174The value of this variable is the smallest integer that Emacs Lisp can
159handle. It is negative. 175handle. It is negative. Typical values are
176@ifnottex
177@minus{}2**29
178@end ifnottex
179@tex
180@math{-2^{29}}
181@end tex
182on 32-bit and
183@ifnottex
184@minus{}2**61
185@end ifnottex
186@tex
187@math{-2^{61}}
188@end tex
189on 64-bit platforms.
160@end defvar 190@end defvar
161 191
162 In Emacs Lisp, text characters are represented by integers. Any 192 In Emacs Lisp, text characters are represented by integers. Any
@@ -168,22 +198,25 @@ considered to be valid as a character. @xref{String Basics}.
168 198
169@cindex @acronym{IEEE} floating point 199@cindex @acronym{IEEE} floating point
170 Floating-point numbers are useful for representing numbers that are 200 Floating-point numbers are useful for representing numbers that are
171not integral. The precise range of floating-point numbers is 201not integral. The range of floating-point numbers is
172machine-specific; it is the same as the range of the C data type 202the same as the range of the C data type @code{double} on the machine
173@code{double} on the machine you are using. Emacs uses the 203you are using. On all computers currently supported by Emacs, this is
174@acronym{IEEE} floating-point standard, which is supported by all 204double-precision @acronym{IEEE} floating point.
175modern computers.
176 205
177 The read syntax for floating-point numbers requires either a decimal 206 The read syntax for floating-point numbers requires either a decimal
178point (with at least one digit following), an exponent, or both. For 207point, an exponent, or both. Optional signs (@samp{+} or @samp{-})
179example, @samp{1500.0}, @samp{15e2}, @samp{15.0e2}, @samp{1.5e3}, and 208precede the number and its exponent. For example, @samp{1500.0},
180@samp{.15e4} are five ways of writing a floating-point number whose 209@samp{+15e2}, @samp{15.0e+2}, @samp{+1500000e-3}, and @samp{.15e4} are
181value is 1500. They are all equivalent. You can also use a minus 210five ways of writing a floating-point number whose value is 1500.
182sign to write negative floating-point numbers, as in @samp{-1.0}. 211They are all equivalent. Like Common Lisp, Emacs Lisp requires at
183 212least one digit after any decimal point in a floating-point number;
184 Emacs Lisp treats @code{-0.0} as numerically equal to ordinary zero (with 213@samp{1500.} is an integer, not a floating-point number.
185respect to @code{equal} and @code{=}), even though the two are 214
186distinguishable in the @acronym{IEEE} floating-point standard. 215 Emacs Lisp treats @code{-0.0} as numerically equal to ordinary zero
216with respect to @code{equal} and @code{=}. This follows the
217@acronym{IEEE} floating-point standard, which says @code{-0.0} and
218@code{0.0} are numerically equal even though other operations can
219distinguish them.
187 220
188@cindex positive infinity 221@cindex positive infinity
189@cindex negative infinity 222@cindex negative infinity
@@ -193,58 +226,53 @@ distinguishable in the @acronym{IEEE} floating-point standard.
193infinity and negative infinity as floating-point values. It also 226infinity and negative infinity as floating-point values. It also
194provides for a class of values called NaN or ``not-a-number''; 227provides for a class of values called NaN or ``not-a-number'';
195numerical functions return such values in cases where there is no 228numerical functions return such values in cases where there is no
196correct answer. For example, @code{(/ 0.0 0.0)} returns a NaN@. (NaN 229correct answer. For example, @code{(/ 0.0 0.0)} returns a NaN@.
197values can also carry a sign, but for practical purposes there's no 230Although NaN values carry a sign, for practical purposes there is no other
198significant difference between different NaN values in Emacs Lisp.) 231significant difference between different NaN values in Emacs Lisp.
199
200When a function is documented to return a NaN, it returns an
201implementation-defined value when Emacs is running on one of the
202now-rare platforms that do not use @acronym{IEEE} floating point. For
203example, @code{(log -1.0)} typically returns a NaN, but on
204non-@acronym{IEEE} platforms it returns an implementation-defined
205value.
206 232
207Here are the read syntaxes for these special floating-point values: 233Here are read syntaxes for these special floating-point values:
208 234
209@table @asis 235@table @asis
210@item positive infinity 236@item infinity
211@samp{1.0e+INF} 237@samp{1.0e+INF} and @samp{-1.0e+INF}
212@item negative infinity 238@item not-a-number
213@samp{-1.0e+INF} 239@samp{0.0e+NaN} and @samp{-0.0e+NaN}
214@item Not-a-number
215@samp{0.0e+NaN} or @samp{-0.0e+NaN}.
216@end table 240@end table
217 241
218@defun isnan number 242 The following functions are specialized for handling floating-point
219This predicate tests whether its argument is NaN, and returns @code{t}
220if so, @code{nil} otherwise. The argument must be a number.
221@end defun
222
223 The following functions are specialized for handling floating point
224numbers: 243numbers:
225 244
226@defun frexp x 245@defun isnan x
227This function returns a cons cell @code{(@var{sig} . @var{exp})}, 246This predicate returns @code{t} if its floating-point argument is a NaN,
228where @var{sig} and @var{exp} are respectively the significand and 247@code{nil} otherwise.
229exponent of the floating point number @var{x}: 248@end defun
230 249
231@smallexample 250@defun frexp x
232@var{x} = @var{sig} * 2^@var{exp} 251This function returns a cons cell @code{(@var{s} . @var{e})},
233@end smallexample 252where @var{s} and @var{e} are respectively the significand and
253exponent of the floating-point number @var{x}.
234 254
235@var{sig} is a floating point number between 0.5 (inclusive) and 1.0 255If @var{x} is finite, @var{s} is a floating-point number between 0.5
236(exclusive). If @var{x} is zero, the return value is @code{(0 . 0)}. 256(inclusive) and 1.0 (exclusive), @var{e} is an integer, and
257@ifnottex
258@var{x} = @var{s} * 2**@var{e}.
259@end ifnottex
260@tex
261@math{x = s 2^e}.
262@end tex
263If @var{x} is zero or infinity, @var{s} is the same as @var{x}.
264If @var{x} is a NaN, @var{s} is also a NaN.
265If @var{x} is zero, @var{e} is 0.
237@end defun 266@end defun
238 267
239@defun ldexp sig &optional exp 268@defun ldexp sig &optional exp
240This function returns a floating point number corresponding to the 269This function returns a floating-point number corresponding to the
241significand @var{sig} and exponent @var{exp}. 270significand @var{sig} and exponent @var{exp}.
242@end defun 271@end defun
243 272
244@defun copysign x1 x2 273@defun copysign x1 x2
245This function copies the sign of @var{x2} to the value of @var{x1}, 274This function copies the sign of @var{x2} to the value of @var{x1},
246and returns the result. @var{x1} and @var{x2} must be floating point 275and returns the result. @var{x1} and @var{x2} must be floating point.
247numbers.
248@end defun 276@end defun
249 277
250@defun logb number 278@defun logb number
@@ -293,8 +321,8 @@ tests to see whether its argument is a nonnegative integer, and
293returns @code{t} if so, @code{nil} otherwise. 0 is considered 321returns @code{t} if so, @code{nil} otherwise. 0 is considered
294non-negative. 322non-negative.
295 323
296@findex wholenump number 324@findex wholenump
297This is a synonym for @code{natnump}. 325@code{wholenump} is a synonym for @code{natnump}.
298@end defun 326@end defun
299 327
300@defun zerop number 328@defun zerop number
@@ -532,8 +560,8 @@ Except for @code{%}, each of these functions accepts both integer and
532floating-point arguments, and returns a floating-point number if any 560floating-point arguments, and returns a floating-point number if any
533argument is floating point. 561argument is floating point.
534 562
535 It is important to note that in Emacs Lisp, arithmetic functions 563 Emacs Lisp arithmetic functions do not check for integer overflow.
536do not check for overflow. Thus @code{(1+ 536870911)} may evaluate to 564Thus @code{(1+ 536870911)} may evaluate to
537@minus{}536870912, depending on your hardware. 565@minus{}536870912, depending on your hardware.
538 566
539@defun 1+ number-or-marker 567@defun 1+ number-or-marker
diff --git a/doc/lispref/objects.texi b/doc/lispref/objects.texi
index 086abecded1..4e8182ccf34 100644
--- a/doc/lispref/objects.texi
+++ b/doc/lispref/objects.texi
@@ -161,8 +161,8 @@ latter are unique to Emacs Lisp.
161@node Integer Type 161@node Integer Type
162@subsection Integer Type 162@subsection Integer Type
163 163
164 The range of values for integers in Emacs Lisp is @minus{}536870912 to 164 The range of values for an integer depends on the machine. The
165536870911 (30 bits; i.e., 165minimum range is @minus{}536,870,912 to 536,870,911 (30 bits; i.e.,
166@ifnottex 166@ifnottex
167@minus{}2**29 167@minus{}2**29
168@end ifnottex 168@end ifnottex
@@ -176,9 +176,9 @@ to
176@tex 176@tex
177@math{2^{29}-1}) 177@math{2^{29}-1})
178@end tex 178@end tex
179on typical 32-bit machines. (Some machines provide a wider range.) 179but many machines provide a wider range.
180Emacs Lisp arithmetic functions do not check for overflow. Thus 180Emacs Lisp arithmetic functions do not check for integer overflow. Thus
181@code{(1+ 536870911)} is @minus{}536870912 if Emacs integers are 30 bits. 181@code{(1+ 536870911)} is @minus{}536,870,912 if Emacs integers are 30 bits.
182 182
183 The read syntax for integers is a sequence of (base ten) digits with an 183 The read syntax for integers is a sequence of (base ten) digits with an
184optional sign at the beginning and an optional period at the end. The 184optional sign at the beginning and an optional period at the end. The
@@ -215,8 +215,8 @@ this records a power of 2 rather than a power of 10.
215 215
216 The printed representation for floating-point numbers requires either 216 The printed representation for floating-point numbers requires either
217a decimal point (with at least one digit following), an exponent, or 217a decimal point (with at least one digit following), an exponent, or
218both. For example, @samp{1500.0}, @samp{15e2}, @samp{15.0e2}, 218both. For example, @samp{1500.0}, @samp{+15e2}, @samp{15.0e+2},
219@samp{1.5e3}, and @samp{.15e4} are five ways of writing a floating-point 219@samp{+1500000e-3}, and @samp{.15e4} are five ways of writing a floating-point
220number whose value is 1500. They are all equivalent. 220number whose value is 1500. They are all equivalent.
221 221
222 @xref{Numbers}, for more information. 222 @xref{Numbers}, for more information.