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