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