[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9. Numbers

SXEmacs supports two numeric data types: integers and floating point numbers. Integers are whole numbers such as -3, 0, #b0111, #xFEED, #o744. Their values are exact. The number prefixes `#b', `#o', and `#x' are supported to represent numbers in binary, octal, and hexadecimal notation (or radix). Floating point numbers are numbers with fractional parts, such as -4.5, 0.0, or 2.71828. They can also be expressed in exponential notation: 1.5e2 equals 150; in this example, `e2' stands for ten to the second power, and is multiplied by 1.5. Floating point values are not exact; they have a fixed, limited amount of precision.

SXEmacs can be compiled to use a multi-precision arithmetic library with the -enable-ent switch. Currently the MP-libraries GMP, BSD-MP, MPFR and MPC are supported. We have a dedicated section for this case, See section Enhanced Number Types (ENT).


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.1 Integer Basics

The range of values for an integer depends on the machine. The minimum range is -134217728 to 134217727 (28 bits; i.e., to but some machines may provide a wider range. Many examples in this chapter assume an integer has 28 bits.

The Lisp reader reads an integer as a sequence of digits with optional initial sign and optional final period.

 
 1               ; The integer 1.
 1.              ; The integer 1.
+1               ; Also the integer 1.
-1               ; The integer -1.
 268435457       ; Also the integer 1, due to overflow.
 0               ; The integer 0.
-0               ; The integer 0.

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

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

 
0000  0000 0000  0000 0000  0000 0101

(We have inserted spaces between groups of 4 bits, and two spaces between groups of 8 bits, to make the binary integer easier to read.)

The integer -1 looks like this:

 
1111  1111 1111  1111 1111  1111 1111

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

The negative integer, -5, is creating by subtracting 4 from -1. In binary, the decimal integer 4 is 100. Consequently, -5 looks like this:

 
1111  1111 1111  1111 1111  1111 1011

In this implementation, the largest 28-bit binary integer is the decimal integer 134,217,727. In binary, it looks like this:

 
0111  1111 1111  1111 1111  1111 1111

Since the arithmetic functions do not check whether integers go outside their range, when you add 1 to 134,217,727, the value is the negative integer -134,217,728:

 
(+ 1 134217727)
     ⇒ -134217728
     ⇒ 1000  0000 0000  0000 0000  0000 0000

Many of the following functions accept markers for arguments as well as integers. (See section Markers.) More precisely, the actual arguments to such functions may be either integers or markers, which is why we often give these arguments the name int-or-marker. When the argument value is a marker, its position value is used and its buffer is ignored.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.2 Floating Point Basics

SXEmacs supports floating point numbers. The precise range of floating point numbers is machine-specific; it is the same as the range of the C data type double on the machine in question.

The printed representation for floating point numbers requires either a decimal point (with at least one digit following), an exponent, or both. For example, `1500.0', `15e2', `15.0e2', `1.5e3', and `.15e4' are five ways of writing a floating point number whose value is 1500. They are all equivalent. You can also use a minus sign to write negative floating point numbers, as in `-1.0'.

Please bear in mind that floating point numbers have a limited and fixed precision although the printed output may suggest something else. The precision varies (depending on the machine) between 12 and 38 digits. Also note, that internally numbers are processed in a 2-adic arithmetic, hence some real numbers cannot be represented precisely and are rounded to the next precisely representable float instead.

Most modern computers support the IEEE floating point standard, which provides for 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, (sqrt -1.0) returns a NaN. For practical purposes, there's no significant difference between different NaN values in SXEmacs Lisp, and there's no rule for precisely which NaN value should be used in a particular case, so this manual doesn't try to distinguish them. SXEmacs Lisp has no read syntax for NaNs or infinities; perhaps we should create a syntax in the future.

You can use logb to extract the binary exponent of a floating point number (or estimate the logarithm of an integer):

Function: logb number

This function returns the binary exponent of number. More precisely, the value is the logarithm of number base 2, rounded down to an integer.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.3 Type Predicates for Numbers

The functions in this section test whether the argument is a number or whether it is a certain sort of number. The functions integerp and floatp can take any type of Lisp object as argument (the predicates would not be of much use otherwise); but the zerop predicate requires a number as its argument. See also integer-or-marker-p, integer-char-or-marker-p, number-or-marker-p and number-char-or-marker-p, in Predicates on Markers.

Function: floatp object

This predicate tests whether its argument is a floating point number and returns t if so, nil otherwise.

floatp does not exist in Emacs versions 18 and earlier.

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

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

Function: zerop number

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

These two forms are equivalent: (zerop x)(= x 0).


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.4 Comparison of Numbers

To test numbers for numerical equality, you should normally use =, not eq. There can be many distinct floating point number 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.

At present, each integer value has a unique Lisp object in SXEmacs Lisp. 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 a good idea to use = if you can, even for comparing integers, just in case we change the representation of integers in a future SXEmacs version.

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

 
(defconst fuzz-factor 1.0e-6)
(defun approx-equal (x y)
  (or (and (= x 0) (= y 0))
      (< (/ (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. SXEmacs Lisp can have just one integer object for any given value because it has a limited range of integer values.

In addition to numbers, all of the following functions also accept characters and markers as arguments, and treat them as their number equivalents.

Function: = number &rest more-numbers

This function returns t if all of its arguments are numerically equal, nil otherwise.

 
(= 5)
     ⇒ t
(= 5 6)
     ⇒ nil
(= 5 5.0)
     ⇒ t
(= 5 5 6)
     ⇒ nil
Function: /= number &rest more-numbers

This function returns t if no two arguments are numerically equal, nil otherwise.

 
(/= 5 6)
     ⇒ t
(/= 5 5 6)
     ⇒ nil
(/= 5 6 1)
     ⇒ t
Function: < number &rest more-numbers

This function returns t if the sequence of its arguments is monotonically increasing, nil otherwise.

 
(< 5 6)
     ⇒ t
(< 5 6 6)
     ⇒ nil
(< 5 6 7)
     ⇒ t
Function: <= number &rest more-numbers

This function returns t if the sequence of its arguments is monotonically nondecreasing, nil otherwise.

 
(<= 5 6)
     ⇒ t
(<= 5 6 6)
     ⇒ t
(<= 5 6 5)
     ⇒ nil
Function: > number &rest more-numbers

This function returns t if the sequence of its arguments is monotonically decreasing, nil otherwise.

Function: >= number &rest more-numbers

This function returns t if the sequence of its arguments is monotonically nonincreasing, nil otherwise.

Function: max number &rest more-numbers

This function returns the largest of its arguments.

 
(max 20)
     ⇒ 20
(max 1 2.5)
     ⇒ 2.5
(max 1 3 2.5)
     ⇒ 3
Function: min number &rest more-numbers

This function returns the smallest of its arguments.

 
(min -4 1)
     ⇒ -4

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.5 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 a floating point number, float returns it unchanged.

There are four functions to convert floating point numbers to integers; they differ in how they round. These functions accept integer arguments also, and return such arguments unchanged.

Function: truncate number

This returns number, converted to an integer by rounding towards zero.

Function: floor number &optional divisor

This returns number, converted to an integer by rounding downward (towards negative infinity).

If divisor is specified, number is divided by divisor before the floor is taken; this is the division operation that corresponds to mod. An arith-error results if divisor is 0.

Function: ceiling number

This returns number, converted to an integer by rounding upward (towards positive infinity).

Function: round number

This returns number, converted to an integer by rounding towards the nearest integer. Rounding a value equidistant between two integers may choose the integer closer to zero, or it may prefer an even integer, depending on your machine.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.6 Arithmetic Operations

SXEmacs Lisp provides the traditional four arithmetic operations: addition, subtraction, multiplication, and division. Remainder and modulus functions supplement the division functions. The functions to add or subtract 1 are provided because they are traditional in Lisp and commonly used.

All of these functions except % return a floating point value if any argument is floating.

It is important to note that in SXEmacs Lisp, arithmetic functions do not check for overflow. Thus (1+ 134217727) may evaluate to -134217728, depending on your hardware.

Function: 1+ number

This function returns number plus one. number may be a number, character or marker. Markers and characters are converted to integers.

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

Now that the cl package is always available from lisp code, a more convenient and natural way to increment a variable is (incf foo).

Function: 1- number

This function returns number minus one. number may be a number, character or marker. Markers and characters are converted to integers.

Function: abs number

This returns the absolute value of number.

Function: + &rest numbers

This function adds its arguments together. When given no arguments, + returns 0.

If any of the arguments are characters or markers, they are first converted to integers.

 
(+)
     ⇒ 0
(+ 1)
     ⇒ 1
(+ 1 2 3 4)
     ⇒ 10
Function: - &optional number &rest other-numbers

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 other-numbers from number, cumulatively. If there are no arguments, an error is signaled.

If any of the arguments are characters or markers, they are first converted to integers.

 
(- 10 1 2 3 4)
     ⇒ 0
(- 10)
     ⇒ -10
(-)
     ⇒ 0
Function: * &rest numbers

This function multiplies its arguments together, and returns the product. When given no arguments, * returns 1.

If any of the arguments are characters or markers, they are first converted to integers.

 
(*)
     ⇒ 1
(* 1)
     ⇒ 1
(* 1 2 3 4)
     ⇒ 24
Function: / dividend &rest divisors

The / function serves two purposes: inversion and division. When / has a single argument, the value is the inverse of the argument. When there are multiple arguments, / divides dividend by each of the divisors, cumulatively, returning the quotient. If there are no arguments, an error is signaled.

If none of the arguments are floats, then the result is an integer. This means the result has to be rounded. On most machines, the result is rounded towards zero after each division, but some machines may round differently with negative arguments. This is because the Lisp function / is implemented using the C division operator, which also permits machine-dependent rounding. As a practical matter, all known machines round in the standard fashion.

If any of the arguments are characters or markers, they are first converted to integers.

If you divide by 0, an arith-error error is signaled. (See section Errors.)

 
(/ 6 2)
     ⇒ 3
(/ 5 2)
     ⇒ 2
(/ 25 3 2)
     ⇒ 4
(/ 3.0)
     ⇒ 0.3333333333333333
(/ -17 6)
     ⇒ -2

The result of (/ -17 6) could in principle be -3 on some machines.

Function: % dividend divisor

This function returns the integer remainder after division of dividend by divisor. The arguments must be integers or markers.

For negative arguments, the remainder is in principle machine-dependent since the quotient is; but in practice, all known machines behave alike.

An arith-error results if divisor is 0.

 
(% 9 4)
     ⇒ 1
(% -9 4)
     ⇒ -1
(% 9 -4)
     ⇒ 1
(% -9 -4)
     ⇒ -1

For any two integers dividend and divisor,

 
(+ (% dividend divisor)
   (* (/ dividend divisor) divisor))

always equals dividend.

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 returns a well-defined result for negative arguments. It also permits floating point arguments; it rounds the quotient downward (towards minus infinity) to an integer, and uses that quotient to compute the remainder.

An arith-error results if divisor is 0.

 
(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. For floor, see Numeric Conversions.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.7 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 number

This function rounds number to the next lower integral value, and returns that value as a floating point number.

Function: fceiling number

This function rounds number to the next higher integral value, and returns that value as a floating point number.

Function: ftruncate number

This function rounds number towards zero to an integral value, and returns that value as a floating point number.

Function: fround number

This function rounds number to the nearest integral value, and returns that value as a floating point number.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.8 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 SXEmacs 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 SXEmacs 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 134,217,727 produces -2 on a 28-bit machine:

 
(lsh 134217727 1)          ; left shift
     ⇒ -2

In binary, in the 28-bit implementation, the argument looks like this:

 
;; Decimal 134,217,727
0111  1111 1111  1111 1111  1111 1111

which becomes the following when left shifted:

 
;; Decimal -2
1111  1111 1111  1111 1111  1111 1110
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  1111 1111  1111 1111  1111 1010
     ⇒
1111  1111 1111  1111 1111  1111 1101

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

 
(lsh -6 -1) ⇒ 134217725
;; Decimal -6 becomes decimal 134,217,725.
1111  1111 1111  1111 1111  1111 1010
     ⇒
0111  1111 1111  1111 1111  1111 1101

Here are other examples:

 
                   ;               28-bit binary values

(lsh 5 2)          ;   5  =  0000  0000 0000  0000 0000  0000 0101
     ⇒ 20         ;      =  0000  0000 0000  0000 0000  0001 0100
(ash 5 2)
     ⇒ 20
(lsh -5 2)         ;  -5  =  1111  1111 1111  1111 1111  1111 1011
     ⇒ -20        ;      =  1111  1111 1111  1111 1111  1110 1100
(ash -5 2)
     ⇒ -20
(lsh 5 -2)         ;   5  =  0000  0000 0000  0000 0000  0000 0101
     ⇒ 1          ;      =  0000  0000 0000  0000 0000  0000 0001
(ash 5 -2)
     ⇒ 1
(lsh -5 -2)        ;  -5  =  1111  1111 1111  1111 1111  1111 1011
     ⇒ 4194302    ;      =  0011  1111 1111  1111 1111  1111 1110
(ash -5 -2)        ;  -5  =  1111  1111 1111  1111 1111  1111 1011
     ⇒ -2         ;      =  1111  1111 1111  1111 1111  1111 1110
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.

 
                   ;                28-bit binary values

(logand 14 13)     ; 14  =  0000  0000 0000  0000 0000  0000 1110
                   ; 13  =  0000  0000 0000  0000 0000  0000 1101
     ⇒ 12         ; 12  =  0000  0000 0000  0000 0000  0000 1100

(logand 14 13 4)   ; 14  =  0000  0000 0000  0000 0000  0000 1110
                   ; 13  =  0000  0000 0000  0000 0000  0000 1101
                   ;  4  =  0000  0000 0000  0000 0000  0000 0100
     ⇒ 4          ;  4  =  0000  0000 0000  0000 0000  0000 0100

(logand)
     ⇒ -1         ; -1  =  1111  1111 1111  1111 1111  1111 1111
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.

 
                   ;               28-bit binary values

(logior 12 5)      ; 12  =  0000  0000 0000  0000 0000  0000 1100
                   ;  5  =  0000  0000 0000  0000 0000  0000 0101
     ⇒ 13         ; 13  =  0000  0000 0000  0000 0000  0000 1101

(logior 12 5 7)    ; 12  =  0000  0000 0000  0000 0000  0000 1100
                   ;  5  =  0000  0000 0000  0000 0000  0000 0101
                   ;  7  =  0000  0000 0000  0000 0000  0000 0111
     ⇒ 15         ; 15  =  0000  0000 0000  0000 0000  0000 1111
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.

 
                   ;               28-bit binary values

(logxor 12 5)      ; 12  =  0000  0000 0000  0000 0000  0000 1100
                   ;  5  =  0000  0000 0000  0000 0000  0000 0101
     ⇒ 9          ;  9  =  0000  0000 0000  0000 0000  0000 1001

(logxor 12 5 7)    ; 12  =  0000  0000 0000  0000 0000  0000 1100
                   ;  5  =  0000  0000 0000  0000 0000  0000 0101
                   ;  7  =  0000  0000 0000  0000 0000  0000 0111
     ⇒ 14         ; 14  =  0000  0000 0000  0000 0000  0000 1110
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  0000 0000  0000 0000  0000 0101
;; becomes
;; -6  =  1111  1111 1111  1111 1111  1111 1010

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.9 Standard Mathematical Functions

These mathematical functions are available if floating point is supported (which is the normal state of affairs). They allow integers as well as floating point numbers as arguments.

Function: sin number
Function: cos number
Function: tan number

These are the ordinary trigonometric functions, with argument measured in radians.

Function: asin number

The value of (asin number) is a number between -pi/2 and pi/2 (inclusive) whose sine is number; if, however, number is out of range (outside [-1, 1]), then the result is a NaN.

Function: acos number

The value of (acos number) is a number between 0 and pi (inclusive) whose cosine is number; if, however, number is out of range (outside [-1, 1]), then the result is a NaN.

Function: atan number &optional number2

The value of (atan number) is a number between -pi/2 and pi/2 (exclusive) whose tangent is number.

If optional argument number2 is supplied, the function returns atan2(number,number2).

Function: sinh number
Function: cosh number
Function: tanh number

These are the ordinary hyperbolic trigonometric functions.

Function: asinh number
Function: acosh number
Function: atanh number

These are the inverse hyperbolic trigonometric functions.

Function: exp number

This is the exponential function; it returns e to the power number. e is a fundamental mathematical constant also called the base of natural logarithms.

Function: log number &optional base

This function returns the logarithm of number, with base base. If you don't specify base, the base e is used. If number is negative, the result is a NaN.

Function: log10 number

This function returns the logarithm of number, with base 10. If number is negative, the result is a NaN. (log10 x)(log x 10), at least approximately.

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, it is truncated to fit the range of possible integer values.

Function: sqrt number

This returns the square root of number. If number is negative, the value is a NaN.

Function: cube-root number

This returns the cube root of number.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.10 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.

In SXEmacs, pseudo-random numbers are generated from a "seed" number. Starting from any given seed, the random function always generates the same sequence of numbers. SXEmacs always starts with the same seed value, so the sequence of values of random is actually the same in each SXEmacs run! For example, in one operating system, the first call to (random) after you start SXEmacs always returns -1457731, and the second one always returns -7692030. This repeatability is helpful for debugging.

If you want truly unpredictable random numbers, execute (random t). This chooses a new seed based on the current time of day and on XEmacs's process ID number.

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.

If limit is t, it means to choose a new seed based on the current time of day and on SXEmacs's process ID number.

On some machines, any integer representable in Lisp may be the result of random. On other machines, the result can never be larger than a certain maximum or less than a certain (negative) minimum.


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by Steve Youngs on September, 23 2008 using texi2html 1.76.