annotate man/lispref/numbers.texi @ 431:a97165e56215

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