Mercurial > emacs
annotate lispref/numbers.texi @ 21456:c0496e62b737
(min, max): Define as macros.
author | Karl Heuer <kwzh@gnu.org> |
---|---|
date | Thu, 09 Apr 1998 18:12:46 +0000 |
parents | 66d807bdc5b4 |
children | 90da2489c498 |
rev | line source |
---|---|
6510 | 1 @c -*-texinfo-*- |
2 @c This is part of the GNU Emacs Lisp Reference Manual. | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998 Free Software Foundation, Inc. |
6510 | 4 @c See the file elisp.texi for copying conditions. |
5 @setfilename ../info/numbers | |
7115
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
6 @node Numbers, Strings and Characters, Lisp Data Types, Top |
6510 | 7 @chapter Numbers |
8 @cindex integers | |
9 @cindex numbers | |
10 | |
11 GNU Emacs supports two numeric data types: @dfn{integers} and | |
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 | |
14 numbers are numbers with fractional parts, such as @minus{}4.5, 0.0, or | |
7115
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
15 2.71828. They can also be expressed in exponential notation: |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
16 1.5e2 equals 150; in this example, @samp{e2} stands for ten to the |
6510 | 17 second power, and is multiplied by 1.5. Floating point values are not |
18 exact; they have a fixed, limited amount of precision. | |
19 | |
20 @menu | |
21 * Integer Basics:: Representation and range of integers. | |
22 * Float Basics:: Representation and range of floating point. | |
23 * Predicates on Numbers:: Testing for numbers. | |
24 * Comparison of Numbers:: Equality and inequality predicates. | |
25 * Numeric Conversions:: Converting float to integer and vice versa. | |
26 * Arithmetic Operations:: How to add, subtract, multiply and divide. | |
27 * Rounding Operations:: Explicitly rounding floating point numbers. | |
28 * Bitwise Operations:: Logical and, or, not, shifting. | |
11230
c6b70cdf844e
Don't call the special math functions "transcendental".
Richard M. Stallman <rms@gnu.org>
parents:
10558
diff
changeset
|
29 * Math Functions:: Trig, exponential and logarithmic functions. |
6510 | 30 * Random Numbers:: Obtaining random integers, predictable or not. |
31 @end menu | |
32 | |
33 @node Integer Basics | |
34 @comment node-name, next, previous, up | |
35 @section Integer Basics | |
36 | |
37 The range of values for an integer depends on the machine. The | |
10558
fbfd717ff79b
Fix integer width changes.
Richard M. Stallman <rms@gnu.org>
parents:
10306
diff
changeset
|
38 minimum range is @minus{}134217728 to 134217727 (28 bits; i.e., |
6510 | 39 @ifinfo |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
40 -2**27 |
6510 | 41 @end ifinfo |
42 @tex | |
10558
fbfd717ff79b
Fix integer width changes.
Richard M. Stallman <rms@gnu.org>
parents:
10306
diff
changeset
|
43 $-2^{27}$ |
6510 | 44 @end tex |
45 to | |
46 @ifinfo | |
10558
fbfd717ff79b
Fix integer width changes.
Richard M. Stallman <rms@gnu.org>
parents:
10306
diff
changeset
|
47 2**27 - 1), |
6510 | 48 @end ifinfo |
49 @tex | |
10558
fbfd717ff79b
Fix integer width changes.
Richard M. Stallman <rms@gnu.org>
parents:
10306
diff
changeset
|
50 $2^{27}-1$), |
6510 | 51 @end tex |
10558
fbfd717ff79b
Fix integer width changes.
Richard M. Stallman <rms@gnu.org>
parents:
10306
diff
changeset
|
52 but some machines may provide a wider range. Many examples in this |
fbfd717ff79b
Fix integer width changes.
Richard M. Stallman <rms@gnu.org>
parents:
10306
diff
changeset
|
53 chapter assume an integer has 28 bits. |
6510 | 54 @cindex overflow |
55 | |
56 The Lisp reader reads an integer as a sequence of digits with optional | |
57 initial sign and optional final period. | |
58 | |
59 @example | |
60 1 ; @r{The integer 1.} | |
61 1. ; @r{The integer 1.} | |
62 +1 ; @r{Also the integer 1.} | |
63 -1 ; @r{The integer @minus{}1.} | |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
64 268435457 ; @r{Also the integer 1, due to overflow.} |
6510 | 65 0 ; @r{The integer 0.} |
66 -0 ; @r{The integer 0.} | |
67 @end example | |
68 | |
69 To understand how various functions work on integers, especially the | |
70 bitwise operators (@pxref{Bitwise Operations}), it is often helpful to | |
71 view the numbers in their binary form. | |
72 | |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
73 In 28-bit binary, the decimal integer 5 looks like this: |
6510 | 74 |
75 @example | |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
76 0000 0000 0000 0000 0000 0000 0101 |
6510 | 77 @end example |
78 | |
79 @noindent | |
80 (We have inserted spaces between groups of 4 bits, and two spaces | |
81 between groups of 8 bits, to make the binary integer easier to read.) | |
82 | |
83 The integer @minus{}1 looks like this: | |
84 | |
85 @example | |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
86 1111 1111 1111 1111 1111 1111 1111 |
6510 | 87 @end example |
88 | |
89 @noindent | |
90 @cindex two's complement | |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
91 @minus{}1 is represented as 28 ones. (This is called @dfn{two's |
6510 | 92 complement} notation.) |
93 | |
94 The negative integer, @minus{}5, is creating by subtracting 4 from | |
95 @minus{}1. In binary, the decimal integer 4 is 100. Consequently, | |
96 @minus{}5 looks like this: | |
97 | |
98 @example | |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
99 1111 1111 1111 1111 1111 1111 1011 |
6510 | 100 @end example |
101 | |
16736
981e116b4ac6
Minor cleanups for overfull hboxes.
Richard M. Stallman <rms@gnu.org>
parents:
12282
diff
changeset
|
102 In this implementation, the largest 28-bit binary integer value is |
981e116b4ac6
Minor cleanups for overfull hboxes.
Richard M. Stallman <rms@gnu.org>
parents:
12282
diff
changeset
|
103 134,217,727 in decimal. In binary, it looks like this: |
6510 | 104 |
105 @example | |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
106 0111 1111 1111 1111 1111 1111 1111 |
6510 | 107 @end example |
108 | |
109 Since the arithmetic functions do not check whether integers go | |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
110 outside their range, when you add 1 to 134,217,727, the value is the |
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
111 negative integer @minus{}134,217,728: |
6510 | 112 |
113 @example | |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
114 (+ 1 134217727) |
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
115 @result{} -134217728 |
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
116 @result{} 1000 0000 0000 0000 0000 0000 0000 |
6510 | 117 @end example |
118 | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
119 Many of the functions described in this chapter accept markers for |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
120 arguments in place of numbers. (@xref{Markers}.) Since the actual |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
121 arguments to such functions may be either numbers or markers, we often |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
122 give these arguments the name @var{number-or-marker}. When the argument |
6510 | 123 value is a marker, its position value is used and its buffer is ignored. |
124 | |
125 @node Float Basics | |
126 @section Floating Point Basics | |
127 | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
128 Floating point numbers are useful for representing numbers that are |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
129 not integral. The precise range of floating point numbers is |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
130 machine-specific; it is the same as the range of the C data type |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
131 @code{double} on the machine you are using. |
6510 | 132 |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
133 The read-syntax for floating point numbers requires either a decimal |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
134 point (with at least one digit following), an exponent, or both. For |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
135 example, @samp{1500.0}, @samp{15e2}, @samp{15.0e2}, @samp{1.5e3}, and |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
136 @samp{.15e4} are five ways of writing a floating point number whose |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
137 value is 1500. They are all equivalent. You can also use a minus sign |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
138 to write negative floating point numbers, as in @samp{-1.0}. |
6510 | 139 |
140 @cindex IEEE floating point | |
141 @cindex positive infinity | |
142 @cindex negative infinity | |
143 @cindex infinity | |
144 @cindex NaN | |
145 Most modern computers support the IEEE floating point standard, which | |
146 provides for positive infinity and negative infinity as floating point | |
7115
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
147 values. It also provides for a class of values called NaN or |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
148 ``not-a-number''; numerical functions return such values in cases where |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
149 there is no correct answer. For example, @code{(sqrt -1.0)} returns a |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
150 NaN. For practical purposes, there's no significant difference between |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
151 different NaN values in Emacs Lisp, and there's no rule for precisely |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
152 which NaN value should be used in a particular case, so this manual |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
153 doesn't try to distinguish them. Here are the read syntaxes for |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
154 these numbers: |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
155 |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
156 @table @asis |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
157 @item positive infinity |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
158 @samp{1.0e+INF} |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
159 @item negative infinity |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
160 @samp{-1.0e+INF} |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
161 @item Not-a-number |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
162 @samp{0.0e+NaN}. |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
163 @end table |
6510 | 164 |
165 You can use @code{logb} to extract the binary exponent of a floating | |
166 point number (or estimate the logarithm of an integer): | |
167 | |
168 @defun logb number | |
169 This function returns the binary exponent of @var{number}. More | |
170 precisely, the value is the logarithm of @var{number} base 2, rounded | |
171 down to an integer. | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
172 |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
173 @example |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
174 (logb 10) |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
175 @result{} 3 |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
176 (logb 10.0e20) |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
177 @result{} 69 |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
178 @end example |
6510 | 179 @end defun |
180 | |
181 @node Predicates on Numbers | |
182 @section Type Predicates for Numbers | |
183 | |
184 The functions in this section test whether the argument is a number or | |
185 whether it is a certain sort of number. The functions @code{integerp} | |
186 and @code{floatp} can take any type of Lisp object as argument (the | |
187 predicates would not be of much use otherwise); but the @code{zerop} | |
188 predicate requires a number as its argument. See also | |
189 @code{integer-or-marker-p} and @code{number-or-marker-p}, in | |
190 @ref{Predicates on Markers}. | |
191 | |
192 @defun floatp object | |
193 This predicate tests whether its argument is a floating point | |
194 number and returns @code{t} if so, @code{nil} otherwise. | |
195 | |
196 @code{floatp} does not exist in Emacs versions 18 and earlier. | |
197 @end defun | |
198 | |
199 @defun integerp object | |
200 This predicate tests whether its argument is an integer, and returns | |
201 @code{t} if so, @code{nil} otherwise. | |
202 @end defun | |
203 | |
204 @defun numberp object | |
205 This predicate tests whether its argument is a number (either integer or | |
206 floating point), and returns @code{t} if so, @code{nil} otherwise. | |
207 @end defun | |
208 | |
7115
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
209 @defun wholenump object |
6510 | 210 @cindex natural numbers |
7115
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
211 The @code{wholenump} predicate (whose name comes from the phrase |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
212 ``whole-number-p'') tests to see whether its argument is a nonnegative |
6510 | 213 integer, and returns @code{t} if so, @code{nil} otherwise. 0 is |
214 considered non-negative. | |
215 | |
7115
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
216 @findex natnump |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
217 @code{natnump} is an obsolete synonym for @code{wholenump}. |
6510 | 218 @end defun |
219 | |
220 @defun zerop number | |
221 This predicate tests whether its argument is zero, and returns @code{t} | |
222 if so, @code{nil} otherwise. The argument must be a number. | |
223 | |
224 These two forms are equivalent: @code{(zerop x)} @equiv{} @code{(= x 0)}. | |
225 @end defun | |
226 | |
227 @node Comparison of Numbers | |
228 @section Comparison of Numbers | |
229 @cindex number equality | |
230 | |
7115
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
231 To test numbers for numerical equality, you should normally use |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
232 @code{=}, not @code{eq}. There can be many distinct floating point |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
233 number objects with the same numeric value. If you use @code{eq} to |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
234 compare them, then you test whether two values are the same |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
235 @emph{object}. By contrast, @code{=} compares only the numeric values |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
236 of the objects. |
6510 | 237 |
7115
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
238 At present, each integer value has a unique Lisp object in Emacs Lisp. |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
239 Therefore, @code{eq} is equivalent to @code{=} where integers are |
7115
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
240 concerned. It is sometimes convenient to use @code{eq} for comparing an |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
241 unknown value with an integer, because @code{eq} does not report an |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
242 error if the unknown value is not a number---it accepts arguments of any |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
243 type. By contrast, @code{=} signals an error if the arguments are not |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
244 numbers or markers. However, it is a good idea to use @code{=} if you |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
245 can, even for comparing integers, just in case we change the |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
246 representation of integers in a future Emacs version. |
6510 | 247 |
248 There is another wrinkle: because floating point arithmetic is not | |
249 exact, it is often a bad idea to check for equality of two floating | |
250 point values. Usually it is better to test for approximate equality. | |
251 Here's a function to do this: | |
252 | |
253 @example | |
254 (defvar fuzz-factor 1.0e-6) | |
255 (defun approx-equal (x y) | |
12098 | 256 (or (and (= x 0) (= y 0)) |
257 (< (/ (abs (- x y)) | |
258 (max (abs x) (abs y))) | |
259 fuzz-factor))) | |
6510 | 260 @end example |
261 | |
262 @cindex CL note---integers vrs @code{eq} | |
263 @quotation | |
7115
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
264 @b{Common Lisp note:} Comparing numbers in Common Lisp always requires |
6510 | 265 @code{=} because Common Lisp implements multi-word integers, and two |
266 distinct integer objects can have the same numeric value. Emacs Lisp | |
267 can have just one integer object for any given value because it has a | |
268 limited range of integer values. | |
269 @end quotation | |
270 | |
271 @defun = number-or-marker1 number-or-marker2 | |
272 This function tests whether its arguments are numerically equal, and | |
273 returns @code{t} if so, @code{nil} otherwise. | |
274 @end defun | |
275 | |
276 @defun /= number-or-marker1 number-or-marker2 | |
277 This function tests whether its arguments are numerically equal, and | |
278 returns @code{t} if they are not, and @code{nil} if they are. | |
279 @end defun | |
280 | |
281 @defun < number-or-marker1 number-or-marker2 | |
282 This function tests whether its first argument is strictly less than | |
283 its second argument. It returns @code{t} if so, @code{nil} otherwise. | |
284 @end defun | |
285 | |
286 @defun <= number-or-marker1 number-or-marker2 | |
287 This function tests whether its first argument is less than or equal | |
288 to its second argument. It returns @code{t} if so, @code{nil} | |
289 otherwise. | |
290 @end defun | |
291 | |
292 @defun > number-or-marker1 number-or-marker2 | |
293 This function tests whether its first argument is strictly greater | |
294 than its second argument. It returns @code{t} if so, @code{nil} | |
295 otherwise. | |
296 @end defun | |
297 | |
298 @defun >= number-or-marker1 number-or-marker2 | |
299 This function tests whether its first argument is greater than or | |
300 equal to its second argument. It returns @code{t} if so, @code{nil} | |
301 otherwise. | |
302 @end defun | |
303 | |
304 @defun max number-or-marker &rest numbers-or-markers | |
305 This function returns the largest of its arguments. | |
306 | |
307 @example | |
308 (max 20) | |
309 @result{} 20 | |
310 (max 1 2.5) | |
311 @result{} 2.5 | |
312 (max 1 3 2.5) | |
313 @result{} 3 | |
314 @end example | |
315 @end defun | |
316 | |
317 @defun min number-or-marker &rest numbers-or-markers | |
318 This function returns the smallest of its arguments. | |
319 | |
320 @example | |
321 (min -4 1) | |
322 @result{} -4 | |
323 @end example | |
324 @end defun | |
325 | |
326 @node Numeric Conversions | |
327 @section Numeric Conversions | |
328 @cindex rounding in conversions | |
329 | |
330 To convert an integer to floating point, use the function @code{float}. | |
331 | |
332 @defun float number | |
333 This returns @var{number} converted to floating point. | |
334 If @var{number} is already a floating point number, @code{float} returns | |
335 it unchanged. | |
336 @end defun | |
337 | |
338 There are four functions to convert floating point numbers to integers; | |
339 they differ in how they round. These functions accept integer arguments | |
340 also, and return such arguments unchanged. | |
341 | |
342 @defun truncate number | |
343 This returns @var{number}, converted to an integer by rounding towards | |
344 zero. | |
345 @end defun | |
346 | |
347 @defun floor number &optional divisor | |
348 This returns @var{number}, converted to an integer by rounding downward | |
349 (towards negative infinity). | |
350 | |
351 If @var{divisor} is specified, @var{number} is divided by @var{divisor} | |
352 before the floor is taken; this is the division operation that | |
353 corresponds to @code{mod}. An @code{arith-error} results if | |
354 @var{divisor} is 0. | |
355 @end defun | |
356 | |
357 @defun ceiling number | |
358 This returns @var{number}, converted to an integer by rounding upward | |
359 (towards positive infinity). | |
360 @end defun | |
361 | |
362 @defun round number | |
363 This returns @var{number}, converted to an integer by rounding towards the | |
12098 | 364 nearest integer. Rounding a value equidistant between two integers |
365 may choose the integer closer to zero, or it may prefer an even integer, | |
366 depending on your machine. | |
6510 | 367 @end defun |
368 | |
369 @node Arithmetic Operations | |
370 @section Arithmetic Operations | |
371 | |
372 Emacs Lisp provides the traditional four arithmetic operations: | |
373 addition, subtraction, multiplication, and division. Remainder and modulus | |
374 functions supplement the division functions. The functions to | |
375 add or subtract 1 are provided because they are traditional in Lisp and | |
376 commonly used. | |
377 | |
378 All of these functions except @code{%} return a floating point value | |
379 if any argument is floating. | |
380 | |
381 It is important to note that in GNU Emacs Lisp, arithmetic functions | |
12067 | 382 do not check for overflow. Thus @code{(1+ 134217727)} may evaluate to |
383 @minus{}134217728, depending on your hardware. | |
6510 | 384 |
385 @defun 1+ number-or-marker | |
386 This function returns @var{number-or-marker} plus 1. | |
387 For example, | |
388 | |
389 @example | |
390 (setq foo 4) | |
391 @result{} 4 | |
392 (1+ foo) | |
393 @result{} 5 | |
394 @end example | |
395 | |
12098 | 396 This function is not analogous to the C operator @code{++}---it does not |
397 increment a variable. It just computes a sum. Thus, if we continue, | |
6510 | 398 |
399 @example | |
400 foo | |
401 @result{} 4 | |
402 @end example | |
403 | |
404 If you want to increment the variable, you must use @code{setq}, | |
405 like this: | |
406 | |
407 @example | |
408 (setq foo (1+ foo)) | |
409 @result{} 5 | |
410 @end example | |
411 @end defun | |
412 | |
413 @defun 1- number-or-marker | |
414 This function returns @var{number-or-marker} minus 1. | |
415 @end defun | |
416 | |
417 @defun abs number | |
418 This returns the absolute value of @var{number}. | |
419 @end defun | |
420 | |
421 @defun + &rest numbers-or-markers | |
422 This function adds its arguments together. When given no arguments, | |
12098 | 423 @code{+} returns 0. |
6510 | 424 |
425 @example | |
426 (+) | |
427 @result{} 0 | |
428 (+ 1) | |
429 @result{} 1 | |
430 (+ 1 2 3 4) | |
431 @result{} 10 | |
432 @end example | |
433 @end defun | |
434 | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
435 @defun - &optional number-or-marker &rest more-numbers-or-markers |
6510 | 436 The @code{-} function serves two purposes: negation and subtraction. |
437 When @code{-} has a single argument, the value is the negative of the | |
438 argument. When there are multiple arguments, @code{-} subtracts each of | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
16736
diff
changeset
|
439 the @var{more-numbers-or-markers} from @var{number-or-marker}, |
12098 | 440 cumulatively. If there are no arguments, the result is 0. |
6510 | 441 |
442 @example | |
443 (- 10 1 2 3 4) | |
444 @result{} 0 | |
445 (- 10) | |
446 @result{} -10 | |
447 (-) | |
448 @result{} 0 | |
449 @end example | |
450 @end defun | |
451 | |
452 @defun * &rest numbers-or-markers | |
453 This function multiplies its arguments together, and returns the | |
12098 | 454 product. When given no arguments, @code{*} returns 1. |
6510 | 455 |
456 @example | |
457 (*) | |
458 @result{} 1 | |
459 (* 1) | |
460 @result{} 1 | |
461 (* 1 2 3 4) | |
462 @result{} 24 | |
463 @end example | |
464 @end defun | |
465 | |
466 @defun / dividend divisor &rest divisors | |
7115
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
467 This function divides @var{dividend} by @var{divisor} and returns the |
6510 | 468 quotient. If there are additional arguments @var{divisors}, then it |
469 divides @var{dividend} by each divisor in turn. Each argument may be a | |
470 number or a marker. | |
471 | |
472 If all the arguments are integers, then the result is an integer too. | |
473 This means the result has to be rounded. On most machines, the result | |
474 is rounded towards zero after each division, but some machines may round | |
475 differently with negative arguments. This is because the Lisp function | |
476 @code{/} is implemented using the C division operator, which also | |
477 permits machine-dependent rounding. As a practical matter, all known | |
478 machines round in the standard fashion. | |
479 | |
480 @cindex @code{arith-error} in division | |
481 If you divide by 0, an @code{arith-error} error is signaled. | |
482 (@xref{Errors}.) | |
483 | |
484 @example | |
12282
586e3ea81792
updates for version 19.29 made by melissa; also needed to check out files
Melissa Weisshaus <melissa@gnu.org>
parents:
12128
diff
changeset
|
485 @group |
6510 | 486 (/ 6 2) |
487 @result{} 3 | |
12282
586e3ea81792
updates for version 19.29 made by melissa; also needed to check out files
Melissa Weisshaus <melissa@gnu.org>
parents:
12128
diff
changeset
|
488 @end group |
6510 | 489 (/ 5 2) |
490 @result{} 2 | |
491 (/ 25 3 2) | |
492 @result{} 4 | |
493 (/ -17 6) | |
494 @result{} -2 | |
495 @end example | |
496 | |
497 The result of @code{(/ -17 6)} could in principle be -3 on some | |
498 machines. | |
499 @end defun | |
500 | |
501 @defun % dividend divisor | |
502 @cindex remainder | |
503 This function returns the integer remainder after division of @var{dividend} | |
504 by @var{divisor}. The arguments must be integers or markers. | |
505 | |
506 For negative arguments, the remainder is in principle machine-dependent | |
507 since the quotient is; but in practice, all known machines behave alike. | |
508 | |
509 An @code{arith-error} results if @var{divisor} is 0. | |
510 | |
511 @example | |
512 (% 9 4) | |
513 @result{} 1 | |
514 (% -9 4) | |
515 @result{} -1 | |
516 (% 9 -4) | |
517 @result{} 1 | |
518 (% -9 -4) | |
519 @result{} -1 | |
520 @end example | |
521 | |
522 For any two integers @var{dividend} and @var{divisor}, | |
523 | |
524 @example | |
525 @group | |
526 (+ (% @var{dividend} @var{divisor}) | |
527 (* (/ @var{dividend} @var{divisor}) @var{divisor})) | |
528 @end group | |
529 @end example | |
530 | |
531 @noindent | |
532 always equals @var{dividend}. | |
533 @end defun | |
534 | |
535 @defun mod dividend divisor | |
536 @cindex modulus | |
537 This function returns the value of @var{dividend} modulo @var{divisor}; | |
538 in other words, the remainder after division of @var{dividend} | |
539 by @var{divisor}, but with the same sign as @var{divisor}. | |
540 The arguments must be numbers or markers. | |
541 | |
542 Unlike @code{%}, @code{mod} returns a well-defined result for negative | |
543 arguments. It also permits floating point arguments; it rounds the | |
544 quotient downward (towards minus infinity) to an integer, and uses that | |
545 quotient to compute the remainder. | |
546 | |
547 An @code{arith-error} results if @var{divisor} is 0. | |
548 | |
549 @example | |
12282
586e3ea81792
updates for version 19.29 made by melissa; also needed to check out files
Melissa Weisshaus <melissa@gnu.org>
parents:
12128
diff
changeset
|
550 @group |
6510 | 551 (mod 9 4) |
552 @result{} 1 | |
12282
586e3ea81792
updates for version 19.29 made by melissa; also needed to check out files
Melissa Weisshaus <melissa@gnu.org>
parents:
12128
diff
changeset
|
553 @end group |
586e3ea81792
updates for version 19.29 made by melissa; also needed to check out files
Melissa Weisshaus <melissa@gnu.org>
parents:
12128
diff
changeset
|
554 @group |
6510 | 555 (mod -9 4) |
556 @result{} 3 | |
12282
586e3ea81792
updates for version 19.29 made by melissa; also needed to check out files
Melissa Weisshaus <melissa@gnu.org>
parents:
12128
diff
changeset
|
557 @end group |
586e3ea81792
updates for version 19.29 made by melissa; also needed to check out files
Melissa Weisshaus <melissa@gnu.org>
parents:
12128
diff
changeset
|
558 @group |
6510 | 559 (mod 9 -4) |
560 @result{} -3 | |
12282
586e3ea81792
updates for version 19.29 made by melissa; also needed to check out files
Melissa Weisshaus <melissa@gnu.org>
parents:
12128
diff
changeset
|
561 @end group |
586e3ea81792
updates for version 19.29 made by melissa; also needed to check out files
Melissa Weisshaus <melissa@gnu.org>
parents:
12128
diff
changeset
|
562 @group |
6510 | 563 (mod -9 -4) |
564 @result{} -1 | |
12282
586e3ea81792
updates for version 19.29 made by melissa; also needed to check out files
Melissa Weisshaus <melissa@gnu.org>
parents:
12128
diff
changeset
|
565 @end group |
586e3ea81792
updates for version 19.29 made by melissa; also needed to check out files
Melissa Weisshaus <melissa@gnu.org>
parents:
12128
diff
changeset
|
566 @group |
6510 | 567 (mod 5.5 2.5) |
568 @result{} .5 | |
12282
586e3ea81792
updates for version 19.29 made by melissa; also needed to check out files
Melissa Weisshaus <melissa@gnu.org>
parents:
12128
diff
changeset
|
569 @end group |
6510 | 570 @end example |
571 | |
572 For any two numbers @var{dividend} and @var{divisor}, | |
573 | |
574 @example | |
575 @group | |
576 (+ (mod @var{dividend} @var{divisor}) | |
577 (* (floor @var{dividend} @var{divisor}) @var{divisor})) | |
578 @end group | |
579 @end example | |
580 | |
581 @noindent | |
12098 | 582 always equals @var{dividend}, subject to rounding error if either |
583 argument is floating point. For @code{floor}, see @ref{Numeric | |
584 Conversions}. | |
6510 | 585 @end defun |
586 | |
587 @node Rounding Operations | |
588 @section Rounding Operations | |
589 @cindex rounding without conversion | |
590 | |
8017 | 591 The functions @code{ffloor}, @code{fceiling}, @code{fround} and |
6510 | 592 @code{ftruncate} take a floating point argument and return a floating |
593 point result whose value is a nearby integer. @code{ffloor} returns the | |
8017 | 594 nearest integer below; @code{fceiling}, the nearest integer above; |
7115
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
595 @code{ftruncate}, the nearest integer in the direction towards zero; |
6510 | 596 @code{fround}, the nearest integer. |
597 | |
598 @defun ffloor float | |
599 This function rounds @var{float} to the next lower integral value, and | |
600 returns that value as a floating point number. | |
601 @end defun | |
602 | |
8017 | 603 @defun fceiling float |
6510 | 604 This function rounds @var{float} to the next higher integral value, and |
605 returns that value as a floating point number. | |
606 @end defun | |
607 | |
7115
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
608 @defun ftruncate float |
6510 | 609 This function rounds @var{float} towards zero to an integral value, and |
610 returns that value as a floating point number. | |
611 @end defun | |
612 | |
613 @defun fround float | |
614 This function rounds @var{float} to the nearest integral value, | |
615 and returns that value as a floating point number. | |
616 @end defun | |
617 | |
618 @node Bitwise Operations | |
619 @section Bitwise Operations on Integers | |
620 | |
621 In a computer, an integer is represented as a binary number, a | |
622 sequence of @dfn{bits} (digits which are either zero or one). A bitwise | |
623 operation acts on the individual bits of such a sequence. For example, | |
624 @dfn{shifting} moves the whole sequence left or right one or more places, | |
625 reproducing the same pattern ``moved over''. | |
626 | |
627 The bitwise operations in Emacs Lisp apply only to integers. | |
628 | |
629 @defun lsh integer1 count | |
630 @cindex logical shift | |
631 @code{lsh}, which is an abbreviation for @dfn{logical shift}, shifts the | |
7115
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
632 bits in @var{integer1} to the left @var{count} places, or to the right |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
633 if @var{count} is negative, bringing zeros into the vacated bits. If |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
634 @var{count} is negative, @code{lsh} shifts zeros into the leftmost |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
635 (most-significant) bit, producing a positive result even if |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
636 @var{integer1} is negative. Contrast this with @code{ash}, below. |
6510 | 637 |
7115
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
638 Here are two examples of @code{lsh}, shifting a pattern of bits one |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
639 place to the left. We show only the low-order eight bits of the binary |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
640 pattern; the rest are all zero. |
6510 | 641 |
642 @example | |
643 @group | |
644 (lsh 5 1) | |
645 @result{} 10 | |
646 ;; @r{Decimal 5 becomes decimal 10.} | |
647 00000101 @result{} 00001010 | |
648 | |
649 (lsh 7 1) | |
650 @result{} 14 | |
651 ;; @r{Decimal 7 becomes decimal 14.} | |
652 00000111 @result{} 00001110 | |
653 @end group | |
654 @end example | |
655 | |
656 @noindent | |
657 As the examples illustrate, shifting the pattern of bits one place to | |
658 the left produces a number that is twice the value of the previous | |
659 number. | |
660 | |
12098 | 661 Shifting a pattern of bits two places to the left produces results |
662 like this (with 8-bit binary numbers): | |
663 | |
664 @example | |
665 @group | |
666 (lsh 3 2) | |
667 @result{} 12 | |
668 ;; @r{Decimal 3 becomes decimal 12.} | |
669 00000011 @result{} 00001100 | |
670 @end group | |
671 @end example | |
672 | |
673 On the other hand, shifting one place to the right looks like this: | |
674 | |
675 @example | |
676 @group | |
677 (lsh 6 -1) | |
678 @result{} 3 | |
679 ;; @r{Decimal 6 becomes decimal 3.} | |
680 00000110 @result{} 00000011 | |
681 @end group | |
682 | |
683 @group | |
684 (lsh 5 -1) | |
685 @result{} 2 | |
686 ;; @r{Decimal 5 becomes decimal 2.} | |
687 00000101 @result{} 00000010 | |
688 @end group | |
689 @end example | |
690 | |
691 @noindent | |
692 As the example illustrates, shifting one place to the right divides the | |
693 value of a positive integer by two, rounding downward. | |
694 | |
7115
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
695 The function @code{lsh}, like all Emacs Lisp arithmetic functions, does |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
696 not check for overflow, so shifting left can discard significant bits |
12067 | 697 and change the sign of the number. For example, left shifting |
698 134,217,727 produces @minus{}2 on a 28-bit machine: | |
6510 | 699 |
700 @example | |
12067 | 701 (lsh 134217727 1) ; @r{left shift} |
6510 | 702 @result{} -2 |
703 @end example | |
704 | |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
705 In binary, in the 28-bit implementation, the argument looks like this: |
6510 | 706 |
707 @example | |
708 @group | |
12128
27144f55d1c6
fixed errors that appeared during update to 19.29.
Melissa Weisshaus <melissa@gnu.org>
parents:
12098
diff
changeset
|
709 ;; @r{Decimal 134,217,727} |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
710 0111 1111 1111 1111 1111 1111 1111 |
6510 | 711 @end group |
712 @end example | |
713 | |
714 @noindent | |
715 which becomes the following when left shifted: | |
716 | |
717 @example | |
718 @group | |
719 ;; @r{Decimal @minus{}2} | |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
720 1111 1111 1111 1111 1111 1111 1110 |
6510 | 721 @end group |
722 @end example | |
723 @end defun | |
724 | |
725 @defun ash integer1 count | |
726 @cindex arithmetic shift | |
727 @code{ash} (@dfn{arithmetic shift}) shifts the bits in @var{integer1} | |
728 to the left @var{count} places, or to the right if @var{count} | |
729 is negative. | |
730 | |
731 @code{ash} gives the same results as @code{lsh} except when | |
732 @var{integer1} and @var{count} are both negative. In that case, | |
12098 | 733 @code{ash} puts ones in the empty bit positions on the left, while |
734 @code{lsh} puts zeros in those bit positions. | |
6510 | 735 |
736 Thus, with @code{ash}, shifting the pattern of bits one place to the right | |
737 looks like this: | |
738 | |
739 @example | |
740 @group | |
741 (ash -6 -1) @result{} -3 | |
742 ;; @r{Decimal @minus{}6 becomes decimal @minus{}3.} | |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
743 1111 1111 1111 1111 1111 1111 1010 |
6510 | 744 @result{} |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
745 1111 1111 1111 1111 1111 1111 1101 |
6510 | 746 @end group |
747 @end example | |
748 | |
749 In contrast, shifting the pattern of bits one place to the right with | |
750 @code{lsh} looks like this: | |
751 | |
752 @example | |
753 @group | |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
754 (lsh -6 -1) @result{} 134217725 |
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
755 ;; @r{Decimal @minus{}6 becomes decimal 134,217,725.} |
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
756 1111 1111 1111 1111 1111 1111 1010 |
6510 | 757 @result{} |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
758 0111 1111 1111 1111 1111 1111 1101 |
6510 | 759 @end group |
760 @end example | |
761 | |
762 Here are other examples: | |
763 | |
764 @c !!! Check if lined up in smallbook format! XDVI shows problem | |
765 @c with smallbook but not with regular book! --rjc 16mar92 | |
766 @smallexample | |
767 @group | |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
768 ; @r{ 28-bit binary values} |
6510 | 769 |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
770 (lsh 5 2) ; 5 = @r{0000 0000 0000 0000 0000 0000 0101} |
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
771 @result{} 20 ; = @r{0000 0000 0000 0000 0000 0001 0100} |
6510 | 772 @end group |
773 @group | |
774 (ash 5 2) | |
775 @result{} 20 | |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
776 (lsh -5 2) ; -5 = @r{1111 1111 1111 1111 1111 1111 1011} |
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
777 @result{} -20 ; = @r{1111 1111 1111 1111 1111 1110 1100} |
6510 | 778 (ash -5 2) |
779 @result{} -20 | |
780 @end group | |
781 @group | |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
782 (lsh 5 -2) ; 5 = @r{0000 0000 0000 0000 0000 0000 0101} |
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
783 @result{} 1 ; = @r{0000 0000 0000 0000 0000 0000 0001} |
6510 | 784 @end group |
785 @group | |
786 (ash 5 -2) | |
787 @result{} 1 | |
788 @end group | |
789 @group | |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
790 (lsh -5 -2) ; -5 = @r{1111 1111 1111 1111 1111 1111 1011} |
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
791 @result{} 4194302 ; = @r{0011 1111 1111 1111 1111 1111 1110} |
6510 | 792 @end group |
793 @group | |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
794 (ash -5 -2) ; -5 = @r{1111 1111 1111 1111 1111 1111 1011} |
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
795 @result{} -2 ; = @r{1111 1111 1111 1111 1111 1111 1110} |
6510 | 796 @end group |
797 @end smallexample | |
798 @end defun | |
799 | |
800 @defun logand &rest ints-or-markers | |
801 @cindex logical and | |
802 @cindex bitwise and | |
803 This function returns the ``logical and'' of the arguments: the | |
804 @var{n}th bit is set in the result if, and only if, the @var{n}th bit is | |
805 set in all the arguments. (``Set'' means that the value of the bit is 1 | |
806 rather than 0.) | |
807 | |
808 For example, using 4-bit binary numbers, the ``logical and'' of 13 and | |
809 12 is 12: 1101 combined with 1100 produces 1100. | |
810 In both the binary numbers, the leftmost two bits are set (i.e., they | |
811 are 1's), so the leftmost two bits of the returned value are set. | |
812 However, for the rightmost two bits, each is zero in at least one of | |
813 the arguments, so the rightmost two bits of the returned value are 0's. | |
814 | |
815 @noindent | |
816 Therefore, | |
817 | |
818 @example | |
819 @group | |
820 (logand 13 12) | |
821 @result{} 12 | |
822 @end group | |
823 @end example | |
824 | |
825 If @code{logand} is not passed any argument, it returns a value of | |
826 @minus{}1. This number is an identity element for @code{logand} | |
827 because its binary representation consists entirely of ones. If | |
828 @code{logand} is passed just one argument, it returns that argument. | |
829 | |
830 @smallexample | |
831 @group | |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
832 ; @r{ 28-bit binary values} |
6510 | 833 |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
834 (logand 14 13) ; 14 = @r{0000 0000 0000 0000 0000 0000 1110} |
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
835 ; 13 = @r{0000 0000 0000 0000 0000 0000 1101} |
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
836 @result{} 12 ; 12 = @r{0000 0000 0000 0000 0000 0000 1100} |
6510 | 837 @end group |
838 | |
839 @group | |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
840 (logand 14 13 4) ; 14 = @r{0000 0000 0000 0000 0000 0000 1110} |
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
841 ; 13 = @r{0000 0000 0000 0000 0000 0000 1101} |
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
842 ; 4 = @r{0000 0000 0000 0000 0000 0000 0100} |
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
843 @result{} 4 ; 4 = @r{0000 0000 0000 0000 0000 0000 0100} |
6510 | 844 @end group |
845 | |
846 @group | |
847 (logand) | |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
848 @result{} -1 ; -1 = @r{1111 1111 1111 1111 1111 1111 1111} |
6510 | 849 @end group |
850 @end smallexample | |
851 @end defun | |
852 | |
853 @defun logior &rest ints-or-markers | |
854 @cindex logical inclusive or | |
855 @cindex bitwise or | |
856 This function returns the ``inclusive or'' of its arguments: the @var{n}th bit | |
857 is set in the result if, and only if, the @var{n}th bit is set in at least | |
858 one of the arguments. If there are no arguments, the result is zero, | |
859 which is an identity element for this operation. If @code{logior} is | |
860 passed just one argument, it returns that argument. | |
861 | |
862 @smallexample | |
863 @group | |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
864 ; @r{ 28-bit binary values} |
6510 | 865 |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
866 (logior 12 5) ; 12 = @r{0000 0000 0000 0000 0000 0000 1100} |
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
867 ; 5 = @r{0000 0000 0000 0000 0000 0000 0101} |
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
868 @result{} 13 ; 13 = @r{0000 0000 0000 0000 0000 0000 1101} |
6510 | 869 @end group |
870 | |
871 @group | |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
872 (logior 12 5 7) ; 12 = @r{0000 0000 0000 0000 0000 0000 1100} |
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
873 ; 5 = @r{0000 0000 0000 0000 0000 0000 0101} |
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
874 ; 7 = @r{0000 0000 0000 0000 0000 0000 0111} |
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
875 @result{} 15 ; 15 = @r{0000 0000 0000 0000 0000 0000 1111} |
6510 | 876 @end group |
877 @end smallexample | |
878 @end defun | |
879 | |
880 @defun logxor &rest ints-or-markers | |
881 @cindex bitwise exclusive or | |
882 @cindex logical exclusive or | |
883 This function returns the ``exclusive or'' of its arguments: the | |
7115
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
884 @var{n}th bit is set in the result if, and only if, the @var{n}th bit is |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
885 set in an odd number of the arguments. If there are no arguments, the |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
886 result is 0, which is an identity element for this operation. If |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
887 @code{logxor} is passed just one argument, it returns that argument. |
6510 | 888 |
889 @smallexample | |
890 @group | |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
891 ; @r{ 28-bit binary values} |
6510 | 892 |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
893 (logxor 12 5) ; 12 = @r{0000 0000 0000 0000 0000 0000 1100} |
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
894 ; 5 = @r{0000 0000 0000 0000 0000 0000 0101} |
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
895 @result{} 9 ; 9 = @r{0000 0000 0000 0000 0000 0000 1001} |
6510 | 896 @end group |
897 | |
898 @group | |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
899 (logxor 12 5 7) ; 12 = @r{0000 0000 0000 0000 0000 0000 1100} |
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
900 ; 5 = @r{0000 0000 0000 0000 0000 0000 0101} |
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
901 ; 7 = @r{0000 0000 0000 0000 0000 0000 0111} |
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
902 @result{} 14 ; 14 = @r{0000 0000 0000 0000 0000 0000 1110} |
6510 | 903 @end group |
904 @end smallexample | |
905 @end defun | |
906 | |
907 @defun lognot integer | |
908 @cindex logical not | |
909 @cindex bitwise not | |
910 This function returns the logical complement of its argument: the @var{n}th | |
911 bit is one in the result if, and only if, the @var{n}th bit is zero in | |
912 @var{integer}, and vice-versa. | |
913 | |
914 @example | |
915 (lognot 5) | |
916 @result{} -6 | |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
917 ;; 5 = @r{0000 0000 0000 0000 0000 0000 0101} |
6510 | 918 ;; @r{becomes} |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
919 ;; -6 = @r{1111 1111 1111 1111 1111 1111 1010} |
6510 | 920 @end example |
921 @end defun | |
922 | |
11230
c6b70cdf844e
Don't call the special math functions "transcendental".
Richard M. Stallman <rms@gnu.org>
parents:
10558
diff
changeset
|
923 @node Math Functions |
c6b70cdf844e
Don't call the special math functions "transcendental".
Richard M. Stallman <rms@gnu.org>
parents:
10558
diff
changeset
|
924 @section Standard Mathematical Functions |
6510 | 925 @cindex transcendental functions |
926 @cindex mathematical functions | |
927 | |
928 These mathematical functions are available if floating point is | |
929 supported. They allow integers as well as floating point numbers | |
930 as arguments. | |
931 | |
932 @defun sin arg | |
933 @defunx cos arg | |
934 @defunx tan arg | |
935 These are the ordinary trigonometric functions, with argument measured | |
936 in radians. | |
937 @end defun | |
938 | |
939 @defun asin arg | |
7115
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
940 The value of @code{(asin @var{arg})} is a number between @minus{}pi/2 |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
941 and pi/2 (inclusive) whose sine is @var{arg}; if, however, @var{arg} |
6510 | 942 is out of range (outside [-1, 1]), then the result is a NaN. |
943 @end defun | |
944 | |
945 @defun acos arg | |
946 The value of @code{(acos @var{arg})} is a number between 0 and pi | |
947 (inclusive) whose cosine is @var{arg}; if, however, @var{arg} | |
948 is out of range (outside [-1, 1]), then the result is a NaN. | |
949 @end defun | |
950 | |
951 @defun atan arg | |
7115
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
952 The value of @code{(atan @var{arg})} is a number between @minus{}pi/2 |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
953 and pi/2 (exclusive) whose tangent is @var{arg}. |
6510 | 954 @end defun |
955 | |
956 @defun exp arg | |
957 This is the exponential function; it returns @i{e} to the power | |
958 @var{arg}. @i{e} is a fundamental mathematical constant also called the | |
959 base of natural logarithms. | |
960 @end defun | |
961 | |
962 @defun log arg &optional base | |
963 This function returns the logarithm of @var{arg}, with base @var{base}. | |
964 If you don't specify @var{base}, the base @var{e} is used. If @var{arg} | |
965 is negative, the result is a NaN. | |
966 @end defun | |
967 | |
968 @ignore | |
969 @defun expm1 arg | |
970 This function returns @code{(1- (exp @var{arg}))}, but it is more | |
971 accurate than that when @var{arg} is negative and @code{(exp @var{arg})} | |
972 is close to 1. | |
973 @end defun | |
974 | |
975 @defun log1p arg | |
976 This function returns @code{(log (1+ @var{arg}))}, but it is more | |
977 accurate than that when @var{arg} is so small that adding 1 to it would | |
978 lose accuracy. | |
979 @end defun | |
980 @end ignore | |
981 | |
982 @defun log10 arg | |
983 This function returns the logarithm of @var{arg}, with base 10. If | |
7115
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
984 @var{arg} is negative, the result is a NaN. @code{(log10 @var{x})} |
9a9e88e65617
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6510
diff
changeset
|
985 @equiv{} @code{(log @var{x} 10)}, at least approximately. |
6510 | 986 @end defun |
987 | |
988 @defun expt x y | |
10306
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
989 This function returns @var{x} raised to power @var{y}. If both |
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
990 arguments are integers and @var{y} is positive, the result is an |
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
991 integer; in this case, it is truncated to fit the range of possible |
89f8d7f3bd73
Integers now at least 28 bits.
Richard M. Stallman <rms@gnu.org>
parents:
8017
diff
changeset
|
992 integer values. |
6510 | 993 @end defun |
994 | |
995 @defun sqrt arg | |
996 This returns the square root of @var{arg}. If @var{arg} is negative, | |
997 the value is a NaN. | |
998 @end defun | |
999 | |
1000 @node Random Numbers | |
1001 @section Random Numbers | |
1002 @cindex random numbers | |
1003 | |
1004 A deterministic computer program cannot generate true random numbers. | |
1005 For most purposes, @dfn{pseudo-random numbers} suffice. A series of | |
1006 pseudo-random numbers is generated in a deterministic fashion. The | |
1007 numbers are not truly random, but they have certain properties that | |
1008 mimic a random series. For example, all possible values occur equally | |
1009 often in a pseudo-random series. | |
1010 | |
1011 In Emacs, pseudo-random numbers are generated from a ``seed'' number. | |
1012 Starting from any given seed, the @code{random} function always | |
1013 generates the same sequence of numbers. Emacs always starts with the | |
1014 same seed value, so the sequence of values of @code{random} is actually | |
1015 the same in each Emacs run! For example, in one operating system, the | |
1016 first call to @code{(random)} after you start Emacs always returns | |
1017 -1457731, and the second one always returns -7692030. This | |
1018 repeatability is helpful for debugging. | |
1019 | |
1020 If you want truly unpredictable random numbers, execute @code{(random | |
1021 t)}. This chooses a new seed based on the current time of day and on | |
1022 Emacs's process @sc{id} number. | |
1023 | |
1024 @defun random &optional limit | |
1025 This function returns a pseudo-random integer. Repeated calls return a | |
1026 series of pseudo-random integers. | |
1027 | |
12067 | 1028 If @var{limit} is a positive integer, the value is chosen to be |
12098 | 1029 nonnegative and less than @var{limit}. |
6510 | 1030 |
1031 If @var{limit} is @code{t}, it means to choose a new seed based on the | |
1032 current time of day and on Emacs's process @sc{id} number. | |
1033 @c "Emacs'" is incorrect usage! | |
1034 | |
1035 On some machines, any integer representable in Lisp may be the result | |
1036 of @code{random}. On other machines, the result can never be larger | |
1037 than a certain maximum or less than a certain (negative) minimum. | |
1038 @end defun |