"Fossies" - the Fresh Open Source Software Archive

Member "emacs-25.3/doc/lispref/numbers.texi" (14 Apr 2017, 37067 Bytes) of package /linux/misc/emacs-25.3.tar.xz:

Caution: As a special service "Fossies" has tried to format the requested Texinfo source page into HTML format but that may be not always succeeeded perfectly. Alternatively you can here view or download the uninterpreted Texinfo source code. A member file download can also be achieved by clicking within a package contents listing on the according byte size field. See also the last Fossies "Diffs" side-by-side code changes report for "numbers.texi": 25.1_vs_25.2.

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

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

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

1.1 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 section Bitwise Operations on Integers), 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. (@xref{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. @xref{Character Codes}.

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

1.2 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 s e

Given a numeric significand s and an integer exponent e, this function returns the floating point number s * 2**e.

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
```

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

1.3 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 @ref{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)`.

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

1.4 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. @xref{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.

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

1.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 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
```

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

1.6 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: / number &rest divisors

With one or more divisors, this function divides number by each divisor in divisors in turn, and returns the quotient. With no divisors, this function returns 1/number, i.e., the multiplicative inverse of number. 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
```
```(/ 4.0)
⇒ 0.25
```
```(/ 4)
⇒ 0
```
```(/ 25 3 2)
⇒ 4
```
```(/ -17 6)
⇒ -2
```

If you divide an integer by the integer 0, Emacs signals an `arith-error` error (@pxref{Errors}). Floating-point division of a nonzero number by zero yields either positive or negative infinity (see section Floating-Point 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.

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

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

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

1.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 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 bitwise AND of the arguments: the nth bit is 1 in the result if, and only if, the nth bit is 1 in all the arguments.

For example, using 4-bit binary numbers, the bitwise AND of 13 and 12 is 12: 1101 combined with 1100 produces 1100. In both the binary numbers, the leftmost two bits are both 1 so the leftmost two bits of the returned value are both 1. However, for the rightmost two bits, each is 0 in at least one of the arguments, so the rightmost two bits of the returned value are both 0.

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 bitwise inclusive OR of its arguments: the nth bit is 1 in the result if, and only if, the nth bit is 1 in at least one of the arguments. If there are no arguments, the result is 0, 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 bitwise exclusive OR of its arguments: the nth bit is 1 in the result if, and only if, the nth bit is 1 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 bitwise 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)
```

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

1.9 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…).

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

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

Pseudo-random numbers are generated from a seed value. 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 section Integer Basics).

If limit is `t`, it means to choose a new seed as if Emacs were restarting, typically from the system entropy. On systems lacking entropy pools, choose the seed from less-random volatile data such as the current time.

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

 [Top] [Contents] [Index] [ ? ]

This document was generated on September 17, 2017 using texi2html.

The buttons in the navigation panels have the following meaning:

Button Name Go to From 1.2.3 go to
[ << ] FastBack Beginning of this chapter or previous chapter 1
[ < ] Back Previous section in reading order 1.2.2
[ Up ] Up Up section 1.2
[ > ] Forward Next section in reading order 1.2.4
[ >> ] FastForward Next chapter 2
[Top] Top Cover (top) of document
[Index] Index Index

where the Example assumes that the current position is at Subsubsection One-Two-Three of a document of the following structure:

• 1. Section One
• 1.1 Subsection One-One
• ...
• 1.2 Subsection One-Two
• 1.2.1 Subsubsection One-Two-One
• 1.2.2 Subsubsection One-Two-Two
• 1.2.3 Subsubsection One-Two-Three     <== Current Position
• 1.2.4 Subsubsection One-Two-Four
• 1.3 Subsection One-Three
• ...
• 1.4 Subsection One-Four

This document was generated on September 17, 2017 using texi2html.