Numbers
*******

GNU Emacs supports two numeric data types: integers and floating-point numbers.  Integers are whole numbers such as -3, 0, 7, 13, and 511.  Floating-point numbers are numbers with fractional parts, such as -4.5, 0.0, and 2.71828.  They can also be expressed in exponential notation: 1.5e2 is the same as 150.0; here, e2 stands for ten to the second power, and that is multiplied by 1.5.  Integer computations are exact, though they may overflow.  Floating-point computations often involve rounding errors, as the numbers have a fixed amount of precision.

Integer Basics
==============

The range of values for an integer depends on the machine.  The minimum range is -536,870,912 to 536,870,911 (30 bits; i.e., -2**29 to 2**29 - 1), but many machines provide a wider range.  Many examples in this chapter assume the minimum integer width of 30 bits.

The Lisp reader reads an integer as a sequence of digits with optional initial sign and optional final period.  An integer that is out of the Emacs range is treated as a floating-point number.

      1               ; The integer 1.
      1.              ; The integer 1.
     +1               ; Also the integer 1.
     -1               ; The integer -1.
      9000000000000000000
                      ; The floating-point number 9e18.
      0               ; The integer 0.
     -0               ; The integer 0.

The syntax for integers in bases other than 10 uses # followed by a letter that specifies the radix: b for binary, o for octal, x for hex, or RADIXr to specify radix RADIX.  Case is not significant for the letter that specifies the radix.  Thus, #bINTEGER reads INTEGER in binary, and #RADIXrINTEGER reads INTEGER in radix RADIX.  Allowed values of RADIX run from 2 to 36.  For example:

     #b101100 => 44
     #o54 => 44
     #x2c => 44
     #24r1k => 44

To understand how various functions work on integers, especially the bitwise operators (see Bitwise Operations), it is often helpful to view the numbers in their binary form.

In 30-bit binary, the decimal integer 5 looks like this:

     0000...000101 (30 bits total)

(The ... stands for enough bits to fill out a 30-bit word; in this case, ... stands for twenty 0 bits.  Later examples also use the ... notation to make binary integers easier to read.)

The integer -1 looks like this:

     1111...111111 (30 bits total)

