2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4 @c See the file elisp.texi for copying conditions.
5 @setfilename ../info/numbers
6 @node Numbers, Strings and Characters, Types of Lisp Object, Top
11 GNU Emacs supports two numeric data types: @dfn{integers} and
12 @dfn{floating point numbers}. Integers are whole numbers such as
13 @minus{}3, 0, 7, 13, and 511. Their values are exact. Floating point
14 numbers are numbers with fractional parts, such as @minus{}4.5, 0.0, or
15 2.71828. They can also be expressed in an exponential notation as well:
16 thus, 1.5e2 equals 150; in this example, @samp{e2} stands for ten to the
17 second power, and is multiplied by 1.5. Floating point values are not
18 exact; they have a fixed, limited amount of precision.
20 Support for floating point numbers is a new feature in Emacs 19, and it
21 is controlled by a separate compilation option, so you may encounter a site
22 where Emacs does not support them.
25 * Integer Basics:: Representation and range of integers.
26 * Float Basics:: Representation and range of floating point.
27 * Predicates on Numbers:: Testing for numbers.
28 * Comparison of Numbers:: Equality and inequality predicates.
29 * Numeric Conversions:: Converting float to integer and vice versa.
30 * Arithmetic Operations:: How to add, subtract, multiply and divide.
31 * Rounding Operations:: Explicitly rounding floating point numbers.
32 * Bitwise Operations:: Logical and, or, not, shifting.
33 * Transcendental Functions:: Trig, exponential and logarithmic functions.
34 * Random Numbers:: Obtaining random integers, predictable or not.
38 @comment node-name, next, previous, up
39 @section Integer Basics
41 The range of values for an integer depends on the machine. The
42 range is @minus{}8388608 to 8388607 (24 bits; i.e.,
56 on most machines, but on others it is @minus{}16777216 to 16777215 (25
57 bits), or @minus{}33554432 to 33554431 (26 bits). Many examples in this
58 chapter assume an integer has 24 bits.
61 The Lisp reader reads an integer as a sequence of digits with optional
62 initial sign and optional final period.
65 1 ; @r{The integer 1.}
66 1. ; @r{The integer 1.}
67 +1 ; @r{Also the integer 1.}
68 -1 ; @r{The integer @minus{}1.}
69 16777217 ; @r{Also the integer 1, due to overflow.}
70 0 ; @r{The integer 0.}
71 -0 ; @r{The integer 0.}
74 To understand how various functions work on integers, especially the
75 bitwise operators (@pxref{Bitwise Operations}), it is often helpful to
76 view the numbers in their binary form.
78 In 24 bit binary, the decimal integer 5 looks like this:
81 0000 0000 0000 0000 0000 0101
85 (We have inserted spaces between groups of 4 bits, and two spaces
86 between groups of 8 bits, to make the binary integer easier to read.)
88 The integer @minus{}1 looks like this:
91 1111 1111 1111 1111 1111 1111
95 @cindex two's complement
96 @minus{}1 is represented as 24 ones. (This is called @dfn{two's
97 complement} notation.)
99 The negative integer, @minus{}5, is creating by subtracting 4 from
100 @minus{}1. In binary, the decimal integer 4 is 100. Consequently,
101 @minus{}5 looks like this:
104 1111 1111 1111 1111 1111 1011
107 In this implementation, the largest 24 bit binary integer is the
108 decimal integer 8,388,607. In binary, it looks like this:
111 0111 1111 1111 1111 1111 1111
114 Since the arithmetic functions do not check whether integers go
115 outside their range, when you add 1 to 8,388,607, the value is negative
116 integer @minus{}8,388,608:
121 @result{} 1000 0000 0000 0000 0000 0000
124 Many of the following functions accept markers for arguments as well
125 as integers. (@xref{Markers}.) More precisely, the actual arguments to
126 such functions may be either integers or markers, which is why we often
127 give these arguments the name @var{int-or-marker}. When the argument
128 value is a marker, its position value is used and its buffer is ignored.
131 In version 19, except where @emph{integer} is specified as an
132 argument, all of the functions for markers and integers also work for
133 floating point numbers.
137 @section Floating Point Basics
139 @cindex @code{LISP_FLOAT_TYPE} configuration macro
140 Emacs version 19 supports floating point numbers, if compiled with the
141 macro @code{LISP_FLOAT_TYPE} defined. The precise range of floating
142 point numbers is machine-specific; it is the same as the range of the C
143 data type @code{double} on the machine in question.
145 The printed representation for floating point numbers requires either
146 a decimal point (with at least one digit following), an exponent, or
147 both. For example, @samp{1500.0}, @samp{15e2}, @samp{15.0e2},
148 @samp{1.5e3}, and @samp{.15e4} are five ways of writing a floating point
149 number whose value is 1500. They are all equivalent. You can also use
150 a minus sign to write negative floating point numbers, as in
153 @cindex IEEE floating point
154 @cindex positive infinity
155 @cindex negative infinity
158 Most modern computers support the IEEE floating point standard, which
159 provides for positive infinity and negative infinity as floating point
160 values. It also provides for a value called NaN or ``not-a-number''
161 which is the result you get from numerical functions in cases where
162 there is no correct answer. For example, @code{(sqrt -1.0)} returns
163 NaN. There is no read syntax for NaN or infinities; perhaps we should
164 create a syntax in the future.
166 You can use @code{logb} to extract the binary exponent of a floating
167 point number (or estimate the logarithm of an integer):
170 This function returns the binary exponent of @var{number}. More
171 precisely, the value is the logarithm of @var{number} base 2, rounded
175 @node Predicates on Numbers
176 @section Type Predicates for Numbers
178 The functions in this section test whether the argument is a number or
179 whether it is a certain sort of number. The functions @code{integerp}
180 and @code{floatp} can take any type of Lisp object as argument (the
181 predicates would not be of much use otherwise); but the @code{zerop}
182 predicate requires a number as its argument. See also
183 @code{integer-or-marker-p} and @code{number-or-marker-p}, in
184 @ref{Predicates on Markers}.
187 This predicate tests whether its argument is a floating point
188 number and returns @code{t} if so, @code{nil} otherwise.
190 @code{floatp} does not exist in Emacs versions 18 and earlier.
193 @defun integerp object
194 This predicate tests whether its argument is an integer, and returns
195 @code{t} if so, @code{nil} otherwise.
198 @defun numberp object
199 This predicate tests whether its argument is a number (either integer or
200 floating point), and returns @code{t} if so, @code{nil} otherwise.
203 @defun natnump object
204 @cindex natural numbers
205 The @code{natnump} predicate (whose name comes from the phrase
206 ``natural-number-p'') tests to see whether its argument is a nonnegative
207 integer, and returns @code{t} if so, @code{nil} otherwise. 0 is
208 considered non-negative.
210 Markers are not converted to integers, hence @code{natnump} of a marker
211 is always @code{nil}.
213 People have pointed out that this function is misnamed, because the term
214 ``natural number'' is usually understood as excluding zero. We are open
215 to suggestions for a better name to use in a future version.
219 This predicate tests whether its argument is zero, and returns @code{t}
220 if so, @code{nil} otherwise. The argument must be a number.
222 These two forms are equivalent: @code{(zerop x)} @equiv{} @code{(= x 0)}.
225 @node Comparison of Numbers
226 @section Comparison of Numbers
227 @cindex number equality
229 Floating point numbers in Emacs Lisp actually take up storage, and
230 there can be many distinct floating point number objects with the same
231 numeric value. If you use @code{eq} to compare them, then you test
232 whether two values are the same @emph{object}. If you want to test for
233 numerical equality, use @code{=}.
235 If you use @code{eq} to compare two integers, it always returns
236 @code{t} if they have the same value. This is sometimes useful, because
237 @code{eq} accepts arguments of any type and never causes an error,
238 whereas @code{=} signals an error if the arguments are not numbers or
239 markers. However, it is a good idea to use @code{=} if you can, even
240 for comparing integers, just in case we change the representation of
241 integers in a future Emacs version.
243 There is another wrinkle: because floating point arithmetic is not
244 exact, it is often a bad idea to check for equality of two floating
245 point values. Usually it is better to test for approximate equality.
246 Here's a function to do this:
249 (defvar fuzz-factor 1.0e-6)
250 (defun approx-equal (x y)
252 (max (abs x) (abs y)))
256 @cindex CL note---integers vrs @code{eq}
258 @b{Common Lisp note:} comparing numbers in Common Lisp always requires
259 @code{=} because Common Lisp implements multi-word integers, and two
260 distinct integer objects can have the same numeric value. Emacs Lisp
261 can have just one integer object for any given value because it has a
262 limited range of integer values.
265 @defun = number-or-marker1 number-or-marker2
266 This function tests whether its arguments are numerically equal, and
267 returns @code{t} if so, @code{nil} otherwise.
270 @defun /= number-or-marker1 number-or-marker2
271 This function tests whether its arguments are numerically equal, and
272 returns @code{t} if they are not, and @code{nil} if they are.
275 @defun < number-or-marker1 number-or-marker2
276 This function tests whether its first argument is strictly less than
277 its second argument. It returns @code{t} if so, @code{nil} otherwise.
280 @defun <= number-or-marker1 number-or-marker2
281 This function tests whether its first argument is less than or equal
282 to its second argument. It returns @code{t} if so, @code{nil}
286 @defun > number-or-marker1 number-or-marker2
287 This function tests whether its first argument is strictly greater
288 than its second argument. It returns @code{t} if so, @code{nil}
292 @defun >= number-or-marker1 number-or-marker2
293 This function tests whether its first argument is greater than or
294 equal to its second argument. It returns @code{t} if so, @code{nil}
298 @defun max number-or-marker &rest numbers-or-markers
299 This function returns the largest of its arguments.
311 @defun min number-or-marker &rest numbers-or-markers
312 This function returns the smallest of its arguments.
320 @node Numeric Conversions
321 @section Numeric Conversions
322 @cindex rounding in conversions
324 To convert an integer to floating point, use the function @code{float}.
327 This returns @var{number} converted to floating point.
328 If @var{number} is already a floating point number, @code{float} returns
332 There are four functions to convert floating point numbers to integers;
333 they differ in how they round. These functions accept integer arguments
334 also, and return such arguments unchanged.
336 @defun truncate number
337 This returns @var{number}, converted to an integer by rounding towards
341 @defun floor number &optional divisor
342 This returns @var{number}, converted to an integer by rounding downward
343 (towards negative infinity).
345 If @var{divisor} is specified, @var{number} is divided by @var{divisor}
346 before the floor is taken; this is the division operation that
347 corresponds to @code{mod}. An @code{arith-error} results if
351 @defun ceiling number
352 This returns @var{number}, converted to an integer by rounding upward
353 (towards positive infinity).
357 This returns @var{number}, converted to an integer by rounding towards the
361 @node Arithmetic Operations
362 @section Arithmetic Operations
364 Emacs Lisp provides the traditional four arithmetic operations:
365 addition, subtraction, multiplication, and division. Remainder and modulus
366 functions supplement the division functions. The functions to
367 add or subtract 1 are provided because they are traditional in Lisp and
370 All of these functions except @code{%} return a floating point value
371 if any argument is floating.
373 It is important to note that in GNU Emacs Lisp, arithmetic functions
374 do not check for overflow. Thus @code{(1+ 8388607)} may evaluate to
375 @minus{}8388608, depending on your hardware.
377 @defun 1+ number-or-marker
378 This function returns @var{number-or-marker} plus 1.
388 This function is not analogous to the C operator @code{++}---it does
389 not increment a variable. It just computes a sum. Thus,
396 If you want to increment the variable, you must use @code{setq},
405 @defun 1- number-or-marker
406 This function returns @var{number-or-marker} minus 1.
410 This returns the absolute value of @var{number}.
413 @defun + &rest numbers-or-markers
414 This function adds its arguments together. When given no arguments,
415 @code{+} returns 0. It does not check for overflow.
427 @defun - &optional number-or-marker &rest other-numbers-or-markers
428 The @code{-} function serves two purposes: negation and subtraction.
429 When @code{-} has a single argument, the value is the negative of the
430 argument. When there are multiple arguments, @code{-} subtracts each of
431 the @var{other-numbers-or-markers} from @var{number-or-marker},
432 cumulatively. If there are no arguments, the result is 0. This
433 function does not check for overflow.
445 @defun * &rest numbers-or-markers
446 This function multiplies its arguments together, and returns the
447 product. When given no arguments, @code{*} returns 1. It does
448 not check for overflow.
460 @defun / dividend divisor &rest divisors
461 This function divides @var{dividend} by @var{divisors} and returns the
462 quotient. If there are additional arguments @var{divisors}, then it
463 divides @var{dividend} by each divisor in turn. Each argument may be a
466 If all the arguments are integers, then the result is an integer too.
467 This means the result has to be rounded. On most machines, the result
468 is rounded towards zero after each division, but some machines may round
469 differently with negative arguments. This is because the Lisp function
470 @code{/} is implemented using the C division operator, which also
471 permits machine-dependent rounding. As a practical matter, all known
472 machines round in the standard fashion.
474 @cindex @code{arith-error} in division
475 If you divide by 0, an @code{arith-error} error is signaled.
489 The result of @code{(/ -17 6)} could in principle be -3 on some
493 @defun % dividend divisor
495 This function returns the integer remainder after division of @var{dividend}
496 by @var{divisor}. The arguments must be integers or markers.
498 For negative arguments, the remainder is in principle machine-dependent
499 since the quotient is; but in practice, all known machines behave alike.
501 An @code{arith-error} results if @var{divisor} is 0.
514 For any two integers @var{dividend} and @var{divisor},
518 (+ (% @var{dividend} @var{divisor})
519 (* (/ @var{dividend} @var{divisor}) @var{divisor}))
524 always equals @var{dividend}.
527 @defun mod dividend divisor
529 This function returns the value of @var{dividend} modulo @var{divisor};
530 in other words, the remainder after division of @var{dividend}
531 by @var{divisor}, but with the same sign as @var{divisor}.
532 The arguments must be numbers or markers.
534 Unlike @code{%}, @code{mod} returns a well-defined result for negative
535 arguments. It also permits floating point arguments; it rounds the
536 quotient downward (towards minus infinity) to an integer, and uses that
537 quotient to compute the remainder.
539 An @code{arith-error} results if @var{divisor} is 0.
554 For any two numbers @var{dividend} and @var{divisor},
558 (+ (mod @var{dividend} @var{divisor})
559 (* (floor @var{dividend} @var{divisor}) @var{divisor}))
564 always equals @var{dividend}, subject to rounding error if
565 either argument is floating point.
568 @node Rounding Operations
569 @section Rounding Operations
570 @cindex rounding without conversion
572 The functions @code{ffloor}, @code{fceil}, @code{fround} and
573 @code{ftruncate} take a floating point argument and return a floating
574 point result whose value is a nearby integer. @code{ffloor} returns the
575 nearest integer below; @code{fceil}, the nearest integer above;
576 @code{ftrucate}, the nearest integer in the direction towards zero;
577 @code{fround}, the nearest integer.
580 This function rounds @var{float} to the next lower integral value, and
581 returns that value as a floating point number.
585 This function rounds @var{float} to the next higher integral value, and
586 returns that value as a floating point number.
590 This function rounds @var{float} towards zero to an integral value, and
591 returns that value as a floating point number.
595 This function rounds @var{float} to the nearest integral value,
596 and returns that value as a floating point number.
599 @node Bitwise Operations
600 @section Bitwise Operations on Integers
602 In a computer, an integer is represented as a binary number, a
603 sequence of @dfn{bits} (digits which are either zero or one). A bitwise
604 operation acts on the individual bits of such a sequence. For example,
605 @dfn{shifting} moves the whole sequence left or right one or more places,
606 reproducing the same pattern ``moved over''.
608 The bitwise operations in Emacs Lisp apply only to integers.
610 @defun lsh integer1 count
611 @cindex logical shift
612 @code{lsh}, which is an abbreviation for @dfn{logical shift}, shifts the
613 bits in @var{integer1} to the left @var{count} places, or to the
614 right if @var{count} is negative. If @var{count} is negative,
615 @code{lsh} shifts zeros into the most-significant bit, producing a
616 positive result even if @var{integer1} is negative. Contrast this with
619 Thus, the decimal number 5 is the binary number 00000101. Shifted once
620 to the left, with a zero put in the one's place, the number becomes
621 00001010, decimal 10.
623 Here are two examples of shifting the pattern of bits one place to the
624 left. Since the contents of the rightmost place has been moved one
625 place to the left, a value has to be inserted into the rightmost place.
626 With @code{lsh}, a zero is placed into the rightmost place. (These
627 examples show only the low-order eight bits of the binary pattern; the
634 ;; @r{Decimal 5 becomes decimal 10.}
635 00000101 @result{} 00001010
639 ;; @r{Decimal 7 becomes decimal 14.}
640 00000111 @result{} 00001110
645 As the examples illustrate, shifting the pattern of bits one place to
646 the left produces a number that is twice the value of the previous
649 Note, however that functions do not check for overflow, and a returned
650 value may be negative (and in any case, no more than a 24 bit value)
651 when an integer is sufficiently left shifted.
653 For example, left shifting 8,388,607 produces @minus{}2:
656 (lsh 8388607 1) ; @r{left shift}
660 In binary, in the 24 bit implementation, the numbers looks like this:
664 ;; @r{Decimal 8,388,607}
665 0111 1111 1111 1111 1111 1111
670 which becomes the following when left shifted:
674 ;; @r{Decimal @minus{}2}
675 1111 1111 1111 1111 1111 1110
679 Shifting the pattern of bits two places to the left produces results
680 like this (with 8-bit binary numbers):
686 ;; @r{Decimal 3 becomes decimal 12.}
687 00000011 @result{} 00001100
691 On the other hand, shifting the pattern of bits one place to the right
698 ;; @r{Decimal 6 becomes decimal 3.}
699 00000110 @result{} 00000011
705 ;; @r{Decimal 5 becomes decimal 2.}
706 00000101 @result{} 00000010
711 As the example illustrates, shifting the pattern of bits one place to
712 the right divides the value of the binary number by two, rounding downward.
715 @defun ash integer1 count
716 @cindex arithmetic shift
717 @code{ash} (@dfn{arithmetic shift}) shifts the bits in @var{integer1}
718 to the left @var{count} places, or to the right if @var{count}
721 @code{ash} gives the same results as @code{lsh} except when
722 @var{integer1} and @var{count} are both negative. In that case,
723 @code{ash} puts a one in the leftmost position, while @code{lsh} puts
724 a zero in the leftmost position.
726 Thus, with @code{ash}, shifting the pattern of bits one place to the right
731 (ash -6 -1) @result{} -3
732 ;; @r{Decimal @minus{}6 becomes decimal @minus{}3.}
733 1111 1111 1111 1111 1111 1010
735 1111 1111 1111 1111 1111 1101
739 In contrast, shifting the pattern of bits one place to the right with
740 @code{lsh} looks like this:
744 (lsh -6 -1) @result{} 8388605
745 ;; @r{Decimal @minus{}6 becomes decimal 8,388,605.}
746 1111 1111 1111 1111 1111 1010
748 0111 1111 1111 1111 1111 1101
753 In this case, the 1 in the leftmost position is shifted one place to the
754 right, and a zero is shifted into the leftmost position.
756 Here are other examples:
758 @c !!! Check if lined up in smallbook format! XDVI shows problem
759 @c with smallbook but not with regular book! --rjc 16mar92
762 ; @r{ 24-bit binary values}
764 (lsh 5 2) ; 5 = @r{0000 0000 0000 0000 0000 0101}
765 @result{} 20 ; 20 = @r{0000 0000 0000 0000 0001 0100}
770 (lsh -5 2) ; -5 = @r{1111 1111 1111 1111 1111 1011}
771 @result{} -20 ; -20 = @r{1111 1111 1111 1111 1110 1100}
776 (lsh 5 -2) ; 5 = @r{0000 0000 0000 0000 0000 0101}
777 @result{} 1 ; 1 = @r{0000 0000 0000 0000 0000 0001}
784 (lsh -5 -2) ; -5 = @r{1111 1111 1111 1111 1111 1011}
785 @result{} 4194302 ; @r{0011 1111 1111 1111 1111 1110}
788 (ash -5 -2) ; -5 = @r{1111 1111 1111 1111 1111 1011}
789 @result{} -2 ; -2 = @r{1111 1111 1111 1111 1111 1110}
794 @defun logand &rest ints-or-markers
797 This function returns the ``logical and'' of the arguments: the
798 @var{n}th bit is set in the result if, and only if, the @var{n}th bit is
799 set in all the arguments. (``Set'' means that the value of the bit is 1
802 For example, using 4-bit binary numbers, the ``logical and'' of 13 and
803 12 is 12: 1101 combined with 1100 produces 1100.
805 In both the binary numbers, the leftmost two bits are set (i.e., they
806 are 1's), so the leftmost two bits of the returned value are set.
807 However, for the rightmost two bits, each is zero in at least one of
808 the arguments, so the rightmost two bits of the returned value are 0's.
820 If @code{logand} is not passed any argument, it returns a value of
821 @minus{}1. This number is an identity element for @code{logand}
822 because its binary representation consists entirely of ones. If
823 @code{logand} is passed just one argument, it returns that argument.
827 ; @r{ 24-bit binary values}
829 (logand 14 13) ; 14 = @r{0000 0000 0000 0000 0000 1110}
830 ; 13 = @r{0000 0000 0000 0000 0000 1101}
831 @result{} 12 ; 12 = @r{0000 0000 0000 0000 0000 1100}
835 (logand 14 13 4) ; 14 = @r{0000 0000 0000 0000 0000 1110}
836 ; 13 = @r{0000 0000 0000 0000 0000 1101}
837 ; 4 = @r{0000 0000 0000 0000 0000 0100}
838 @result{} 4 ; 4 = @r{0000 0000 0000 0000 0000 0100}
843 @result{} -1 ; -1 = @r{1111 1111 1111 1111 1111 1111}
848 @defun logior &rest ints-or-markers
849 @cindex logical inclusive or
851 This function returns the ``inclusive or'' of its arguments: the @var{n}th bit
852 is set in the result if, and only if, the @var{n}th bit is set in at least
853 one of the arguments. If there are no arguments, the result is zero,
854 which is an identity element for this operation. If @code{logior} is
855 passed just one argument, it returns that argument.
859 ; @r{ 24-bit binary values}
861 (logior 12 5) ; 12 = @r{0000 0000 0000 0000 0000 1100}
862 ; 5 = @r{0000 0000 0000 0000 0000 0101}
863 @result{} 13 ; 13 = @r{0000 0000 0000 0000 0000 1101}
867 (logior 12 5 7) ; 12 = @r{0000 0000 0000 0000 0000 1100}
868 ; 5 = @r{0000 0000 0000 0000 0000 0101}
869 ; 7 = @r{0000 0000 0000 0000 0000 0111}
870 @result{} 15 ; 15 = @r{0000 0000 0000 0000 0000 1111}
875 @defun logxor &rest ints-or-markers
876 @cindex bitwise exclusive or
877 @cindex logical exclusive or
878 This function returns the ``exclusive or'' of its arguments: the
879 @var{n}th bit is set in the result if, and only if, the @var{n}th bit
880 is set in an odd number of the arguments. If there are no arguments,
881 the result is 0. If @code{logxor} is passed just one argument, it returns
886 ; @r{ 24-bit binary values}
888 (logxor 12 5) ; 12 = @r{0000 0000 0000 0000 0000 1100}
889 ; 5 = @r{0000 0000 0000 0000 0000 0101}
890 @result{} 9 ; 9 = @r{0000 0000 0000 0000 0000 1001}
894 (logxor 12 5 7) ; 12 = @r{0000 0000 0000 0000 0000 1100}
895 ; 5 = @r{0000 0000 0000 0000 0000 0101}
896 ; 7 = @r{0000 0000 0000 0000 0000 0111}
897 @result{} 14 ; 14 = @r{0000 0000 0000 0000 0000 1110}
902 @defun lognot integer
905 This function returns the logical complement of its argument: the @var{n}th
906 bit is one in the result if, and only if, the @var{n}th bit is zero in
907 @var{integer}, and vice-versa.
912 ;; 5 = @r{0000 0000 0000 0000 0000 0101}
914 ;; -6 = @r{1111 1111 1111 1111 1111 1010}
918 @node Transcendental Functions
919 @section Transcendental Functions
920 @cindex transcendental functions
921 @cindex mathematical functions
923 These mathematical functions are available if floating point is
924 supported. They allow integers as well as floating point numbers
930 These are the ordinary trigonometric functions, with argument measured
935 The value of @code{(asin @var{arg})} is a number between @minus{} pi / 2
936 and pi / 2 (inclusive) whose sine is @var{arg}; if, however, @var{arg}
937 is out of range (outside [-1, 1]), then the result is a NaN.
941 The value of @code{(acos @var{arg})} is a number between 0 and pi
942 (inclusive) whose cosine is @var{arg}; if, however, @var{arg}
943 is out of range (outside [-1, 1]), then the result is a NaN.
947 The value of @code{(atan @var{arg})} is a number between @minus{} pi / 2
948 and pi / 2 (exclusive) whose tangent is @var{arg}.
952 This is the exponential function; it returns @i{e} to the power
953 @var{arg}. @i{e} is a fundamental mathematical constant also called the
954 base of natural logarithms.
957 @defun log arg &optional base
958 This function returns the logarithm of @var{arg}, with base @var{base}.
959 If you don't specify @var{base}, the base @var{e} is used. If @var{arg}
960 is negative, the result is a NaN.
965 This function returns @code{(1- (exp @var{arg}))}, but it is more
966 accurate than that when @var{arg} is negative and @code{(exp @var{arg})}
971 This function returns @code{(log (1+ @var{arg}))}, but it is more
972 accurate than that when @var{arg} is so small that adding 1 to it would
978 This function returns the logarithm of @var{arg}, with base 10. If
979 @var{arg} is negative, the result is a NaN.
983 This function returns @var{x} raised to power @var{y}.
987 This returns the square root of @var{arg}. If @var{arg} is negative,
992 @section Random Numbers
993 @cindex random numbers
995 A deterministic computer program cannot generate true random numbers.
996 For most purposes, @dfn{pseudo-random numbers} suffice. A series of
997 pseudo-random numbers is generated in a deterministic fashion. The
998 numbers are not truly random, but they have certain properties that
999 mimic a random series. For example, all possible values occur equally
1000 often in a pseudo-random series.
1002 In Emacs, pseudo-random numbers are generated from a ``seed'' number.
1003 Starting from any given seed, the @code{random} function always
1004 generates the same sequence of numbers. Emacs always starts with the
1005 same seed value, so the sequence of values of @code{random} is actually
1006 the same in each Emacs run! For example, in one operating system, the
1007 first call to @code{(random)} after you start Emacs always returns
1008 -1457731, and the second one always returns -7692030. This
1009 repeatability is helpful for debugging.
1011 If you want truly unpredictable random numbers, execute @code{(random
1012 t)}. This chooses a new seed based on the current time of day and on
1013 Emacs's process @sc{id} number.
1015 @defun random &optional limit
1016 This function returns a pseudo-random integer. Repeated calls return a
1017 series of pseudo-random integers.
1019 If @var{limit} is @code{nil}, then the value may in principle be any
1020 integer. If @var{limit} is a positive integer, the value is chosen to
1021 be nonnegative and less than @var{limit} (only in Emacs 19).
1023 If @var{limit} is @code{t}, it means to choose a new seed based on the
1024 current time of day and on Emacs's process @sc{id} number.
1025 @c "Emacs'" is incorrect usage!
1027 On some machines, any integer representable in Lisp may be the result
1028 of @code{random}. On other machines, the result can never be larger
1029 than a certain maximum or less than a certain (negative) minimum.