Commit | Line | Data |
---|---|---|
b8d4c8d0 GM |
1 | @c -*-texinfo-*- |
2 | @c This is part of the GNU Emacs Lisp Reference Manual. | |
ba318903 | 3 | @c Copyright (C) 1990-1995, 1998-1999, 2001-2014 Free Software |
ab422c4d | 4 | @c Foundation, Inc. |
b8d4c8d0 | 5 | @c See the file elisp.texi for copying conditions. |
ecc6530d | 6 | @node Numbers |
b8d4c8d0 GM |
7 | @chapter Numbers |
8 | @cindex integers | |
9 | @cindex numbers | |
10 | ||
11 | GNU Emacs supports two numeric data types: @dfn{integers} and | |
1917cf46 PE |
12 | @dfn{floating-point numbers}. Integers are whole numbers such as |
13 | @minus{}3, 0, 7, 13, and 511. Floating-point numbers are numbers with | |
14 | fractional parts, such as @minus{}4.5, 0.0, and 2.71828. They can | |
15 | also be expressed in exponential notation: @samp{1.5e2} is the same as | |
16 | @samp{150.0}; here, @samp{e2} stands for ten to the second power, and | |
17 | that is multiplied by 1.5. Integer computations are exact, though | |
18 | they may overflow. Floating-point computations often involve rounding | |
19 | errors, as the numbers have a fixed amount of precision. | |
b8d4c8d0 GM |
20 | |
21 | @menu | |
22 | * Integer Basics:: Representation and range of integers. | |
d24880de | 23 | * Float Basics:: Representation and range of floating point. |
b8d4c8d0 GM |
24 | * Predicates on Numbers:: Testing for numbers. |
25 | * Comparison of Numbers:: Equality and inequality predicates. | |
d24880de | 26 | * Numeric Conversions:: Converting float to integer and vice versa. |
b8d4c8d0 | 27 | * Arithmetic Operations:: How to add, subtract, multiply and divide. |
09b73f08 | 28 | * Rounding Operations:: Explicitly rounding floating-point numbers. |
b8d4c8d0 GM |
29 | * Bitwise Operations:: Logical and, or, not, shifting. |
30 | * Math Functions:: Trig, exponential and logarithmic functions. | |
31 | * Random Numbers:: Obtaining random integers, predictable or not. | |
32 | @end menu | |
33 | ||
34 | @node Integer Basics | |
b8d4c8d0 GM |
35 | @section Integer Basics |
36 | ||
37 | The range of values for an integer depends on the machine. The | |
1917cf46 | 38 | minimum range is @minus{}536,870,912 to 536,870,911 (30 bits; i.e., |
b8d4c8d0 | 39 | @ifnottex |
09b73f08 | 40 | @minus{}2**29 |
b8d4c8d0 GM |
41 | @end ifnottex |
42 | @tex | |
1ddd6622 | 43 | @math{-2^{29}} |
b8d4c8d0 GM |
44 | @end tex |
45 | to | |
46 | @ifnottex | |
f99f1641 | 47 | 2**29 @minus{} 1), |
b8d4c8d0 GM |
48 | @end ifnottex |
49 | @tex | |
1ddd6622 | 50 | @math{2^{29}-1}), |
b8d4c8d0 | 51 | @end tex |
48de8b12 CY |
52 | but many machines provide a wider range. Many examples in this |
53 | chapter assume the minimum integer width of 30 bits. | |
b8d4c8d0 GM |
54 | @cindex overflow |
55 | ||
56 | The Lisp reader reads an integer as a sequence of digits with optional | |
fed14fd7 PE |
57 | initial sign and optional final period. An integer that is out of the |
58 | Emacs range is treated as a floating-point number. | |
b8d4c8d0 GM |
59 | |
60 | @example | |
61 | 1 ; @r{The integer 1.} | |
62 | 1. ; @r{The integer 1.} | |
63 | +1 ; @r{Also the integer 1.} | |
64 | -1 ; @r{The integer @minus{}1.} | |
1917cf46 PE |
65 | 9000000000000000000 |
66 | ; @r{The floating-point number 9e18.} | |
b8d4c8d0 GM |
67 | 0 ; @r{The integer 0.} |
68 | -0 ; @r{The integer 0.} | |
69 | @end example | |
70 | ||
71 | @cindex integers in specific radix | |
72 | @cindex radix for reading an integer | |
73 | @cindex base for reading an integer | |
74 | @cindex hex numbers | |
75 | @cindex octal numbers | |
76 | @cindex reading numbers in hex, octal, and binary | |
77 | The syntax for integers in bases other than 10 uses @samp{#} | |
78 | followed by a letter that specifies the radix: @samp{b} for binary, | |
79 | @samp{o} for octal, @samp{x} for hex, or @samp{@var{radix}r} to | |
80 | specify radix @var{radix}. Case is not significant for the letter | |
81 | that specifies the radix. Thus, @samp{#b@var{integer}} reads | |
82 | @var{integer} in binary, and @samp{#@var{radix}r@var{integer}} reads | |
83 | @var{integer} in radix @var{radix}. Allowed values of @var{radix} run | |
84 | from 2 to 36. For example: | |
85 | ||
86 | @example | |
87 | #b101100 @result{} 44 | |
88 | #o54 @result{} 44 | |
89 | #x2c @result{} 44 | |
90 | #24r1k @result{} 44 | |
91 | @end example | |
92 | ||
93 | To understand how various functions work on integers, especially the | |
94 | bitwise operators (@pxref{Bitwise Operations}), it is often helpful to | |
95 | view the numbers in their binary form. | |
96 | ||
1ddd6622 | 97 | In 30-bit binary, the decimal integer 5 looks like this: |
b8d4c8d0 GM |
98 | |
99 | @example | |
001903b5 | 100 | 0000...000101 (30 bits total) |
b8d4c8d0 GM |
101 | @end example |
102 | ||
103 | @noindent | |
001903b5 PE |
104 | (The @samp{...} stands for enough bits to fill out a 30-bit word; in |
105 | this case, @samp{...} stands for twenty 0 bits. Later examples also | |
106 | use the @samp{...} notation to make binary integers easier to read.) | |
b8d4c8d0 GM |
107 | |
108 | The integer @minus{}1 looks like this: | |
109 | ||
110 | @example | |
001903b5 | 111 | 1111...111111 (30 bits total) |
b8d4c8d0 GM |
112 | @end example |
113 | ||
114 | @noindent | |
115 | @cindex two's complement | |
1ddd6622 | 116 | @minus{}1 is represented as 30 ones. (This is called @dfn{two's |
b8d4c8d0 GM |
117 | complement} notation.) |
118 | ||
119 | The negative integer, @minus{}5, is creating by subtracting 4 from | |
120 | @minus{}1. In binary, the decimal integer 4 is 100. Consequently, | |
121 | @minus{}5 looks like this: | |
122 | ||
123 | @example | |
001903b5 | 124 | 1111...111011 (30 bits total) |
b8d4c8d0 GM |
125 | @end example |
126 | ||
09b73f08 | 127 | In this implementation, the largest 30-bit binary integer is |
1ddd6622 | 128 | 536,870,911 in decimal. In binary, it looks like this: |
b8d4c8d0 GM |
129 | |
130 | @example | |
001903b5 | 131 | 0111...111111 (30 bits total) |
b8d4c8d0 GM |
132 | @end example |
133 | ||
134 | Since the arithmetic functions do not check whether integers go | |
1ddd6622 GM |
135 | outside their range, when you add 1 to 536,870,911, the value is the |
136 | negative integer @minus{}536,870,912: | |
b8d4c8d0 GM |
137 | |
138 | @example | |
1ddd6622 GM |
139 | (+ 1 536870911) |
140 | @result{} -536870912 | |
001903b5 | 141 | @result{} 1000...000000 (30 bits total) |
b8d4c8d0 GM |
142 | @end example |
143 | ||
144 | Many of the functions described in this chapter accept markers for | |
145 | arguments in place of numbers. (@xref{Markers}.) Since the actual | |
146 | arguments to such functions may be either numbers or markers, we often | |
147 | give these arguments the name @var{number-or-marker}. When the argument | |
148 | value is a marker, its position value is used and its buffer is ignored. | |
149 | ||
09b73f08 PE |
150 | @cindex largest Lisp integer |
151 | @cindex maximum Lisp integer | |
b8d4c8d0 | 152 | @defvar most-positive-fixnum |
1917cf46 PE |
153 | The value of this variable is the largest integer that Emacs Lisp can |
154 | handle. Typical values are | |
155 | @ifnottex | |
156 | 2**29 @minus{} 1 | |
157 | @end ifnottex | |
158 | @tex | |
159 | @math{2^{29}-1} | |
160 | @end tex | |
161 | on 32-bit and | |
162 | @ifnottex | |
163 | 2**61 @minus{} 1 | |
164 | @end ifnottex | |
165 | @tex | |
166 | @math{2^{61}-1} | |
167 | @end tex | |
168 | on 64-bit platforms. | |
b8d4c8d0 GM |
169 | @end defvar |
170 | ||
09b73f08 PE |
171 | @cindex smallest Lisp integer |
172 | @cindex minimum Lisp integer | |
b8d4c8d0 GM |
173 | @defvar most-negative-fixnum |
174 | The value of this variable is the smallest integer that Emacs Lisp can | |
1917cf46 PE |
175 | handle. It is negative. Typical values are |
176 | @ifnottex | |
177 | @minus{}2**29 | |
178 | @end ifnottex | |
179 | @tex | |
180 | @math{-2^{29}} | |
181 | @end tex | |
182 | on 32-bit and | |
183 | @ifnottex | |
184 | @minus{}2**61 | |
185 | @end ifnottex | |
186 | @tex | |
187 | @math{-2^{61}} | |
188 | @end tex | |
189 | on 64-bit platforms. | |
b8d4c8d0 GM |
190 | @end defvar |
191 | ||
48de8b12 CY |
192 | In Emacs Lisp, text characters are represented by integers. Any |
193 | integer between zero and the value of @code{max-char}, inclusive, is | |
194 | considered to be valid as a character. @xref{String Basics}. | |
57e2db6d | 195 | |
b8d4c8d0 | 196 | @node Float Basics |
09b73f08 | 197 | @section Floating-Point Basics |
b8d4c8d0 | 198 | |
fead402d | 199 | @cindex @acronym{IEEE} floating point |
09b73f08 | 200 | Floating-point numbers are useful for representing numbers that are |
1917cf46 PE |
201 | not integral. The range of floating-point numbers is |
202 | the same as the range of the C data type @code{double} on the machine | |
203 | you are using. On all computers currently supported by Emacs, this is | |
204 | double-precision @acronym{IEEE} floating point. | |
b8d4c8d0 | 205 | |
09b73f08 | 206 | The read syntax for floating-point numbers requires either a decimal |
1917cf46 PE |
207 | point, an exponent, or both. Optional signs (@samp{+} or @samp{-}) |
208 | precede the number and its exponent. For example, @samp{1500.0}, | |
209 | @samp{+15e2}, @samp{15.0e+2}, @samp{+1500000e-3}, and @samp{.15e4} are | |
210 | five ways of writing a floating-point number whose value is 1500. | |
211 | They are all equivalent. Like Common Lisp, Emacs Lisp requires at | |
212 | least one digit after any decimal point in a floating-point number; | |
213 | @samp{1500.} is an integer, not a floating-point number. | |
214 | ||
215 | Emacs Lisp treats @code{-0.0} as numerically equal to ordinary zero | |
216 | with 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 | |
219 | distinguish them. | |
b8d4c8d0 | 220 | |
b8d4c8d0 GM |
221 | @cindex positive infinity |
222 | @cindex negative infinity | |
223 | @cindex infinity | |
224 | @cindex NaN | |
09b73f08 PE |
225 | The @acronym{IEEE} floating-point standard supports positive |
226 | infinity and negative infinity as floating-point values. It also | |
fead402d CY |
227 | provides for a class of values called NaN or ``not-a-number''; |
228 | numerical functions return such values in cases where there is no | |
1917cf46 PE |
229 | correct answer. For example, @code{(/ 0.0 0.0)} returns a NaN@. |
230 | Although NaN values carry a sign, for practical purposes there is no other | |
231 | significant difference between different NaN values in Emacs Lisp. | |
c990426a | 232 | |
1917cf46 | 233 | Here are read syntaxes for these special floating-point values: |
b8d4c8d0 GM |
234 | |
235 | @table @asis | |
1917cf46 PE |
236 | @item infinity |
237 | @samp{1.0e+INF} and @samp{-1.0e+INF} | |
238 | @item not-a-number | |
239 | @samp{0.0e+NaN} and @samp{-0.0e+NaN} | |
b8d4c8d0 GM |
240 | @end table |
241 | ||
1917cf46 | 242 | The following functions are specialized for handling floating-point |
fead402d CY |
243 | numbers: |
244 | ||
1917cf46 PE |
245 | @defun isnan x |
246 | This predicate returns @code{t} if its floating-point argument is a NaN, | |
247 | @code{nil} otherwise. | |
248 | @end defun | |
fead402d | 249 | |
1917cf46 PE |
250 | @defun frexp x |
251 | This function returns a cons cell @code{(@var{s} . @var{e})}, | |
252 | where @var{s} and @var{e} are respectively the significand and | |
253 | exponent of the floating-point number @var{x}. | |
fead402d | 254 | |
1917cf46 PE |
255 | If @var{x} is finite, @var{s} is a floating-point number between 0.5 |
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 | |
263 | If @var{x} is zero or infinity, @var{s} is the same as @var{x}. | |
264 | If @var{x} is a NaN, @var{s} is also a NaN. | |
265 | If @var{x} is zero, @var{e} is 0. | |
fead402d | 266 | @end defun |
b8d4c8d0 | 267 | |
fead402d | 268 | @defun ldexp sig &optional exp |
1917cf46 | 269 | This function returns a floating-point number corresponding to the |
fead402d CY |
270 | significand @var{sig} and exponent @var{exp}. |
271 | @end defun | |
b8d4c8d0 | 272 | |
fead402d CY |
273 | @defun copysign x1 x2 |
274 | This function copies the sign of @var{x2} to the value of @var{x1}, | |
1917cf46 | 275 | and returns the result. @var{x1} and @var{x2} must be floating point. |
fead402d | 276 | @end defun |
b8d4c8d0 GM |
277 | |
278 | @defun logb number | |
279 | This function returns the binary exponent of @var{number}. More | |
c990426a | 280 | precisely, the value is the logarithm of |@var{number}| base 2, rounded |
b8d4c8d0 GM |
281 | down to an integer. |
282 | ||
283 | @example | |
284 | (logb 10) | |
285 | @result{} 3 | |
286 | (logb 10.0e20) | |
287 | @result{} 69 | |
288 | @end example | |
289 | @end defun | |
290 | ||
291 | @node Predicates on Numbers | |
292 | @section Type Predicates for Numbers | |
293 | @cindex predicates for numbers | |
294 | ||
295 | The functions in this section test for numbers, or for a specific | |
296 | type of number. The functions @code{integerp} and @code{floatp} can | |
297 | take any type of Lisp object as argument (they would not be of much | |
298 | use otherwise), but the @code{zerop} predicate requires a number as | |
299 | its argument. See also @code{integer-or-marker-p} and | |
300 | @code{number-or-marker-p}, in @ref{Predicates on Markers}. | |
301 | ||
302 | @defun floatp object | |
09b73f08 PE |
303 | This predicate tests whether its argument is floating point |
304 | and returns @code{t} if so, @code{nil} otherwise. | |
b8d4c8d0 GM |
305 | @end defun |
306 | ||
307 | @defun integerp object | |
308 | This predicate tests whether its argument is an integer, and returns | |
309 | @code{t} if so, @code{nil} otherwise. | |
310 | @end defun | |
311 | ||
312 | @defun numberp object | |
313 | This predicate tests whether its argument is a number (either integer or | |
314 | floating point), and returns @code{t} if so, @code{nil} otherwise. | |
315 | @end defun | |
316 | ||
0f29fa41 | 317 | @defun natnump object |
b8d4c8d0 | 318 | @cindex natural numbers |
bccc0e40 | 319 | This predicate (whose name comes from the phrase ``natural number'') |
0f29fa41 CY |
320 | tests to see whether its argument is a nonnegative integer, and |
321 | returns @code{t} if so, @code{nil} otherwise. 0 is considered | |
322 | non-negative. | |
b8d4c8d0 | 323 | |
1917cf46 PE |
324 | @findex wholenump |
325 | @code{wholenump} is a synonym for @code{natnump}. | |
b8d4c8d0 GM |
326 | @end defun |
327 | ||
328 | @defun zerop number | |
329 | This predicate tests whether its argument is zero, and returns @code{t} | |
330 | if so, @code{nil} otherwise. The argument must be a number. | |
331 | ||
332 | @code{(zerop x)} is equivalent to @code{(= x 0)}. | |
333 | @end defun | |
334 | ||
335 | @node Comparison of Numbers | |
336 | @section Comparison of Numbers | |
337 | @cindex number comparison | |
338 | @cindex comparing numbers | |
339 | ||
340 | To test numbers for numerical equality, you should normally use | |
09b73f08 PE |
341 | @code{=}, not @code{eq}. There can be many distinct floating-point |
342 | objects with the same numeric value. If you use @code{eq} to | |
b8d4c8d0 GM |
343 | compare them, then you test whether two values are the same |
344 | @emph{object}. By contrast, @code{=} compares only the numeric values | |
345 | of the objects. | |
346 | ||
09b73f08 | 347 | In Emacs Lisp, each integer is a unique Lisp object. |
b8d4c8d0 | 348 | Therefore, @code{eq} is equivalent to @code{=} where integers are |
48de8b12 CY |
349 | concerned. It is sometimes convenient to use @code{eq} for comparing |
350 | an unknown value with an integer, because @code{eq} does not report an | |
351 | error if the unknown value is not a number---it accepts arguments of | |
352 | any type. By contrast, @code{=} signals an error if the arguments are | |
353 | not numbers or markers. However, it is better programming practice to | |
354 | use @code{=} if you can, even for comparing integers. | |
355 | ||
356 | Sometimes it is useful to compare numbers with @code{equal}, which | |
b8d4c8d0 GM |
357 | treats two numbers as equal if they have the same data type (both |
358 | integers, or both floating point) and the same value. By contrast, | |
09b73f08 | 359 | @code{=} can treat an integer and a floating-point number as equal. |
b8d4c8d0 GM |
360 | @xref{Equality Predicates}. |
361 | ||
09b73f08 PE |
362 | There is another wrinkle: because floating-point arithmetic is not |
363 | exact, it is often a bad idea to check for equality of floating-point | |
364 | values. Usually it is better to test for approximate equality. | |
b8d4c8d0 GM |
365 | Here's a function to do this: |
366 | ||
367 | @example | |
368 | (defvar fuzz-factor 1.0e-6) | |
369 | (defun approx-equal (x y) | |
370 | (or (and (= x 0) (= y 0)) | |
371 | (< (/ (abs (- x y)) | |
372 | (max (abs x) (abs y))) | |
373 | fuzz-factor))) | |
374 | @end example | |
375 | ||
376 | @cindex CL note---integers vrs @code{eq} | |
377 | @quotation | |
378 | @b{Common Lisp note:} Comparing numbers in Common Lisp always requires | |
379 | @code{=} because Common Lisp implements multi-word integers, and two | |
380 | distinct integer objects can have the same numeric value. Emacs Lisp | |
381 | can have just one integer object for any given value because it has a | |
09b73f08 | 382 | limited range of integers. |
b8d4c8d0 GM |
383 | @end quotation |
384 | ||
3fbba716 TH |
385 | @defun = number-or-marker &rest number-or-markers |
386 | This function tests whether all its arguments are numerically equal, | |
387 | and returns @code{t} if so, @code{nil} otherwise. | |
b8d4c8d0 GM |
388 | @end defun |
389 | ||
390 | @defun eql value1 value2 | |
391 | This function acts like @code{eq} except when both arguments are | |
392 | numbers. It compares numbers by type and numeric value, so that | |
393 | @code{(eql 1.0 1)} returns @code{nil}, but @code{(eql 1.0 1.0)} and | |
394 | @code{(eql 1 1)} both return @code{t}. | |
395 | @end defun | |
396 | ||
397 | @defun /= number-or-marker1 number-or-marker2 | |
398 | This function tests whether its arguments are numerically equal, and | |
399 | returns @code{t} if they are not, and @code{nil} if they are. | |
400 | @end defun | |
401 | ||
3fbba716 TH |
402 | @defun < number-or-marker &rest number-or-markers |
403 | This function tests whether every argument is strictly less than the | |
404 | respective next argument. It returns @code{t} if so, @code{nil} | |
405 | otherwise. | |
b8d4c8d0 GM |
406 | @end defun |
407 | ||
3fbba716 TH |
408 | @defun <= number-or-marker &rest number-or-markers |
409 | This function tests whether every argument is less than or equal to | |
410 | the respective next argument. It returns @code{t} if so, @code{nil} | |
b8d4c8d0 GM |
411 | otherwise. |
412 | @end defun | |
413 | ||
3fbba716 TH |
414 | @defun > number-or-marker &rest number-or-markers |
415 | This function tests whether every argument is strictly greater than | |
416 | the respective next argument. It returns @code{t} if so, @code{nil} | |
b8d4c8d0 GM |
417 | otherwise. |
418 | @end defun | |
419 | ||
3fbba716 TH |
420 | @defun >= number-or-marker &rest number-or-markers |
421 | This function tests whether every argument is greater than or equal to | |
422 | the respective next argument. It returns @code{t} if so, @code{nil} | |
b8d4c8d0 GM |
423 | otherwise. |
424 | @end defun | |
425 | ||
426 | @defun max number-or-marker &rest numbers-or-markers | |
427 | This function returns the largest of its arguments. | |
09b73f08 | 428 | If any of the arguments is floating point, the value is returned |
b8d4c8d0 GM |
429 | as floating point, even if it was given as an integer. |
430 | ||
431 | @example | |
432 | (max 20) | |
433 | @result{} 20 | |
434 | (max 1 2.5) | |
435 | @result{} 2.5 | |
436 | (max 1 3 2.5) | |
437 | @result{} 3.0 | |
438 | @end example | |
439 | @end defun | |
440 | ||
441 | @defun min number-or-marker &rest numbers-or-markers | |
442 | This function returns the smallest of its arguments. | |
09b73f08 | 443 | If any of the arguments is floating point, the value is returned |
b8d4c8d0 GM |
444 | as floating point, even if it was given as an integer. |
445 | ||
446 | @example | |
447 | (min -4 1) | |
448 | @result{} -4 | |
449 | @end example | |
450 | @end defun | |
451 | ||
452 | @defun abs number | |
453 | This function returns the absolute value of @var{number}. | |
454 | @end defun | |
455 | ||
456 | @node Numeric Conversions | |
457 | @section Numeric Conversions | |
458 | @cindex rounding in conversions | |
459 | @cindex number conversions | |
460 | @cindex converting numbers | |
461 | ||
462 | To convert an integer to floating point, use the function @code{float}. | |
463 | ||
464 | @defun float number | |
465 | This returns @var{number} converted to floating point. | |
09b73f08 | 466 | If @var{number} is already floating point, @code{float} returns |
b8d4c8d0 GM |
467 | it unchanged. |
468 | @end defun | |
469 | ||
09b73f08 | 470 | There are four functions to convert floating-point numbers to |
48de8b12 CY |
471 | integers; they differ in how they round. All accept an argument |
472 | @var{number} and an optional argument @var{divisor}. Both arguments | |
09b73f08 | 473 | may be integers or floating-point numbers. @var{divisor} may also be |
b8d4c8d0 GM |
474 | @code{nil}. If @var{divisor} is @code{nil} or omitted, these |
475 | functions convert @var{number} to an integer, or return it unchanged | |
476 | if it already is an integer. If @var{divisor} is non-@code{nil}, they | |
477 | divide @var{number} by @var{divisor} and convert the result to an | |
33f846fb | 478 | integer. If @var{divisor} is zero (whether integer or |
09b73f08 | 479 | floating point), Emacs signals an @code{arith-error} error. |
b8d4c8d0 GM |
480 | |
481 | @defun truncate number &optional divisor | |
482 | This returns @var{number}, converted to an integer by rounding towards | |
483 | zero. | |
484 | ||
485 | @example | |
486 | (truncate 1.2) | |
487 | @result{} 1 | |
488 | (truncate 1.7) | |
489 | @result{} 1 | |
490 | (truncate -1.2) | |
491 | @result{} -1 | |
492 | (truncate -1.7) | |
493 | @result{} -1 | |
494 | @end example | |
495 | @end defun | |
496 | ||
497 | @defun floor number &optional divisor | |
498 | This returns @var{number}, converted to an integer by rounding downward | |
499 | (towards negative infinity). | |
500 | ||
501 | If @var{divisor} is specified, this uses the kind of division | |
502 | operation that corresponds to @code{mod}, rounding downward. | |
503 | ||
504 | @example | |
505 | (floor 1.2) | |
506 | @result{} 1 | |
507 | (floor 1.7) | |
508 | @result{} 1 | |
509 | (floor -1.2) | |
510 | @result{} -2 | |
511 | (floor -1.7) | |
512 | @result{} -2 | |
513 | (floor 5.99 3) | |
514 | @result{} 1 | |
515 | @end example | |
516 | @end defun | |
517 | ||
518 | @defun ceiling number &optional divisor | |
519 | This returns @var{number}, converted to an integer by rounding upward | |
520 | (towards positive infinity). | |
521 | ||
522 | @example | |
523 | (ceiling 1.2) | |
524 | @result{} 2 | |
525 | (ceiling 1.7) | |
526 | @result{} 2 | |
527 | (ceiling -1.2) | |
528 | @result{} -1 | |
529 | (ceiling -1.7) | |
530 | @result{} -1 | |
531 | @end example | |
532 | @end defun | |
533 | ||
534 | @defun round number &optional divisor | |
535 | This returns @var{number}, converted to an integer by rounding towards the | |
536 | nearest integer. Rounding a value equidistant between two integers | |
537 | may choose the integer closer to zero, or it may prefer an even integer, | |
538 | depending on your machine. | |
539 | ||
540 | @example | |
541 | (round 1.2) | |
542 | @result{} 1 | |
543 | (round 1.7) | |
544 | @result{} 2 | |
545 | (round -1.2) | |
546 | @result{} -1 | |
547 | (round -1.7) | |
548 | @result{} -2 | |
549 | @end example | |
550 | @end defun | |
551 | ||
552 | @node Arithmetic Operations | |
553 | @section Arithmetic Operations | |
554 | @cindex arithmetic operations | |
555 | ||
48de8b12 CY |
556 | Emacs Lisp provides the traditional four arithmetic operations |
557 | (addition, subtraction, multiplication, and division), as well as | |
558 | remainder and modulus functions, and functions to add or subtract 1. | |
559 | Except for @code{%}, each of these functions accepts both integer and | |
09b73f08 PE |
560 | floating-point arguments, and returns a floating-point number if any |
561 | argument is floating point. | |
b8d4c8d0 | 562 | |
1917cf46 PE |
563 | Emacs Lisp arithmetic functions do not check for integer overflow. |
564 | Thus @code{(1+ 536870911)} may evaluate to | |
001903b5 | 565 | @minus{}536870912, depending on your hardware. |
b8d4c8d0 GM |
566 | |
567 | @defun 1+ number-or-marker | |
568 | This function returns @var{number-or-marker} plus 1. | |
569 | For example, | |
570 | ||
571 | @example | |
572 | (setq foo 4) | |
573 | @result{} 4 | |
574 | (1+ foo) | |
575 | @result{} 5 | |
576 | @end example | |
577 | ||
578 | This function is not analogous to the C operator @code{++}---it does not | |
579 | increment a variable. It just computes a sum. Thus, if we continue, | |
580 | ||
581 | @example | |
582 | foo | |
583 | @result{} 4 | |
584 | @end example | |
585 | ||
586 | If you want to increment the variable, you must use @code{setq}, | |
587 | like this: | |
588 | ||
589 | @example | |
590 | (setq foo (1+ foo)) | |
591 | @result{} 5 | |
592 | @end example | |
593 | @end defun | |
594 | ||
595 | @defun 1- number-or-marker | |
596 | This function returns @var{number-or-marker} minus 1. | |
597 | @end defun | |
598 | ||
599 | @defun + &rest numbers-or-markers | |
600 | This function adds its arguments together. When given no arguments, | |
601 | @code{+} returns 0. | |
602 | ||
603 | @example | |
604 | (+) | |
605 | @result{} 0 | |
606 | (+ 1) | |
607 | @result{} 1 | |
608 | (+ 1 2 3 4) | |
609 | @result{} 10 | |
610 | @end example | |
611 | @end defun | |
612 | ||
613 | @defun - &optional number-or-marker &rest more-numbers-or-markers | |
614 | The @code{-} function serves two purposes: negation and subtraction. | |
615 | When @code{-} has a single argument, the value is the negative of the | |
616 | argument. When there are multiple arguments, @code{-} subtracts each of | |
617 | the @var{more-numbers-or-markers} from @var{number-or-marker}, | |
618 | cumulatively. If there are no arguments, the result is 0. | |
619 | ||
620 | @example | |
621 | (- 10 1 2 3 4) | |
622 | @result{} 0 | |
623 | (- 10) | |
624 | @result{} -10 | |
625 | (-) | |
626 | @result{} 0 | |
627 | @end example | |
628 | @end defun | |
629 | ||
630 | @defun * &rest numbers-or-markers | |
631 | This function multiplies its arguments together, and returns the | |
632 | product. When given no arguments, @code{*} returns 1. | |
633 | ||
634 | @example | |
635 | (*) | |
636 | @result{} 1 | |
637 | (* 1) | |
638 | @result{} 1 | |
639 | (* 1 2 3 4) | |
640 | @result{} 24 | |
641 | @end example | |
642 | @end defun | |
643 | ||
644 | @defun / dividend divisor &rest divisors | |
645 | This function divides @var{dividend} by @var{divisor} and returns the | |
646 | quotient. If there are additional arguments @var{divisors}, then it | |
647 | divides @var{dividend} by each divisor in turn. Each argument may be a | |
648 | number or a marker. | |
649 | ||
48de8b12 CY |
650 | If all the arguments are integers, the result is an integer, obtained |
651 | by rounding the quotient towards zero after each division. | |
652 | (Hypothetically, some machines may have different rounding behavior | |
653 | for negative arguments, because @code{/} is implemented using the C | |
654 | division operator, which permits machine-dependent rounding; but this | |
655 | does not happen in practice.) | |
b8d4c8d0 GM |
656 | |
657 | @example | |
658 | @group | |
659 | (/ 6 2) | |
660 | @result{} 3 | |
661 | @end group | |
48de8b12 | 662 | @group |
b8d4c8d0 GM |
663 | (/ 5 2) |
664 | @result{} 2 | |
48de8b12 CY |
665 | @end group |
666 | @group | |
b8d4c8d0 GM |
667 | (/ 5.0 2) |
668 | @result{} 2.5 | |
48de8b12 CY |
669 | @end group |
670 | @group | |
b8d4c8d0 GM |
671 | (/ 5 2.0) |
672 | @result{} 2.5 | |
48de8b12 CY |
673 | @end group |
674 | @group | |
b8d4c8d0 GM |
675 | (/ 5.0 2.0) |
676 | @result{} 2.5 | |
48de8b12 CY |
677 | @end group |
678 | @group | |
b8d4c8d0 GM |
679 | (/ 25 3 2) |
680 | @result{} 4 | |
48de8b12 | 681 | @end group |
b8d4c8d0 GM |
682 | @group |
683 | (/ -17 6) | |
48de8b12 | 684 | @result{} -2 |
b8d4c8d0 GM |
685 | @end group |
686 | @end example | |
48de8b12 CY |
687 | |
688 | @cindex @code{arith-error} in division | |
689 | If you divide an integer by the integer 0, Emacs signals an | |
09b73f08 PE |
690 | @code{arith-error} error (@pxref{Errors}). Floating-point division of |
691 | a nonzero number by zero yields either positive or negative infinity | |
692 | (@pxref{Float Basics}). | |
b8d4c8d0 GM |
693 | @end defun |
694 | ||
695 | @defun % dividend divisor | |
696 | @cindex remainder | |
697 | This function returns the integer remainder after division of @var{dividend} | |
698 | by @var{divisor}. The arguments must be integers or markers. | |
699 | ||
48de8b12 CY |
700 | For any two integers @var{dividend} and @var{divisor}, |
701 | ||
702 | @example | |
703 | @group | |
704 | (+ (% @var{dividend} @var{divisor}) | |
705 | (* (/ @var{dividend} @var{divisor}) @var{divisor})) | |
706 | @end group | |
707 | @end example | |
b8d4c8d0 | 708 | |
48de8b12 CY |
709 | @noindent |
710 | always equals @var{dividend}. If @var{divisor} is zero, Emacs signals | |
711 | an @code{arith-error} error. | |
b8d4c8d0 GM |
712 | |
713 | @example | |
714 | (% 9 4) | |
715 | @result{} 1 | |
716 | (% -9 4) | |
717 | @result{} -1 | |
718 | (% 9 -4) | |
719 | @result{} 1 | |
720 | (% -9 -4) | |
721 | @result{} -1 | |
722 | @end example | |
b8d4c8d0 GM |
723 | @end defun |
724 | ||
725 | @defun mod dividend divisor | |
726 | @cindex modulus | |
727 | This function returns the value of @var{dividend} modulo @var{divisor}; | |
728 | in other words, the remainder after division of @var{dividend} | |
729 | by @var{divisor}, but with the same sign as @var{divisor}. | |
730 | The arguments must be numbers or markers. | |
731 | ||
09b73f08 | 732 | Unlike @code{%}, @code{mod} permits floating-point arguments; it |
48de8b12 CY |
733 | rounds the quotient downward (towards minus infinity) to an integer, |
734 | and uses that quotient to compute the remainder. | |
b8d4c8d0 | 735 | |
c990426a PE |
736 | If @var{divisor} is zero, @code{mod} signals an @code{arith-error} |
737 | error if both arguments are integers, and returns a NaN otherwise. | |
b8d4c8d0 GM |
738 | |
739 | @example | |
740 | @group | |
741 | (mod 9 4) | |
742 | @result{} 1 | |
743 | @end group | |
744 | @group | |
745 | (mod -9 4) | |
746 | @result{} 3 | |
747 | @end group | |
748 | @group | |
749 | (mod 9 -4) | |
750 | @result{} -3 | |
751 | @end group | |
752 | @group | |
753 | (mod -9 -4) | |
754 | @result{} -1 | |
755 | @end group | |
756 | @group | |
757 | (mod 5.5 2.5) | |
758 | @result{} .5 | |
759 | @end group | |
760 | @end example | |
761 | ||
762 | For any two numbers @var{dividend} and @var{divisor}, | |
763 | ||
764 | @example | |
765 | @group | |
766 | (+ (mod @var{dividend} @var{divisor}) | |
767 | (* (floor @var{dividend} @var{divisor}) @var{divisor})) | |
768 | @end group | |
769 | @end example | |
770 | ||
771 | @noindent | |
772 | always equals @var{dividend}, subject to rounding error if either | |
773 | argument is floating point. For @code{floor}, see @ref{Numeric | |
774 | Conversions}. | |
775 | @end defun | |
776 | ||
777 | @node Rounding Operations | |
778 | @section Rounding Operations | |
779 | @cindex rounding without conversion | |
780 | ||
781 | The functions @code{ffloor}, @code{fceiling}, @code{fround}, and | |
09b73f08 PE |
782 | @code{ftruncate} take a floating-point argument and return a floating-point |
783 | result whose value is a nearby integer. @code{ffloor} returns the | |
b8d4c8d0 GM |
784 | nearest integer below; @code{fceiling}, the nearest integer above; |
785 | @code{ftruncate}, the nearest integer in the direction towards zero; | |
786 | @code{fround}, the nearest integer. | |
787 | ||
788 | @defun ffloor float | |
789 | This function rounds @var{float} to the next lower integral value, and | |
09b73f08 | 790 | returns that value as a floating-point number. |
b8d4c8d0 GM |
791 | @end defun |
792 | ||
793 | @defun fceiling float | |
794 | This function rounds @var{float} to the next higher integral value, and | |
09b73f08 | 795 | returns that value as a floating-point number. |
b8d4c8d0 GM |
796 | @end defun |
797 | ||
798 | @defun ftruncate float | |
799 | This function rounds @var{float} towards zero to an integral value, and | |
09b73f08 | 800 | returns that value as a floating-point number. |
b8d4c8d0 GM |
801 | @end defun |
802 | ||
803 | @defun fround float | |
804 | This function rounds @var{float} to the nearest integral value, | |
09b73f08 | 805 | and returns that value as a floating-point number. |
b8d4c8d0 GM |
806 | @end defun |
807 | ||
808 | @node Bitwise Operations | |
809 | @section Bitwise Operations on Integers | |
810 | @cindex bitwise arithmetic | |
811 | @cindex logical arithmetic | |
812 | ||
813 | In a computer, an integer is represented as a binary number, a | |
814 | sequence of @dfn{bits} (digits which are either zero or one). A bitwise | |
815 | operation acts on the individual bits of such a sequence. For example, | |
816 | @dfn{shifting} moves the whole sequence left or right one or more places, | |
16152b76 | 817 | reproducing the same pattern ``moved over''. |
b8d4c8d0 GM |
818 | |
819 | The bitwise operations in Emacs Lisp apply only to integers. | |
820 | ||
821 | @defun lsh integer1 count | |
822 | @cindex logical shift | |
823 | @code{lsh}, which is an abbreviation for @dfn{logical shift}, shifts the | |
824 | bits in @var{integer1} to the left @var{count} places, or to the right | |
825 | if @var{count} is negative, bringing zeros into the vacated bits. If | |
826 | @var{count} is negative, @code{lsh} shifts zeros into the leftmost | |
827 | (most-significant) bit, producing a positive result even if | |
828 | @var{integer1} is negative. Contrast this with @code{ash}, below. | |
829 | ||
830 | Here are two examples of @code{lsh}, shifting a pattern of bits one | |
831 | place to the left. We show only the low-order eight bits of the binary | |
832 | pattern; the rest are all zero. | |
833 | ||
834 | @example | |
835 | @group | |
836 | (lsh 5 1) | |
837 | @result{} 10 | |
838 | ;; @r{Decimal 5 becomes decimal 10.} | |
839 | 00000101 @result{} 00001010 | |
840 | ||
841 | (lsh 7 1) | |
842 | @result{} 14 | |
843 | ;; @r{Decimal 7 becomes decimal 14.} | |
844 | 00000111 @result{} 00001110 | |
845 | @end group | |
846 | @end example | |
847 | ||
848 | @noindent | |
849 | As the examples illustrate, shifting the pattern of bits one place to | |
850 | the left produces a number that is twice the value of the previous | |
851 | number. | |
852 | ||
853 | Shifting a pattern of bits two places to the left produces results | |
854 | like this (with 8-bit binary numbers): | |
855 | ||
856 | @example | |
857 | @group | |
858 | (lsh 3 2) | |
859 | @result{} 12 | |
860 | ;; @r{Decimal 3 becomes decimal 12.} | |
861 | 00000011 @result{} 00001100 | |
862 | @end group | |
863 | @end example | |
864 | ||
865 | On the other hand, shifting one place to the right looks like this: | |
866 | ||
867 | @example | |
868 | @group | |
869 | (lsh 6 -1) | |
870 | @result{} 3 | |
871 | ;; @r{Decimal 6 becomes decimal 3.} | |
872 | 00000110 @result{} 00000011 | |
873 | @end group | |
874 | ||
875 | @group | |
876 | (lsh 5 -1) | |
877 | @result{} 2 | |
878 | ;; @r{Decimal 5 becomes decimal 2.} | |
879 | 00000101 @result{} 00000010 | |
880 | @end group | |
881 | @end example | |
882 | ||
883 | @noindent | |
884 | As the example illustrates, shifting one place to the right divides the | |
885 | value of a positive integer by two, rounding downward. | |
886 | ||
c717b326 | 887 | The function @code{lsh}, like all Emacs Lisp arithmetic functions, does |
b8d4c8d0 GM |
888 | not check for overflow, so shifting left can discard significant bits |
889 | and change the sign of the number. For example, left shifting | |
001903b5 | 890 | 536,870,911 produces @minus{}2 in the 30-bit implementation: |
b8d4c8d0 GM |
891 | |
892 | @example | |
1ddd6622 | 893 | (lsh 536870911 1) ; @r{left shift} |
b8d4c8d0 GM |
894 | @result{} -2 |
895 | @end example | |
896 | ||
001903b5 | 897 | In binary, the argument looks like this: |
b8d4c8d0 GM |
898 | |
899 | @example | |
900 | @group | |
1ddd6622 | 901 | ;; @r{Decimal 536,870,911} |
001903b5 | 902 | 0111...111111 (30 bits total) |
b8d4c8d0 GM |
903 | @end group |
904 | @end example | |
905 | ||
906 | @noindent | |
907 | which becomes the following when left shifted: | |
908 | ||
909 | @example | |
910 | @group | |
911 | ;; @r{Decimal @minus{}2} | |
001903b5 | 912 | 1111...111110 (30 bits total) |
b8d4c8d0 GM |
913 | @end group |
914 | @end example | |
915 | @end defun | |
916 | ||
917 | @defun ash integer1 count | |
918 | @cindex arithmetic shift | |
919 | @code{ash} (@dfn{arithmetic shift}) shifts the bits in @var{integer1} | |
920 | to the left @var{count} places, or to the right if @var{count} | |
921 | is negative. | |
922 | ||
923 | @code{ash} gives the same results as @code{lsh} except when | |
924 | @var{integer1} and @var{count} are both negative. In that case, | |
925 | @code{ash} puts ones in the empty bit positions on the left, while | |
926 | @code{lsh} puts zeros in those bit positions. | |
927 | ||
928 | Thus, with @code{ash}, shifting the pattern of bits one place to the right | |
929 | looks like this: | |
930 | ||
931 | @example | |
932 | @group | |
933 | (ash -6 -1) @result{} -3 | |
934 | ;; @r{Decimal @minus{}6 becomes decimal @minus{}3.} | |
001903b5 | 935 | 1111...111010 (30 bits total) |
b8d4c8d0 | 936 | @result{} |
001903b5 | 937 | 1111...111101 (30 bits total) |
b8d4c8d0 GM |
938 | @end group |
939 | @end example | |
940 | ||
941 | In contrast, shifting the pattern of bits one place to the right with | |
942 | @code{lsh} looks like this: | |
943 | ||
944 | @example | |
945 | @group | |
1ddd6622 GM |
946 | (lsh -6 -1) @result{} 536870909 |
947 | ;; @r{Decimal @minus{}6 becomes decimal 536,870,909.} | |
001903b5 | 948 | 1111...111010 (30 bits total) |
b8d4c8d0 | 949 | @result{} |
001903b5 | 950 | 0111...111101 (30 bits total) |
b8d4c8d0 GM |
951 | @end group |
952 | @end example | |
953 | ||
954 | Here are other examples: | |
955 | ||
956 | @c !!! Check if lined up in smallbook format! XDVI shows problem | |
957 | @c with smallbook but not with regular book! --rjc 16mar92 | |
958 | @smallexample | |
959 | @group | |
001903b5 | 960 | ; @r{ 30-bit binary values} |
b8d4c8d0 | 961 | |
be14b9ab PE |
962 | (lsh 5 2) ; 5 = @r{0000...000101} |
963 | @result{} 20 ; = @r{0000...010100} | |
b8d4c8d0 GM |
964 | @end group |
965 | @group | |
966 | (ash 5 2) | |
967 | @result{} 20 | |
be14b9ab PE |
968 | (lsh -5 2) ; -5 = @r{1111...111011} |
969 | @result{} -20 ; = @r{1111...101100} | |
b8d4c8d0 GM |
970 | (ash -5 2) |
971 | @result{} -20 | |
972 | @end group | |
973 | @group | |
be14b9ab PE |
974 | (lsh 5 -2) ; 5 = @r{0000...000101} |
975 | @result{} 1 ; = @r{0000...000001} | |
b8d4c8d0 GM |
976 | @end group |
977 | @group | |
978 | (ash 5 -2) | |
979 | @result{} 1 | |
980 | @end group | |
981 | @group | |
be14b9ab | 982 | (lsh -5 -2) ; -5 = @r{1111...111011} |
001903b5 | 983 | @result{} 268435454 |
be14b9ab | 984 | ; = @r{0011...111110} |
b8d4c8d0 GM |
985 | @end group |
986 | @group | |
be14b9ab PE |
987 | (ash -5 -2) ; -5 = @r{1111...111011} |
988 | @result{} -2 ; = @r{1111...111110} | |
b8d4c8d0 GM |
989 | @end group |
990 | @end smallexample | |
991 | @end defun | |
992 | ||
993 | @defun logand &rest ints-or-markers | |
994 | This function returns the ``logical and'' of the arguments: the | |
995 | @var{n}th bit is set in the result if, and only if, the @var{n}th bit is | |
996 | set in all the arguments. (``Set'' means that the value of the bit is 1 | |
997 | rather than 0.) | |
998 | ||
999 | For example, using 4-bit binary numbers, the ``logical and'' of 13 and | |
1000 | 12 is 12: 1101 combined with 1100 produces 1100. | |
1001 | In both the binary numbers, the leftmost two bits are set (i.e., they | |
1002 | are 1's), so the leftmost two bits of the returned value are set. | |
1003 | However, for the rightmost two bits, each is zero in at least one of | |
1004 | the arguments, so the rightmost two bits of the returned value are 0's. | |
1005 | ||
1006 | @noindent | |
1007 | Therefore, | |
1008 | ||
1009 | @example | |
1010 | @group | |
1011 | (logand 13 12) | |
1012 | @result{} 12 | |
1013 | @end group | |
1014 | @end example | |
1015 | ||
1016 | If @code{logand} is not passed any argument, it returns a value of | |
1017 | @minus{}1. This number is an identity element for @code{logand} | |
1018 | because its binary representation consists entirely of ones. If | |
1019 | @code{logand} is passed just one argument, it returns that argument. | |
1020 | ||
1021 | @smallexample | |
1022 | @group | |
001903b5 | 1023 | ; @r{ 30-bit binary values} |
b8d4c8d0 | 1024 | |
be14b9ab PE |
1025 | (logand 14 13) ; 14 = @r{0000...001110} |
1026 | ; 13 = @r{0000...001101} | |
1027 | @result{} 12 ; 12 = @r{0000...001100} | |
b8d4c8d0 GM |
1028 | @end group |
1029 | ||
1030 | @group | |
be14b9ab PE |
1031 | (logand 14 13 4) ; 14 = @r{0000...001110} |
1032 | ; 13 = @r{0000...001101} | |
1033 | ; 4 = @r{0000...000100} | |
1034 | @result{} 4 ; 4 = @r{0000...000100} | |
b8d4c8d0 GM |
1035 | @end group |
1036 | ||
1037 | @group | |
1038 | (logand) | |
be14b9ab | 1039 | @result{} -1 ; -1 = @r{1111...111111} |
b8d4c8d0 GM |
1040 | @end group |
1041 | @end smallexample | |
1042 | @end defun | |
1043 | ||
1044 | @defun logior &rest ints-or-markers | |
1045 | This function returns the ``inclusive or'' of its arguments: the @var{n}th bit | |
1046 | is set in the result if, and only if, the @var{n}th bit is set in at least | |
1047 | one of the arguments. If there are no arguments, the result is zero, | |
1048 | which is an identity element for this operation. If @code{logior} is | |
1049 | passed just one argument, it returns that argument. | |
1050 | ||
1051 | @smallexample | |
1052 | @group | |
001903b5 | 1053 | ; @r{ 30-bit binary values} |
b8d4c8d0 | 1054 | |
be14b9ab PE |
1055 | (logior 12 5) ; 12 = @r{0000...001100} |
1056 | ; 5 = @r{0000...000101} | |
1057 | @result{} 13 ; 13 = @r{0000...001101} | |
b8d4c8d0 GM |
1058 | @end group |
1059 | ||
1060 | @group | |
be14b9ab PE |
1061 | (logior 12 5 7) ; 12 = @r{0000...001100} |
1062 | ; 5 = @r{0000...000101} | |
1063 | ; 7 = @r{0000...000111} | |
1064 | @result{} 15 ; 15 = @r{0000...001111} | |
b8d4c8d0 GM |
1065 | @end group |
1066 | @end smallexample | |
1067 | @end defun | |
1068 | ||
1069 | @defun logxor &rest ints-or-markers | |
1070 | This function returns the ``exclusive or'' of its arguments: the | |
1071 | @var{n}th bit is set in the result if, and only if, the @var{n}th bit is | |
1072 | set in an odd number of the arguments. If there are no arguments, the | |
1073 | result is 0, which is an identity element for this operation. If | |
1074 | @code{logxor} is passed just one argument, it returns that argument. | |
1075 | ||
1076 | @smallexample | |
1077 | @group | |
001903b5 | 1078 | ; @r{ 30-bit binary values} |
b8d4c8d0 | 1079 | |
be14b9ab PE |
1080 | (logxor 12 5) ; 12 = @r{0000...001100} |
1081 | ; 5 = @r{0000...000101} | |
1082 | @result{} 9 ; 9 = @r{0000...001001} | |
b8d4c8d0 GM |
1083 | @end group |
1084 | ||
1085 | @group | |
be14b9ab PE |
1086 | (logxor 12 5 7) ; 12 = @r{0000...001100} |
1087 | ; 5 = @r{0000...000101} | |
1088 | ; 7 = @r{0000...000111} | |
1089 | @result{} 14 ; 14 = @r{0000...001110} | |
b8d4c8d0 GM |
1090 | @end group |
1091 | @end smallexample | |
1092 | @end defun | |
1093 | ||
1094 | @defun lognot integer | |
1095 | This function returns the logical complement of its argument: the @var{n}th | |
1096 | bit is one in the result if, and only if, the @var{n}th bit is zero in | |
1097 | @var{integer}, and vice-versa. | |
1098 | ||
1099 | @example | |
1100 | (lognot 5) | |
1101 | @result{} -6 | |
001903b5 | 1102 | ;; 5 = @r{0000...000101} (30 bits total) |
b8d4c8d0 | 1103 | ;; @r{becomes} |
001903b5 | 1104 | ;; -6 = @r{1111...111010} (30 bits total) |
b8d4c8d0 GM |
1105 | @end example |
1106 | @end defun | |
1107 | ||
1108 | @node Math Functions | |
1109 | @section Standard Mathematical Functions | |
1110 | @cindex transcendental functions | |
1111 | @cindex mathematical functions | |
1112 | @cindex floating-point functions | |
1113 | ||
09b73f08 | 1114 | These mathematical functions allow integers as well as floating-point |
b8d4c8d0 GM |
1115 | numbers as arguments. |
1116 | ||
1117 | @defun sin arg | |
1118 | @defunx cos arg | |
1119 | @defunx tan arg | |
48de8b12 CY |
1120 | These are the basic trigonometric functions, with argument @var{arg} |
1121 | measured in radians. | |
b8d4c8d0 GM |
1122 | @end defun |
1123 | ||
1124 | @defun asin arg | |
1125 | The value of @code{(asin @var{arg})} is a number between | |
1126 | @ifnottex | |
1127 | @minus{}pi/2 | |
1128 | @end ifnottex | |
1129 | @tex | |
1130 | @math{-\pi/2} | |
1131 | @end tex | |
1132 | and | |
1133 | @ifnottex | |
1134 | pi/2 | |
1135 | @end ifnottex | |
1136 | @tex | |
1137 | @math{\pi/2} | |
1138 | @end tex | |
c990426a PE |
1139 | (inclusive) whose sine is @var{arg}. If @var{arg} is out of range |
1140 | (outside [@minus{}1, 1]), @code{asin} returns a NaN. | |
b8d4c8d0 GM |
1141 | @end defun |
1142 | ||
1143 | @defun acos arg | |
1144 | The value of @code{(acos @var{arg})} is a number between 0 and | |
1145 | @ifnottex | |
1146 | pi | |
1147 | @end ifnottex | |
1148 | @tex | |
1149 | @math{\pi} | |
1150 | @end tex | |
c990426a PE |
1151 | (inclusive) whose cosine is @var{arg}. If @var{arg} is out of range |
1152 | (outside [@minus{}1, 1]), @code{acos} returns a NaN. | |
b8d4c8d0 GM |
1153 | @end defun |
1154 | ||
1155 | @defun atan y &optional x | |
1156 | The value of @code{(atan @var{y})} is a number between | |
1157 | @ifnottex | |
1158 | @minus{}pi/2 | |
1159 | @end ifnottex | |
1160 | @tex | |
1161 | @math{-\pi/2} | |
1162 | @end tex | |
1163 | and | |
1164 | @ifnottex | |
1165 | pi/2 | |
1166 | @end ifnottex | |
1167 | @tex | |
1168 | @math{\pi/2} | |
1169 | @end tex | |
1170 | (exclusive) whose tangent is @var{y}. If the optional second | |
1171 | argument @var{x} is given, the value of @code{(atan y x)} is the | |
1172 | angle in radians between the vector @code{[@var{x}, @var{y}]} and the | |
1173 | @code{X} axis. | |
1174 | @end defun | |
1175 | ||
1176 | @defun exp arg | |
fead402d CY |
1177 | This is the exponential function; it returns @math{e} to the power |
1178 | @var{arg}. | |
b8d4c8d0 GM |
1179 | @end defun |
1180 | ||
1181 | @defun log arg &optional base | |
fead402d CY |
1182 | This function returns the logarithm of @var{arg}, with base |
1183 | @var{base}. If you don't specify @var{base}, the natural base | |
c990426a PE |
1184 | @math{e} is used. If @var{arg} or @var{base} is negative, @code{log} |
1185 | returns a NaN. | |
b8d4c8d0 GM |
1186 | @end defun |
1187 | ||
b8d4c8d0 GM |
1188 | @defun expt x y |
1189 | This function returns @var{x} raised to power @var{y}. If both | |
c717b326 PE |
1190 | arguments are integers and @var{y} is positive, the result is an |
1191 | integer; in this case, overflow causes truncation, so watch out. | |
c990426a PE |
1192 | If @var{x} is a finite negative number and @var{y} is a finite |
1193 | non-integer, @code{expt} returns a NaN. | |
b8d4c8d0 GM |
1194 | @end defun |
1195 | ||
1196 | @defun sqrt arg | |
1197 | This returns the square root of @var{arg}. If @var{arg} is negative, | |
c990426a | 1198 | @code{sqrt} returns a NaN. |
b8d4c8d0 GM |
1199 | @end defun |
1200 | ||
fead402d CY |
1201 | In addition, Emacs defines the following common mathematical |
1202 | constants: | |
1203 | ||
1204 | @defvar float-e | |
1205 | The mathematical constant @math{e} (2.71828@dots{}). | |
1206 | @end defvar | |
1207 | ||
1208 | @defvar float-pi | |
1209 | The mathematical constant @math{pi} (3.14159@dots{}). | |
1210 | @end defvar | |
1211 | ||
b8d4c8d0 GM |
1212 | @node Random Numbers |
1213 | @section Random Numbers | |
1214 | @cindex random numbers | |
1215 | ||
48de8b12 CY |
1216 | A deterministic computer program cannot generate true random |
1217 | numbers. For most purposes, @dfn{pseudo-random numbers} suffice. A | |
1218 | series of pseudo-random numbers is generated in a deterministic | |
1219 | fashion. The numbers are not truly random, but they have certain | |
1220 | properties that mimic a random series. For example, all possible | |
1221 | values occur equally often in a pseudo-random series. | |
b8d4c8d0 | 1222 | |
48de8b12 CY |
1223 | Pseudo-random numbers are generated from a ``seed''. Starting from |
1224 | any given seed, the @code{random} function always generates the same | |
1225 | sequence of numbers. By default, Emacs initializes the random seed at | |
1226 | startup, in such a way that the sequence of values of @code{random} | |
1227 | (with overwhelming likelihood) differs in each Emacs run. | |
0e23ef9d | 1228 | |
48de8b12 | 1229 | Sometimes you want the random number sequence to be repeatable. For |
0e23ef9d PE |
1230 | example, when debugging a program whose behavior depends on the random |
1231 | number sequence, it is helpful to get the same behavior in each | |
1232 | program run. To make the sequence repeat, execute @code{(random "")}. | |
1233 | This sets the seed to a constant value for your particular Emacs | |
1234 | executable (though it may differ for other Emacs builds). You can use | |
1235 | other strings to choose various seed values. | |
b8d4c8d0 GM |
1236 | |
1237 | @defun random &optional limit | |
1238 | This function returns a pseudo-random integer. Repeated calls return a | |
1239 | series of pseudo-random integers. | |
1240 | ||
1241 | If @var{limit} is a positive integer, the value is chosen to be | |
48de8b12 | 1242 | nonnegative and less than @var{limit}. Otherwise, the value might be |
1df7defd | 1243 | any integer representable in Lisp, i.e., an integer between |
48de8b12 CY |
1244 | @code{most-negative-fixnum} and @code{most-positive-fixnum} |
1245 | (@pxref{Integer Basics}). | |
b8d4c8d0 GM |
1246 | |
1247 | If @var{limit} is @code{t}, it means to choose a new seed based on the | |
1248 | current time of day and on Emacs's process @acronym{ID} number. | |
b8d4c8d0 | 1249 | |
0e23ef9d PE |
1250 | If @var{limit} is a string, it means to choose a new seed based on the |
1251 | string's contents. | |
1252 | ||
b8d4c8d0 | 1253 | @end defun |