-1 is represented as 30 ones.  (This is called two's complement notation.)

Subtracting 4 from -1 returns the negative integer -5. In binary, the decimal integer 4 is 100.  Consequently, -5 looks like this:

     1111...111011 (30 bits total)

In this implementation, the largest 30-bit binary integer is 536,870,911 in decimal.  In binary, it looks like this:

     0111...111111 (30 bits total)

Since the arithmetic functions do not check whether integers go outside their range, when you add 1 to 536,870,911, the value is the negative integer -536,870,912:

     (+ 1 536870911)
          => -536870912
          => 1000...000000 (30 bits total)

Many of the functions described in this chapter accept markers for arguments in place of numbers.  (see Markers.)  Since the actual arguments to such functions may be either numbers or markers, we often give these arguments the name NUMBER-OR-MARKER.  When the argument value is a marker, its position value is used and its buffer is ignored.

 -- Variable: most-positive-fixnum
     The value of this variable is the largest integer that Emacs Lisp can handle.  Typical values are 2**29 - 1 on 32-bit and 2**61 - 1 on 64-bit platforms.

 -- Variable: most-negative-fixnum
     The value of this variable is the smallest integer that Emacs Lisp can handle.  It is negative.  Typical values are -2**29 on 32-bit and -2**61 on 64-bit platforms.

In Emacs Lisp, text characters are represented by integers.  Any integer between zero and the value of (max-char), inclusive, is considered to be valid as a character.  See Character Codes.

Floating-Point Basics
=====================

Floating-point numbers are useful for representing numbers that are not integral.  The range of floating-point numbers is the same as the range of the C data type double on the machine you are using.  On all computers currently supported by Emacs, this is double-precision IEEE floating point.

The read syntax for floating-point numbers requires either a decimal point, an exponent, or both.  Optional signs (+ or -) precede the number and its exponent.  For example, 1500.0, +15e2, 15.0e+2, +1500000e-3, and .15e4 are five ways of writing a floating-point number whose value is 1500. They are all equivalent.  Like Common Lisp, Emacs Lisp requires at least one digit after any decimal point in a floating-point number; 1500. is an integer, not a floating-point number.

Emacs Lisp treats -0.0 as numerically equal to ordinary zero with respect to equal and =.  This follows the IEEE floating-point standard, which says -0.0 and 0.0 are numerically equal even though other operations can distinguish them.

The IEEE floating-point standard supports positive infinity and negative infinity as floating-point values.  It also provides for a class of values called NaN or ``not-a-number''; numerical functions return such values in cases where there is no correct answer.  For example, (/ 0.0 0.0) returns a NaN. Although NaN values carry a sign, for practical purposes there is no other significant difference between different NaN values in Emacs Lisp.

Here are read syntaxes for these special floating-point values:

infinity
     1.0e+INF and -1.0e+INF

not-a-number
     0.0e+NaN and -0.0e+NaN

The following functions are specialized for handling floating-point numbers:

 -- Function: isnan x
     This predicate returns t if its floating-point argument is a NaN, nil otherwise.

 -- Function: frexp x
     This function returns a cons cell (S . E), where S and E are respectively the significand and exponent of the floating-point number X.

     If X is finite, then S is a floating-point number between 0.5 (inclusive) and 1.0 (exclusive), E is an integer, and X = S * 2**E. If X is zero or infinity, then S is the same as X. If X is a NaN, then S is also a NaN. If X is zero, then E is 0.

 -- Function: ldexp sig &optional exp
     This function returns a floating-point number corresponding to the significand SIG and exponent EXP.

 -- Function: copysign x1 x2
     This function copies the sign of X2 to the value of X1, and returns the result.  X1 and X2 must be floating point.

 -- Function: logb x
     This function returns the binary exponent of X.  More precisely, the value is the logarithm base 2 of |x|, rounded down to an integer.

          (logb 10)
               => 3
          (logb 10.0e20)
               => 69

Type Predicates for Numbers
===========================

The functions in this section test for numbers, or for a specific type of number.  The functions integerp and floatp can take any type of Lisp object as argument (they would not be of much use otherwise), but the zerop predicate requires a number as its argument.  See also integer-or-marker-p and number-or-marker-p, in Predicates on Markers.

 -- Function: floatp object
     This predicate tests whether its argument is floating point and returns t if so, nil otherwise.

 -- Function: integerp object
     This predicate tests whether its argument is an integer, and returns t if so, nil otherwise.

 -- Function: numberp object
     This predicate tests whether its argument is a number (either integer or floating point), and returns t if so, nil otherwise.

 -- Function: natnump object
     This predicate (whose name comes from the phrase ``natural number'') tests to see whether its argument is a nonnegative integer, and returns t if so, nil otherwise.  0 is considered non-negative.

     wholenump is a synonym for natnump.

 -- Function: zerop number
     This predicate tests whether its argument is zero, and returns t if so, nil otherwise.  The argument must be a number.

     (zerop x) is equivalent to (= x 0).

Comparison of Numbers
=====================

To test numbers for numerical equality, you should normally use =, not eq.  There can be many distinct floating-point objects with the same numeric value.  If you use eq to compare them, then you test whether two values are the same object.  By contrast, = compares only the numeric values of the objects.

In Emacs Lisp, each integer is a unique Lisp object. Therefore, eq is equivalent to = where integers are concerned.  It is sometimes convenient to use eq for comparing an unknown value with an integer, because eq does not report an error if the unknown value is not a number---it accepts arguments of any type.  By contrast, = signals an error if the arguments are not numbers or markers.  However, it is better programming practice to use = if you can, even for comparing integers.

Sometimes it is useful to compare numbers with equal, which treats two numbers as equal if they have the same data type (both integers, or both floating point) and the same value.  By contrast, = can treat an integer and a floating-point number as equal. See Equality Predicates.

There is another wrinkle: because floating-point arithmetic is not exact, it is often a bad idea to check for equality of floating-point values.  Usually it is better to test for approximate equality. Here's a function to do this:

     (defvar fuzz-factor 1.0e-6)
     (defun approx-equal (x y)
       (or (= x y)
           (< (/ (abs (- x y))
                 (max (abs x) (abs y)))
              fuzz-factor)))

     Common Lisp note: Comparing numbers in Common Lisp always requires = because Common Lisp implements multi-word integers, and two distinct integer objects can have the same numeric value.  Emacs Lisp can have just one integer object for any given value because it has a limited range of integers.

 -- Function: = number-or-marker &rest number-or-markers
     This function tests whether all its arguments are numerically equal, and returns t if so, nil otherwise.

 -- Function: eql value1 value2
     This function acts like eq except when both arguments are numbers.  It compares numbers by type and numeric value, so that (eql 1.0 1) returns nil, but (eql 1.0 1.0) and (eql 1 1) both return t.

 -- Function: /= number-or-marker1 number-or-marker2
     This function tests whether its arguments are numerically equal, and returns t if they are not, and nil if they are.

 -- Function: < number-or-marker &rest number-or-markers
     This function tests whether each argument is strictly less than the following argument.  It returns t if so, nil otherwise.

 -- Function: <= number-or-marker &rest number-or-markers
     This function tests whether each argument is less than or equal to the following argument.  It returns t if so, nil otherwise.

 -- Function: > number-or-marker &rest number-or-markers
     This function tests whether each argument is strictly greater than the following argument.  It returns t if so, nil otherwise.

 -- Function: >= number-or-marker &rest number-or-markers
     This function tests whether each argument is greater than or equal to the following argument.  It returns t if so, nil otherwise.

 -- Function: max number-or-marker &rest numbers-or-markers
     This function returns the largest of its arguments. If any of the arguments is floating point, the value is returned as floating point, even if it was given as an integer.

          (max 20)
               => 20
          (max 1 2.5)
               => 2.5
          (max 1 3 2.5)
               => 3.0

 -- Function: min number-or-marker &rest numbers-or-markers
     This function returns the smallest of its arguments. If any of the arguments is floating point, the value is returned as floating point, even if it was given as an integer.

          (min -4 1)
               => -4

 -- Function: abs number
     This function returns the absolute value of NUMBER.

Numeric Conversions
===================

To convert an integer to floating point, use the function float.

 -- Function: float number
     This returns NUMBER converted to floating point. If NUMBER is already floating point, float returns it unchanged.

There are four functions to convert floating-point numbers to integers; they differ in how they round.  All accept an argument NUMBER and an optional argument DIVISOR.  Both arguments may be integers or floating-point numbers.  DIVISOR may also be nil.  If DIVISOR is nil or omitted, these functions convert NUMBER to an integer, or return it unchanged if it already is an integer.  If DIVISOR is non-nil, they divide NUMBER by DIVISOR and convert the result to an integer.  If DIVISOR is zero (whether integer or floating point), Emacs signals an arith-error error.

 -- Function: truncate number &optional divisor
     This returns NUMBER, converted to an integer by rounding towards zero.

          (truncate 1.2)
               => 1
          (truncate 1.7)
               => 1
          (truncate -1.2)
               => -1
          (truncate -1.7)
               => -1

 -- Function: floor number &optional divisor
     This returns NUMBER, converted to an integer by rounding downward (towards negative infinity).

     If DIVISOR is specified, this uses the kind of division operation that corresponds to mod, rounding downward.

          (floor 1.2)
               => 1
          (floor 1.7)
               => 1
          (floor -1.2)
               => -2
          (floor -1.7)
               => -2
          (floor 5.99 3)
               => 1

 -- Function: ceiling number &optional divisor
     This returns NUMBER, converted to an integer by rounding upward (towards positive infinity).

          (ceiling 1.2)
               => 2
          (ceiling 1.7)
               => 2
          (ceiling -1.2)
               => -1
          (ceiling -1.7)
               => -1

 -- Function: round number &optional divisor
     This returns NUMBER, converted to an integer by rounding towards the nearest integer.  Rounding a value equidistant between two integers returns the even integer.

          (round 1.2)
               => 1
          (round 1.7)
               => 2
          (round -1.2)
               => -1
          (round -1.7)
               => -2

Arithmetic Operations
=====================

Emacs Lisp provides the traditional four arithmetic operations (addition, subtraction, multiplication, and division), as well as remainder and modulus functions, and functions to add or subtract 1. Except for %, each of these functions accepts both integer and floating-point arguments, and returns a floating-point number if any argument is floating point.

Emacs Lisp arithmetic functions do not check for integer overflow. Thus (1+ 536870911) may evaluate to -536870912, depending on your hardware.

 -- Function: 1+ number-or-marker
     This function returns NUMBER-OR-MARKER plus 1. For example,

          (setq foo 4)
               => 4
          (1+ foo)
               => 5

     This function is not analogous to the C operator ++---it does not increment a variable.  It just computes a sum.  Thus, if we continue,

          foo
               => 4

     If you want to increment the variable, you must use setq, like this:

          (setq foo (1+ foo))
               => 5

 -- Function: 1- number-or-marker
     This function returns NUMBER-OR-MARKER minus 1.

 -- Function: + &rest numbers-or-markers
     This function adds its arguments together.  When given no arguments, + returns 0.

          (+)
               => 0
          (+ 1)
               => 1
          (+ 1 2 3 4)
               => 10

 -- Function: - &optional number-or-marker &rest more-numbers-or-markers
     The - function serves two purposes: negation and subtraction. When - has a single argument, the value is the negative of the argument.  When there are multiple arguments, - subtracts each of the MORE-NUMBERS-OR-MARKERS from NUMBER-OR-MARKER, cumulatively.  If there are no arguments, the result is 0.

          (- 10 1 2 3 4)
               => 0
          (- 10)
               => -10
          (-)
               => 0

 -- Function: * &rest numbers-or-markers
     This function multiplies its arguments together, and returns the product.  When given no arguments, * returns 1.

          (*)
               => 1
          (* 1)
               => 1
          (* 1 2 3 4)
               => 24

 -- Function: / dividend divisor &rest divisors
     This function divides DIVIDEND by DIVISOR and returns the quotient.  If there are additional arguments DIVISORS, then it divides DIVIDEND by each divisor in turn.  Each argument may be a number or a marker.

     If all the arguments are integers, the result is an integer, obtained by rounding the quotient towards zero after each division.

          (/ 6 2)
               => 3

          (/ 5 2)
               => 2

          (/ 5.0 2)
               => 2.5

          (/ 5 2.0)
               => 2.5

          (/ 5.0 2.0)
               => 2.5

          (/ 25 3 2)
               => 4

          (/ -17 6)
               => -2

     If you divide an integer by the integer 0, Emacs signals an arith-error error (see Errors).  Floating-point division of a nonzero number by zero yields either positive or negative infinity (see Float Basics).

 -- Function: % dividend divisor
     This function returns the integer remainder after division of DIVIDEND by DIVISOR.  The arguments must be integers or markers.

     For any two integers DIVIDEND and DIVISOR,

          (+ (% DIVIDEND DIVISOR)
             (* (/ DIVIDEND DIVISOR) DIVISOR))

     always equals DIVIDEND if DIVISOR is nonzero.

          (% 9 4)
               => 1
          (% -9 4)
               => -1
          (% 9 -4)
               => 1
          (% -9 -4)
               => -1

 -- Function: mod dividend divisor
     This function returns the value of DIVIDEND modulo DIVISOR; in other words, the remainder after division of DIVIDEND by DIVISOR, but with the same sign as DIVISOR. The arguments must be numbers or markers.

     Unlike %, mod permits floating-point arguments; it rounds the quotient downward (towards minus infinity) to an integer, and uses that quotient to compute the remainder.

     If DIVISOR is zero, mod signals an arith-error error if both arguments are integers, and returns a NaN otherwise.

          (mod 9 4)
               => 1

          (mod -9 4)
               => 3

          (mod 9 -4)
               => -3

          (mod -9 -4)
               => -1

          (mod 5.5 2.5)
               => .5

     For any two numbers DIVIDEND and DIVISOR,

          (+ (mod DIVIDEND DIVISOR)
             (* (floor DIVIDEND DIVISOR) DIVISOR))

     always equals DIVIDEND, subject to rounding error if either argument is floating point and to an arith-error if DIVIDEND is an integer and DIVISOR is 0.  For floor, see Numeric Conversions.

Rounding Operations
===================

The functions ffloor, fceiling, fround, and ftruncate take a floating-point argument and return a floating-point result whose value is a nearby integer.  ffloor returns the nearest integer below; fceiling, the nearest integer above; ftruncate, the nearest integer in the direction towards zero; fround, the nearest integer.

 -- Function: ffloor float
     This function rounds FLOAT to the next lower integral value, and returns that value as a floating-point number.

 -- Function: fceiling float
     This function rounds FLOAT to the next higher integral value, and returns that value as a floating-point number.

 -- Function: ftruncate float
     This function rounds FLOAT towards zero to an integral value, and returns that value as a floating-point number.

 -- Function: fround float
     This function rounds FLOAT to the nearest integral value, and returns that value as a floating-point number. Rounding a value equidistant between two integers returns the even integer.

Bitwise Operations on Integers
==============================

In a computer, an integer is represented as a binary number, a sequence of bits (digits which are either zero or one).  A bitwise operation acts on the individual bits of such a sequence.  For example, shifting moves the whole sequence left or right one or more places, reproducing the same pattern ``moved over''.

The bitwise operations in Emacs Lisp apply only to integers.

 -- Function: lsh integer1 count
     lsh, which is an abbreviation for logical shift, shifts the bits in INTEGER1 to the left COUNT places, or to the right if COUNT is negative, bringing zeros into the vacated bits.  If COUNT is negative, lsh shifts zeros into the leftmost (most-significant) bit, producing a positive result even if INTEGER1 is negative.  Contrast this with ash, below.

     Here are two examples of lsh, shifting a pattern of bits one place to the left.  We show only the low-order eight bits of the binary pattern; the rest are all zero.

          (lsh 5 1)
               => 10
          ;; Decimal 5 becomes decimal 10.
          00000101 => 00001010

          (lsh 7 1)
               => 14
          ;; Decimal 7 becomes decimal 14.
          00000111 => 00001110

     As the examples illustrate, shifting the pattern of bits one place to the left produces a number that is twice the value of the previous number.

     Shifting a pattern of bits two places to the left produces results like this (with 8-bit binary numbers):

          (lsh 3 2)
               => 12
          ;; Decimal 3 becomes decimal 12.
          00000011 => 00001100

     On the other hand, shifting one place to the right looks like this:

          (lsh 6 -1)
               => 3
          ;; Decimal 6 becomes decimal 3.
          00000110 => 00000011

          (lsh 5 -1)
               => 2
          ;; Decimal 5 becomes decimal 2.
          00000101 => 00000010

     As the example illustrates, shifting one place to the right divides the value of a positive integer by two, rounding downward.

     The function lsh, like all Emacs Lisp arithmetic functions, does not check for overflow, so shifting left can discard significant bits and change the sign of the number.  For example, left shifting 536,870,911 produces -2 in the 30-bit implementation:

          (lsh 536870911 1)          ; left shift
               => -2

     In binary, the argument looks like this:

          ;; Decimal 536,870,911
          0111...111111 (30 bits total)

     which becomes the following when left shifted:

          ;; Decimal -2
          1111...111110 (30 bits total)

 -- Function: ash integer1 count
     ash (arithmetic shift) shifts the bits in INTEGER1 to the left COUNT places, or to the right if COUNT is negative.

     ash gives the same results as lsh except when INTEGER1 and COUNT are both negative.  In that case, ash puts ones in the empty bit positions on the left, while lsh puts zeros in those bit positions.

     Thus, with ash, shifting the pattern of bits one place to the right looks like this:

          (ash -6 -1) => -3
          ;; Decimal -6 becomes decimal -3.
          1111...111010 (30 bits total)
               =>
          1111...111101 (30 bits total)

     In contrast, shifting the pattern of bits one place to the right with lsh looks like this:

          (lsh -6 -1) => 536870909
          ;; Decimal -6 becomes decimal 536,870,909.
          1111...111010 (30 bits total)
               =>
          0111...111101 (30 bits total)

     Here are other examples:

                             ;         30-bit binary values

          (lsh 5 2)          ;   5  =  0000...000101
               => 20         ;      =  0000...010100

          (ash 5 2)
               => 20
          (lsh -5 2)         ;  -5  =  1111...111011
               => -20        ;      =  1111...101100
          (ash -5 2)
               => -20

          (lsh 5 -2)         ;   5  =  0000...000101
               => 1          ;      =  0000...000001

          (ash 5 -2)
               => 1

          (lsh -5 -2)        ;  -5  =  1111...111011
               => 268435454
                             ;      =  0011...111110

          (ash -5 -2)        ;  -5  =  1111...111011
               => -2         ;      =  1111...111110

 -- Function: logand &rest ints-or-markers
     This function returns the ``logical and'' of the arguments: the Nth bit is set in the result if, and only if, the Nth bit is set in all the arguments.  (``Set'' means that the value of the bit is 1 rather than 0.)

     For example, using 4-bit binary numbers, the ``logical and'' of 13 and 12 is 12: 1101 combined with 1100 produces 1100. In both the binary numbers, the leftmost two bits are set (i.e., they are 1's), so the leftmost two bits of the returned value are set. However, for the rightmost two bits, each is zero in at least one of the arguments, so the rightmost two bits of the returned value are 0's.

     Therefore,

          (logand 13 12)
               => 12

     If logand is not passed any argument, it returns a value of -1.  This number is an identity element for logand because its binary representation consists entirely of ones.  If logand is passed just one argument, it returns that argument.

                             ;        30-bit binary values

          (logand 14 13)     ; 14  =  0000...001110
                             ; 13  =  0000...001101
               => 12         ; 12  =  0000...001100

          (logand 14 13 4)   ; 14  =  0000...001110
                             ; 13  =  0000...001101
                             ;  4  =  0000...000100
               => 4          ;  4  =  0000...000100

          (logand)
               => -1         ; -1  =  1111...111111

 -- Function: logior &rest ints-or-markers
     This function returns the ``inclusive or'' of its arguments: the Nth bit is set in the result if, and only if, the Nth bit is set in at least one of the arguments.  If there are no arguments, the result is zero, which is an identity element for this operation.  If logior is passed just one argument, it returns that argument.

                             ;        30-bit binary values

          (logior 12 5)      ; 12  =  0000...001100
                             ;  5  =  0000...000101
               => 13         ; 13  =  0000...001101

          (logior 12 5 7)    ; 12  =  0000...001100
                             ;  5  =  0000...000101
                             ;  7  =  0000...000111
               => 15         ; 15  =  0000...001111

 -- Function: logxor &rest ints-or-markers
     This function returns the ``exclusive or'' of its arguments: the Nth bit is set in the result if, and only if, the Nth bit is set in an odd number of the arguments.  If there are no arguments, the result is 0, which is an identity element for this operation.  If logxor is passed just one argument, it returns that argument.

                             ;        30-bit binary values

          (logxor 12 5)      ; 12  =  0000...001100
                             ;  5  =  0000...000101
               => 9          ;  9  =  0000...001001

          (logxor 12 5 7)    ; 12  =  0000...001100
                             ;  5  =  0000...000101
                             ;  7  =  0000...000111
               => 14         ; 14  =  0000...001110

 -- Function: lognot integer
     This function returns the logical complement of its argument: the Nth bit is one in the result if, and only if, the Nth bit is zero in INTEGER, and vice-versa.

          (lognot 5)
               => -6
          ;;  5  =  0000...000101 (30 bits total)
          ;; becomes
          ;; -6  =  1111...111010 (30 bits total)

Standard Mathematical Functions
===============================

These mathematical functions allow integers as well as floating-point numbers as arguments.

 -- Function: sin arg
 -- Function: cos arg
 -- Function: tan arg
     These are the basic trigonometric functions, with argument ARG measured in radians.

 -- Function: asin arg
     The value of (asin ARG) is a number between -pi/2 and pi/2 (inclusive) whose sine is ARG.  If ARG is out of range (outside [-1, 1]), asin returns a NaN.

 -- Function: acos arg
     The value of (acos ARG) is a number between 0 and pi (inclusive) whose cosine is ARG.  If ARG is out of range (outside [-1, 1]), acos returns a NaN.

 -- Function: atan y &optional x
     The value of (atan Y) is a number between -pi/2 and pi/2 (exclusive) whose tangent is Y.  If the optional second argument X is given, the value of (atan y x) is the angle in radians between the vector [X, Y] and the X axis.

 -- Function: exp arg
     This is the exponential function; it returns e to the power ARG.

 -- Function: log arg &optional base
     This function returns the logarithm of ARG, with base BASE.  If you don't specify BASE, the natural base e is used.  If ARG or BASE is negative, log returns a NaN.

 -- Function: expt x y
     This function returns X raised to power Y.  If both arguments are integers and Y is positive, the result is an integer; in this case, overflow causes truncation, so watch out. If X is a finite negative number and Y is a finite non-integer, expt returns a NaN.

 -- Function: sqrt arg
     This returns the square root of ARG.  If ARG is finite and less than zero, sqrt returns a NaN.

In addition, Emacs defines the following common mathematical constants:

 -- Variable: float-e
     The mathematical constant e (2.71828...).

 -- Variable: float-pi
     The mathematical constant pi (3.14159...).

Random Numbers
==============

A deterministic computer program cannot generate true random numbers.  For most purposes, pseudo-random numbers suffice.  A series of pseudo-random numbers is generated in a deterministic fashion.  The numbers are not truly random, but they have certain properties that mimic a random series.  For example, all possible values occur equally often in a pseudo-random series.

Pseudo-random numbers are generated from a ``seed''.  Starting from any given seed, the random function always generates the same sequence of numbers.  By default, Emacs initializes the random seed at startup, in such a way that the sequence of values of random (with overwhelming likelihood) differs in each Emacs run.

Sometimes you want the random number sequence to be repeatable.  For example, when debugging a program whose behavior depends on the random number sequence, it is helpful to get the same behavior in each program run.  To make the sequence repeat, execute (random ""). This sets the seed to a constant value for your particular Emacs executable (though it may differ for other Emacs builds).  You can use other strings to choose various seed values.

 -- Function: random &optional limit
     This function returns a pseudo-random integer.  Repeated calls return a series of pseudo-random integers.

     If LIMIT is a positive integer, the value is chosen to be nonnegative and less than LIMIT.  Otherwise, the value might be any integer representable in Lisp, i.e., an integer between most-negative-fixnum and most-positive-fixnum (see Integer Basics).

     If LIMIT is t, it means to choose a new seed as if Emacs were restarting.

     If LIMIT is a string, it means to choose a new seed based on the string's contents.