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