428
|
1 @c -*-texinfo-*-
|
|
2 @c This is part of the XEmacs Lisp Reference Manual.
|
444
|
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
|
428
|
4 @c See the file lispref.texi for copying conditions.
|
|
5 @setfilename ../../info/numbers.info
|
|
6 @node Numbers, Strings and Characters, Lisp Data Types, Top
|
|
7 @chapter Numbers
|
|
8 @cindex integers
|
|
9 @cindex numbers
|
|
10
|
2028
|
11 XEmacs supports two to five numeric data types. @dfn{Integers} and
|
|
12 @dfn{floating point numbers} are always supported. As a build-time
|
|
13 option, @dfn{bignums}, @dfn{ratios}, and @dfn{bigfloats} may be
|
|
14 enabled on some platforms.
|
|
15
|
|
16 Integers, which are what Common Lisp calls
|
|
17 @dfn{fixnums}, are whole numbers such as @minus{}3, 0, #b0111, #xFEED,
|
|
18 #o744. Their values are exact, and their range is limited. The
|
428
|
19 number prefixes `#b', `#o', and `#x' are supported to represent numbers
|
|
20 in binary, octal, and hexadecimal notation (or radix). Floating point
|
|
21 numbers are numbers with fractional parts, such as @minus{}4.5, 0.0, or
|
|
22 2.71828. They can also be expressed in exponential notation: 1.5e2
|
|
23 equals 150; in this example, @samp{e2} stands for ten to the second
|
|
24 power, and is multiplied by 1.5. Floating point values are not exact;
|
|
25 they have a fixed, limited amount of precision.
|
|
26
|
2028
|
27 Bignums are arbitrary precision integers. When supported, XEmacs can
|
|
28 handle any integral calculations you have enough virtual memory to
|
|
29 store. (More precisely, on current architectures the representation
|
|
30 allows integers whose storage would exhaust the address space.) They
|
|
31 are notated in the same way as other integers (fixnums). XEmacs
|
|
32 automatically converts results of computations from fixnum to bignum,
|
|
33 and back, depending on the storage required to represent the number.
|
|
34 Thus use of bignums are entirely transparent to the user, except for a
|
|
35 few special applications that expect overflows. Ratios are rational
|
|
36 numbers with arbitrary precision. (In theory fixed-size rationals could
|
|
37 be supported, but for almost all applications floats are a reasonable
|
|
38 substitute for fixed-precision rationals.) They are notated in the
|
|
39 usual way with the solidus, for example 5/3 or @minus{}22/7. Bigfloats
|
|
40 are floating point numbers with arbitrary precision. Unlike integers,
|
|
41 which are always infinitely precise if they can be represented, floating
|
|
42 point numbers are inherently imprecise. Therefore XEmacs automatically
|
|
43 converts @emph{from float to bigfloat} when floats and bigfloats are
|
|
44 mixed in an expression, but a bigfloat will never be converted to a
|
|
45 float unless the user explicitly coerces the value. Nor will the result
|
|
46 of a float operation be converted to bigfloat, except for ``contagion''
|
|
47 from another operand that is already a bigfloat.
|
|
48
|
|
49 Note that the term ``integer'' is used throughout the XEmacs
|
|
50 documentation and code to mean ``fixnum''. This is inconsistent with
|
|
51 Common Lisp, and likely to cause confusion. Similarly, ``float'' is
|
|
52 used to mean ``fixed precision floating point number'', and the Common
|
|
53 Lisp distinctions among @dfn{short-floats}, @dfn{long-floats}, and
|
|
54 bigfloats are not reflected in XEmacs terminology.
|
|
55
|
428
|
56 @menu
|
|
57 * Integer Basics:: Representation and range of integers.
|
2028
|
58 * Rational Basics:: Representation and range of rational numbers.
|
|
59 * Float Basics:: Representation and range of floating point.
|
|
60 * The Bignum Extension:: Arbitrary precision integers, ratios, and floats.
|
428
|
61 * Predicates on Numbers:: Testing for numbers.
|
|
62 * Comparison of Numbers:: Equality and inequality predicates.
|
2028
|
63 * Numeric Conversions:: Converting float to integer and vice versa.
|
428
|
64 * Arithmetic Operations:: How to add, subtract, multiply and divide.
|
|
65 * Rounding Operations:: Explicitly rounding floating point numbers.
|
|
66 * Bitwise Operations:: Logical and, or, not, shifting.
|
|
67 * Math Functions:: Trig, exponential and logarithmic functions.
|
|
68 * Random Numbers:: Obtaining random integers, predictable or not.
|
|
69 @end menu
|
|
70
|
|
71 @node Integer Basics
|
|
72 @section Integer Basics
|
|
73
|
2028
|
74 The range of values for an integer depends on the machine. If a
|
|
75 multiple-precision arithmetic library is available on your platform,
|
|
76 support for bignums, that is, integers with arbitrary precision, maybe
|
|
77 compiled in to your XEmacs. The rest of this section assumes that the
|
|
78 bignum extension is @emph{not} available. The bignum extension and the
|
|
79 user-visible differences in normal integer arithmetic are discussed in a
|
|
80 separate section @ref{The Bignum Extension}.
|
|
81
|
|
82 The minimum range is @minus{}1073741824 to 1073741823 (31 bits; i.e.,
|
444
|
83 @ifinfo
|
2028
|
84 -2**30
|
428
|
85 @end ifinfo
|
444
|
86 @tex
|
2028
|
87 $-2^{30}$
|
428
|
88 @end tex
|
444
|
89 to
|
|
90 @ifinfo
|
2028
|
91 2**30 - 1),
|
428
|
92 @end ifinfo
|
444
|
93 @tex
|
2028
|
94 $2^{30}-1$),
|
428
|
95 @end tex
|
|
96 but some machines may provide a wider range. Many examples in this
|
2028
|
97 chapter assume an integer has 31 bits.
|
428
|
98 @cindex overflow
|
|
99
|
2028
|
100 The range of fixnums is available to Lisp programs:
|
|
101
|
|
102 @defvar most-positive-fixnum
|
|
103 The fixed-precision integer closest in value to positive infinity.
|
|
104 @end defvar
|
|
105
|
|
106 @defvar most-negative-fixnum
|
|
107 The fixed-precision integer closest in value to negative infinity.
|
|
108 @end defvar
|
|
109
|
|
110 Here is a common idiom to temporarily suppress garbage collection:
|
|
111 @example
|
|
112 (garbage-collect)
|
|
113 (let ((gc-cons-threshold most-positive-fixnum))
|
|
114 ;; allocation-intensive computation
|
|
115 )
|
|
116 (garbage-collect)
|
|
117 @end example
|
|
118
|
428
|
119 The Lisp reader reads an integer as a sequence of digits with optional
|
|
120 initial sign and optional final period.
|
|
121
|
|
122 @example
|
|
123 1 ; @r{The integer 1.}
|
|
124 1. ; @r{The integer 1.}
|
|
125 +1 ; @r{Also the integer 1.}
|
|
126 -1 ; @r{The integer @minus{}1.}
|
2028
|
127 2147483648 ; @r{Read error, due to overflow.}
|
428
|
128 0 ; @r{The integer 0.}
|
|
129 -0 ; @r{The integer 0.}
|
|
130 @end example
|
|
131
|
|
132 To understand how various functions work on integers, especially the
|
|
133 bitwise operators (@pxref{Bitwise Operations}), it is often helpful to
|
|
134 view the numbers in their binary form.
|
|
135
|
2028
|
136 In 31-bit binary, the decimal integer 5 looks like this:
|
428
|
137
|
|
138 @example
|
2028
|
139 000 0000 0000 0000 0000 0000 0000 0101
|
428
|
140 @end example
|
|
141
|
|
142 @noindent
|
|
143 (We have inserted spaces between groups of 4 bits, and two spaces
|
|
144 between groups of 8 bits, to make the binary integer easier to read.)
|
|
145
|
|
146 The integer @minus{}1 looks like this:
|
|
147
|
|
148 @example
|
2028
|
149 111 1111 1111 1111 1111 1111 1111 1111
|
428
|
150 @end example
|
|
151
|
|
152 @noindent
|
|
153 @cindex two's complement
|
2028
|
154 @minus{}1 is represented as 31 ones. (This is called @dfn{two's
|
428
|
155 complement} notation.)
|
|
156
|
|
157 The negative integer, @minus{}5, is creating by subtracting 4 from
|
|
158 @minus{}1. In binary, the decimal integer 4 is 100. Consequently,
|
|
159 @minus{}5 looks like this:
|
|
160
|
|
161 @example
|
2028
|
162 111 1111 1111 1111 1111 1111 1111 1011
|
428
|
163 @end example
|
|
164
|
2028
|
165 In this implementation, the largest 31-bit binary integer is the
|
|
166 decimal integer 1,073,741,823. In binary, it looks like this:
|
428
|
167
|
|
168 @example
|
2028
|
169 011 1111 1111 1111 1111 1111 1111 1111
|
428
|
170 @end example
|
|
171
|
|
172 Since the arithmetic functions do not check whether integers go
|
2028
|
173 outside their range, when you add 1 to 1,073,741,823, the value is the
|
|
174 negative integer @minus{}1,073,741,824:
|
428
|
175
|
|
176 @example
|
2028
|
177 (+ 1 1073741823)
|
|
178 @result{} -1073741824
|
|
179 @result{} 100 0000 0000 0000 0000 0000 0000 0000
|
428
|
180 @end example
|
|
181
|
2028
|
182 Many of the arithmetic functions accept markers for arguments as well
|
428
|
183 as integers. (@xref{Markers}.) More precisely, the actual arguments to
|
|
184 such functions may be either integers or markers, which is why we often
|
|
185 give these arguments the name @var{int-or-marker}. When the argument
|
|
186 value is a marker, its position value is used and its buffer is ignored.
|
|
187
|
|
188 @ignore
|
|
189 In version 19, except where @emph{integer} is specified as an
|
|
190 argument, all of the functions for markers and integers also work for
|
|
191 floating point numbers.
|
|
192 @end ignore
|
|
193
|
2028
|
194
|
2032
|
195 @node Rational Basics
|
|
196 @section Rational Basics
|
2028
|
197
|
|
198 Ratios (built-in rational numbers) are available only when the bignum
|
|
199 extension is built into your XEmacs. This facility is new and
|
|
200 experimental. It is discussed in a separate section for convenience of
|
|
201 updating the documentation @ref{The Bignum Extension}.
|
|
202
|
|
203
|
428
|
204 @node Float Basics
|
|
205 @section Floating Point Basics
|
|
206
|
|
207 XEmacs supports floating point numbers. The precise range of floating
|
|
208 point numbers is machine-specific; it is the same as the range of the C
|
2028
|
209 data type @code{double} on the machine in question. If a
|
|
210 multiple-precision arithmetic library is available on your platform,
|
|
211 support for bigfloats, that is, floating point numbers with arbitrary
|
|
212 precision, maybe compiled in to your XEmacs. The rest of this section
|
|
213 assumes that the bignum extension is @emph{not} available. The bigfloat
|
|
214 extension and the user-visible differences in normal float arithmetic
|
|
215 are discussed in a separate section @ref{The Bignum Extension}.
|
428
|
216
|
|
217 The printed representation for floating point numbers requires either
|
|
218 a decimal point (with at least one digit following), an exponent, or
|
|
219 both. For example, @samp{1500.0}, @samp{15e2}, @samp{15.0e2},
|
|
220 @samp{1.5e3}, and @samp{.15e4} are five ways of writing a floating point
|
|
221 number whose value is 1500. They are all equivalent. You can also use
|
|
222 a minus sign to write negative floating point numbers, as in
|
|
223 @samp{-1.0}.
|
|
224
|
|
225 @cindex IEEE floating point
|
|
226 @cindex positive infinity
|
|
227 @cindex negative infinity
|
|
228 @cindex infinity
|
|
229 @cindex NaN
|
|
230 Most modern computers support the IEEE floating point standard, which
|
|
231 provides for positive infinity and negative infinity as floating point
|
|
232 values. It also provides for a class of values called NaN or
|
|
233 ``not-a-number''; numerical functions return such values in cases where
|
|
234 there is no correct answer. For example, @code{(sqrt -1.0)} returns a
|
|
235 NaN. For practical purposes, there's no significant difference between
|
|
236 different NaN values in XEmacs Lisp, and there's no rule for precisely
|
|
237 which NaN value should be used in a particular case, so this manual
|
|
238 doesn't try to distinguish them. XEmacs Lisp has no read syntax for NaNs
|
|
239 or infinities; perhaps we should create a syntax in the future.
|
|
240
|
|
241 You can use @code{logb} to extract the binary exponent of a floating
|
|
242 point number (or estimate the logarithm of an integer):
|
|
243
|
|
244 @defun logb number
|
|
245 This function returns the binary exponent of @var{number}. More
|
|
246 precisely, the value is the logarithm of @var{number} base 2, rounded
|
|
247 down to an integer.
|
|
248 @end defun
|
|
249
|
2028
|
250 The range of floats is available to Lisp programs:
|
|
251
|
|
252 @defvar most-positive-float
|
|
253 The fixed-precision floating-point-number closest in value to positive
|
|
254 infinity.
|
|
255 @end defvar
|
|
256
|
|
257 @defvar most-negative-float
|
|
258 The fixed-precision floating point number closest in value to negative
|
|
259 infinity.
|
|
260 @end defvar
|
|
261
|
|
262 @defvar least-positive-float
|
|
263 The positive float closest in value to 0. May not be normalized.
|
|
264 @end defvar
|
|
265
|
|
266 @defvar least-negative-float
|
|
267 The positive float closest in value to 0. Must be normalized.
|
|
268 @end defvar
|
|
269
|
|
270 @defvar least-positive-normalized-float
|
|
271 The negative float closest in value to 0. May not be normalized.
|
|
272 @end defvar
|
|
273
|
|
274 @defvar least-negative-normalized-float
|
|
275 The negative float closest in value to 0. Must be normalized.
|
|
276 @end defvar
|
|
277
|
|
278 Note that for floating point numbers there is an interesting limit on
|
|
279 how small they can get, as well as a limit on how big they can get. In
|
|
280 some representations, a floating point number is @dfn{normalized} if the
|
|
281 leading digit is non-zero. This allows representing numbers smaller
|
|
282 than the most-negative exponent can express, by having fractional
|
|
283 mantissas. This means that the number is less precise than a normalized
|
|
284 floating point number, so Lisp programs can detect loss of precision due
|
|
285 to unnormalized floats by checking whether the number is between
|
|
286 @code{least-positive-float} and @code{least-positive-normalized-float}.
|
|
287
|
|
288
|
|
289 @node The Bignum Extension
|
|
290 @section The Bignum Extension
|
|
291
|
|
292 In XEmacs 21.5.18, an extension was added by @email{james@@xemacs.org,
|
|
293 Jerry James} to allow linking with arbitrary-precision arithmetic
|
|
294 libraries if they are available on your platform. ``Arbitrary''
|
|
295 precision means precisely what it says. Your ability to work with large
|
|
296 numbers is limited only by the amount of virtual memory (and time) you
|
|
297 can throw at them.
|
|
298
|
|
299 As of 09 April 2004, support for the GNU Multiple Precision
|
|
300 arithmetic library (GMP) is nearly complete, and support for the BSD
|
|
301 Multiple Precision arithmetic library (MP) is being debugged. To enable
|
|
302 bignum support using GMP (respectively MP), invoke configure with your
|
|
303 usual options, and add @samp{--use-number-lib=gmp} (respectively
|
|
304 @samp{--use-number-lib=mp}). The default is to disable bignum support,
|
|
305 but if you are using a script to automate the build process, it may be
|
|
306 convenient to explicitly disable support by @emph{appending}
|
|
307 @samp{--use-number-lib=no} to your invocation of configure. GMP has an
|
|
308 MP compatibility mode, but it is not recommended, as there remain poorly
|
|
309 understood bugs (even more so than for other vendors' versions of MP).
|
|
310
|
|
311 With GMP, exact arithmetic with integers and ratios of arbitrary
|
|
312 precision and approximate (``floating point'') arithmetic of arbitrary
|
|
313 precision are implemented efficiently in the library. (Note that
|
|
314 numerical implementations are quite delicate and sensitive to
|
|
315 optimization. If the library was poorly optimized for your hardware, as
|
|
316 is often the case with Linux distributions for 80x86, you may achieve
|
|
317 gains of @emph{several orders of magnitude} by rebuilding the MP
|
|
318 library. See @uref{http://www.swox.com/gmp/gmp-speed.html}.) The MP
|
|
319 implementation provides arbitrary precision integers, but ratios were
|
|
320 implemented by the XEmacs implementer, Jerry James, who is not a
|
|
321 numerical analyst. Arbitrary precision floats are not available with
|
|
322 MP.
|
|
323
|
2033
|
324 If your code needs to run correctly whether or not the feature is
|
|
325 provided, you may test for the features @code{bignum}, @code{ratio}, and
|
|
326 @code{bigfloat}.
|
|
327
|
2028
|
328 The XEmacs bignum facility implements the Common Lisp notion of
|
|
329 @dfn{contagion}, so that integers are always represented using the
|
|
330 ``smallest'' representation that is exact, and integral ratios are
|
|
331 converted to integers. Since floating point arithmetic is inherently
|
|
332 imprecise, numbers are implicitly coerced to bigfloats only if other
|
|
333 operands in the expression are bigfloat, and bigfloats are only coerced
|
|
334 to other numerical types by explicit calls to the function @code{coerce}.
|
|
335
|
|
336 Bignum support is incomplete. If you would like to help with bignum
|
|
337 support, especially on BSD MP, please subscribe to the
|
|
338 @uref{http://www.xemacs.org/Lists/#xemacs-beta, XEmacs Beta mailing
|
|
339 list}, and book up on @file{number-gmp.h} and @file{number-mp.h}. Jerry
|
|
340 has promised to write internals documentation eventually, but if your
|
|
341 skills run more to analysis and documentation than to writing new code,
|
|
342 feel free to fill in the gap!
|
|
343
|
|
344 @menu
|
|
345 * Bignum Basics:: Representation and range of integers.
|
|
346 * Ratio Basics:: Representation and range of rational numbers.
|
|
347 * Bigfloat Basics:: Representation and range of floating point.
|
|
348 * Contagion and Canonicalization:: Automatic coercion to other types.
|
|
349 * Compatibility Issues:: Changes in fixed-precision arithmetic.
|
|
350 @end menu
|
|
351
|
|
352
|
|
353 @node Bignum Basics
|
|
354 @subsection Bignum Basics
|
|
355
|
|
356 In most cases, bignum support should be transparent to users and Lisp
|
|
357 programmers. A bignum-enabled XEmacs will automatically convert from
|
|
358 fixnums to bignums and back in pure integer arithmetic, and for GNU MP,
|
|
359 from floats to bigfloats. (Bigfloats must be explicitly coerced to
|
|
360 other types, even if they are exactly representable by less precise
|
|
361 types.) The Lisp reader and printer have been enhanced to handle
|
|
362 bignums, as have the mathematical functions. Rationals (fixnums,
|
|
363 bignums, and ratios) are printed using the @samp{%d}, @samp{%o},
|
|
364 @samp{%x}, and @samp{%u} format conversions.
|
|
365
|
|
366
|
|
367 @node Ratio Basics
|
|
368 @subsection Ratio Basics
|
|
369
|
|
370 Ratios, when available have the read syntax and print representation
|
|
371 @samp{3/5}. Like other rationals (fixnums and bignums), they are
|
|
372 printed using the @samp{%d}, @samp{%o}, @samp{%x}, and @samp{%u} format
|
|
373 conversions.
|
|
374
|
|
375
|
|
376 @node Bigfloat Basics
|
|
377 @subsection Bigfloat Basics
|
|
378
|
|
379 Bigfloats, when available, have the same read syntax and print
|
|
380 representations as fixed-precision floats.
|
|
381
|
|
382
|
|
383 @node Contagion and Canonicalization
|
|
384 @subsection Contagion
|
|
385
|
|
386 @dfn{Contagion} is one way to address the requirement that an arithmetic
|
|
387 operation should not fail because of differing types of the operands, or
|
|
388 insufficient precision in the representation. With bignum support, we
|
|
389 can represent @code{(+ most-positive-fixnum most-positive-fixnum)} (or
|
|
390 @code{(* most-positive-fixnum most-positive-fixnum)}, for that matter)
|
|
391 @emph{exactly}. There should be no overflow or ``wrap-around,'' and the
|
|
392 computation should not be interrupted by an error. Contagion is the
|
|
393 idea that less precise operands are converted to the more precise type,
|
|
394 and then the operation is performed. This involves no loss of
|
|
395 information, and therefore it is safe to do it automatically.
|
|
396
|
|
397 In XEmacs, the following rules of contagion are used:
|
|
398
|
|
399 @c #### this probably wants names for each rule
|
|
400 @enumerate
|
|
401 @item
|
|
402 If a fixnum operation would overflow or underflow, the operands are
|
|
403 promoted to bignums and the operation is performed.
|
|
404
|
|
405 @item
|
|
406 If a fixnum and a bignum are the operands, the fixnum is promoted to
|
|
407 bignum, and the operation is performed.
|
|
408
|
|
409 @c #### seems plausible
|
|
410 @item
|
|
411 If a float operation would overflow or underflow (@emph{i.e.}, produce
|
|
412 an unrepresentably small but non-zero result), the operands are
|
|
413 converted to bigfloats and the operation performed.
|
|
414
|
|
415 @c #### seems likely....
|
|
416 @item
|
|
417 If an expression mixes a rational type (fixnum, bignum, or ratio) with a
|
|
418 float, the rational operand is converted to a float and the operation
|
|
419 performed if the result would fit in a float, otherwise both operands
|
|
420 are promoted to bigfloat, and the operation performed.
|
|
421
|
|
422 @item
|
|
423 If an expression mixes any other type with a bigfloat, the other operand
|
|
424 is converted to bigfloat and the operation performed.
|
|
425 @end enumerate
|
|
426
|
|
427 Note that there is @emph{no} contagion with ratios. Integer-to-integer
|
|
428 arithmetic with truncation or rounding is useful, and familiar.
|
|
429 Therefore instead of converting the result of @code{/} to ratio if it is
|
|
430 non-integral, the traditional definition is maintained, and a new
|
|
431 function @code{div} is provided to give a ratio result if a division
|
|
432 does not come out evenly.
|
|
433
|
|
434 On the other hand, the representation of arbitrary precision numbers is
|
|
435 inefficient in both space and time. The principle of
|
|
436 @dfn{canonicalization} addresses this issue. The idea of
|
|
437 canonicalization is that when no information is lost, the representation
|
|
438 should be demoted to the more efficient (smaller) representation. Note
|
|
439 that the inefficiency is likely to be greater than you might think.
|
|
440 Experience with numerical analysis shows that in very precise
|
|
441 calculations, precision tends to increase. Thus it is typically wasted
|
|
442 effort to attempt to convert to smaller representations, as the number
|
|
443 is often reused and requires a larger representation. However, XEmacs
|
|
444 Lisp presumes that calculations using bignums are the exception, so it
|
|
445 applies canonicalization. The rules are
|
|
446
|
|
447 @enumerate
|
|
448 @item
|
|
449 If a ratio is integral, demote it to a bignum. (In XEmacs Lisp all
|
|
450 ratios are arbitrary precision numbers.)
|
|
451
|
|
452 @item
|
|
453 If a bignum is small enough to fit in a fixnum, demote it to fixnum.
|
|
454 @end enumerate
|
|
455
|
|
456 Note that there are no rules to canonicalize floats or bigfloats. This
|
|
457 might seem surprising, but in both cases information will be lost. Any
|
|
458 floating point representation is implicitly approximate. A conversion
|
|
459 to a rational type, even if it seems exact, loses this information.
|
|
460 More subtly, demoting a bigfloat to a smaller bigfloat or to a float
|
|
461 would lose information about the precision of the result, and thus some
|
|
462 information about the accuracy. Thus floating point numbers are always
|
|
463 already in canonical form.
|
|
464
|
|
465 Of course the programmer can explicitly request canonicalization, or
|
|
466 more coercion to another type. Coercion uses the Common Lisp
|
|
467 compatibility function @code{coerce} from the @file{cl-extra.el}
|
|
468 library. A number can be explicitly converted to canonical form
|
|
469 according to the above rules using
|
|
470
|
|
471 @defun canonicalize-number number
|
|
472 Return the canonical form of @var{number}.
|
|
473 @end defun
|
|
474
|
|
475
|
|
476 @node Compatibility Issues
|
|
477 @subsection Compatibility Issues
|
|
478
|
|
479 @emph{Surgeon General's Warning}: The automatic conversions cannot be
|
|
480 disabled at runtime. Old functions will not produce ratios unless there
|
|
481 is a ratio operand, so there should be few surprises with type
|
|
482 conflicts (the contagion rules are quite natural for Lisp programmers
|
|
483 used to the behavior of integers and floats in pre-21.5.18 XEmacsen),
|
|
484 but they can't be ruled out. Also, if you work with extremely large
|
|
485 numbers, your machine may arbitrarily decide to hand you an unpleasant
|
|
486 surprise rather than a bignum.
|
|
487
|
|
488 User-visible changes in behavior include (in probable order of annoyance)
|
|
489
|
|
490 @itemize
|
|
491 @item
|
|
492 Arithmetic can cause a segfault, depending on your MP library.
|
|
493
|
|
494 GMP by default allocates temporaries on the stack. If you run out of
|
|
495 stack space, you're dead; there is no way that we know of to reliably
|
|
496 detect this condition, because @samp{alloca} is typically implemented to
|
|
497 be @emph{fast} rather than robust. If you just need a little more
|
|
498 oomph, use a bigger stack (@emph{e.g.}, the @file{ulimit -s} command in
|
|
499 bash(1)). If you want robustness at the cost of speed, configure GMP
|
|
500 with @samp{--disable-alloca} and rebuild the GMP library.
|
|
501
|
|
502 We do not know whether BSD MP uses @samp{alloca} or not. Please send
|
|
503 any information you have as a bug report (@kbd{M-x report-xemacs-bug
|
|
504 @key{RET}}), which will give us platform information. (We do know that
|
|
505 BSD MP implementations vary across vendors, but how much, we do not know
|
|
506 yet.)
|
|
507
|
|
508 @item
|
|
509 Terminology is not Common-Lisp-conforming. For example, ``integer'' for
|
|
510 Emacs Lisp means what Common Lisp calls ``fixnum''. This issue is being
|
|
511 investigated, but the use of ``integer'' for fixnum is pervasive and may
|
|
512 cause backward-compatibility and GNU-Emacs-compatibility problems.
|
|
513 There are similar issues for floating point numbers. Since Emacs Lisp
|
|
514 has not had a ratio type before, there should be no problems there.
|
|
515
|
|
516 @item
|
|
517 An atom with ratio read syntax now returns a number, not a symbol.
|
|
518
|
|
519 @item
|
|
520 Many operations that used to cause a range error now succeed, with
|
|
521 intermediate results and return values coerced to bignums as needed.
|
|
522
|
|
523 @item
|
|
524 The @samp{%u} format conversion will now give an error if its argument
|
|
525 is negative. (Without MP, it prints a number which Lisp can't read.)
|
|
526 @end itemize
|
|
527
|
|
528 This is not a compatibility issue in the sense of specification, but
|
|
529 careless programmers who have taken advantage of the immediate
|
|
530 representation for numbers and written @code{(eq x y)} are in for a
|
|
531 surprise. This doesn't work with bignums, even if both arguments are
|
|
532 bignums! Arbitrary precision obviously requires consing new objects
|
|
533 because the objects are ``large'' and of variable size, and the
|
|
534 definition of @samp{eq} does not permit different objects to compare as
|
|
535 equal. Instead of @code{eq}, use @code{eql}, in which numbers of the
|
|
536 same type which have equal values compare equal, or @code{=}, which does
|
|
537 any necessary type coercions before comparing for equality
|
|
538 @ref{Comparison of Numbers}.
|
|
539
|
|
540
|
428
|
541 @node Predicates on Numbers
|
|
542 @section Type Predicates for Numbers
|
|
543
|
|
544 The functions in this section test whether the argument is a number or
|
|
545 whether it is a certain sort of number. The functions @code{integerp}
|
|
546 and @code{floatp} can take any type of Lisp object as argument (the
|
|
547 predicates would not be of much use otherwise); but the @code{zerop}
|
|
548 predicate requires a number as its argument. See also
|
|
549 @code{integer-or-marker-p}, @code{integer-char-or-marker-p},
|
|
550 @code{number-or-marker-p} and @code{number-char-or-marker-p}, in
|
|
551 @ref{Predicates on Markers}.
|
|
552
|
|
553 @defun floatp object
|
|
554 This predicate tests whether its argument is a floating point
|
|
555 number and returns @code{t} if so, @code{nil} otherwise.
|
|
556
|
|
557 @code{floatp} does not exist in Emacs versions 18 and earlier.
|
|
558 @end defun
|
|
559
|
|
560 @defun integerp object
|
|
561 This predicate tests whether its argument is an integer, and returns
|
|
562 @code{t} if so, @code{nil} otherwise.
|
|
563 @end defun
|
|
564
|
|
565 @defun numberp object
|
|
566 This predicate tests whether its argument is a number (either integer or
|
|
567 floating point), and returns @code{t} if so, @code{nil} otherwise.
|
|
568 @end defun
|
|
569
|
|
570 @defun natnump object
|
|
571 @cindex natural numbers
|
|
572 The @code{natnump} predicate (whose name comes from the phrase
|
|
573 ``natural-number-p'') tests to see whether its argument is a nonnegative
|
|
574 integer, and returns @code{t} if so, @code{nil} otherwise. 0 is
|
|
575 considered non-negative.
|
|
576 @end defun
|
|
577
|
|
578 @defun zerop number
|
|
579 This predicate tests whether its argument is zero, and returns @code{t}
|
|
580 if so, @code{nil} otherwise. The argument must be a number.
|
|
581
|
|
582 These two forms are equivalent: @code{(zerop x)} @equiv{} @code{(= x 0)}.
|
|
583 @end defun
|
|
584
|
|
585 @node Comparison of Numbers
|
|
586 @section Comparison of Numbers
|
|
587 @cindex number equality
|
|
588
|
|
589 To test numbers for numerical equality, you should normally use
|
|
590 @code{=}, not @code{eq}. There can be many distinct floating point
|
|
591 number objects with the same numeric value. If you use @code{eq} to
|
|
592 compare them, then you test whether two values are the same
|
|
593 @emph{object}. By contrast, @code{=} compares only the numeric values
|
|
594 of the objects.
|
|
595
|
2028
|
596 In versions before 21.5.18, each integer value had a unique Lisp
|
|
597 object in XEmacs Lisp. Therefore, @code{eq} was equivalent to @code{=}
|
|
598 where integers are concerned. Even with the introduction of bignums, it
|
|
599 is sometimes convenient to use @code{eq} for comparing an unknown value
|
|
600 with an integer, because @code{eq} does not report an error if the
|
|
601 unknown value is not a number---it accepts arguments of any type. By
|
|
602 contrast, @code{=} signals an error if the arguments are not numbers or
|
|
603 markers. However, it is a good idea to use @code{=} if you can, even
|
|
604 for comparing exact values, because two bignums or ratios with the same
|
|
605 value will often not be the same object.
|
428
|
606
|
|
607 There is another wrinkle: because floating point arithmetic is not
|
|
608 exact, it is often a bad idea to check for equality of two floating
|
|
609 point values. Usually it is better to test for approximate equality.
|
|
610 Here's a function to do this:
|
|
611
|
|
612 @example
|
|
613 (defconst fuzz-factor 1.0e-6)
|
|
614 (defun approx-equal (x y)
|
|
615 (or (and (= x 0) (= y 0))
|
|
616 (< (/ (abs (- x y))
|
|
617 (max (abs x) (abs y)))
|
|
618 fuzz-factor)))
|
|
619 @end example
|
|
620
|
|
621 @cindex CL note---integers vrs @code{eq}
|
|
622 @quotation
|
|
623 @b{Common Lisp note:} Comparing numbers in Common Lisp always requires
|
|
624 @code{=} because Common Lisp implements multi-word integers, and two
|
|
625 distinct integer objects can have the same numeric value. XEmacs Lisp
|
|
626 can have just one integer object for any given value because it has a
|
|
627 limited range of integer values.
|
|
628 @end quotation
|
|
629
|
|
630 In addition to numbers, all of the following functions also accept
|
|
631 characters and markers as arguments, and treat them as their number
|
|
632 equivalents.
|
|
633
|
|
634 @defun = number &rest more-numbers
|
|
635 This function returns @code{t} if all of its arguments are numerically
|
|
636 equal, @code{nil} otherwise.
|
|
637
|
|
638 @example
|
|
639 (= 5)
|
|
640 @result{} t
|
|
641 (= 5 6)
|
|
642 @result{} nil
|
|
643 (= 5 5.0)
|
|
644 @result{} t
|
|
645 (= 5 5 6)
|
|
646 @result{} nil
|
|
647 @end example
|
|
648 @end defun
|
|
649
|
|
650 @defun /= number &rest more-numbers
|
|
651 This function returns @code{t} if no two arguments are numerically
|
|
652 equal, @code{nil} otherwise.
|
|
653
|
|
654 @example
|
|
655 (/= 5 6)
|
|
656 @result{} t
|
|
657 (/= 5 5 6)
|
|
658 @result{} nil
|
|
659 (/= 5 6 1)
|
|
660 @result{} t
|
|
661 @end example
|
|
662 @end defun
|
|
663
|
|
664 @defun < number &rest more-numbers
|
|
665 This function returns @code{t} if the sequence of its arguments is
|
|
666 monotonically increasing, @code{nil} otherwise.
|
|
667
|
|
668 @example
|
|
669 (< 5 6)
|
|
670 @result{} t
|
|
671 (< 5 6 6)
|
|
672 @result{} nil
|
|
673 (< 5 6 7)
|
|
674 @result{} t
|
|
675 @end example
|
|
676 @end defun
|
|
677
|
|
678 @defun <= number &rest more-numbers
|
|
679 This function returns @code{t} if the sequence of its arguments is
|
|
680 monotonically nondecreasing, @code{nil} otherwise.
|
|
681
|
|
682 @example
|
|
683 (<= 5 6)
|
|
684 @result{} t
|
|
685 (<= 5 6 6)
|
|
686 @result{} t
|
|
687 (<= 5 6 5)
|
|
688 @result{} nil
|
|
689 @end example
|
|
690 @end defun
|
|
691
|
|
692 @defun > number &rest more-numbers
|
|
693 This function returns @code{t} if the sequence of its arguments is
|
|
694 monotonically decreasing, @code{nil} otherwise.
|
|
695 @end defun
|
|
696
|
|
697 @defun >= number &rest more-numbers
|
|
698 This function returns @code{t} if the sequence of its arguments is
|
|
699 monotonically nonincreasing, @code{nil} otherwise.
|
|
700 @end defun
|
|
701
|
|
702 @defun max number &rest more-numbers
|
|
703 This function returns the largest of its arguments.
|
|
704
|
|
705 @example
|
|
706 (max 20)
|
|
707 @result{} 20
|
|
708 (max 1 2.5)
|
|
709 @result{} 2.5
|
|
710 (max 1 3 2.5)
|
|
711 @result{} 3
|
|
712 @end example
|
|
713 @end defun
|
|
714
|
|
715 @defun min number &rest more-numbers
|
|
716 This function returns the smallest of its arguments.
|
|
717
|
|
718 @example
|
|
719 (min -4 1)
|
|
720 @result{} -4
|
|
721 @end example
|
|
722 @end defun
|
|
723
|
|
724 @node Numeric Conversions
|
|
725 @section Numeric Conversions
|
|
726 @cindex rounding in conversions
|
|
727
|
|
728 To convert an integer to floating point, use the function @code{float}.
|
|
729
|
|
730 @defun float number
|
|
731 This returns @var{number} converted to floating point.
|
|
732 If @var{number} is already a floating point number, @code{float} returns
|
|
733 it unchanged.
|
|
734 @end defun
|
|
735
|
|
736 There are four functions to convert floating point numbers to integers;
|
|
737 they differ in how they round. These functions accept integer arguments
|
|
738 also, and return such arguments unchanged.
|
|
739
|
|
740 @defun truncate number
|
|
741 This returns @var{number}, converted to an integer by rounding towards
|
|
742 zero.
|
|
743 @end defun
|
|
744
|
|
745 @defun floor number &optional divisor
|
|
746 This returns @var{number}, converted to an integer by rounding downward
|
|
747 (towards negative infinity).
|
|
748
|
|
749 If @var{divisor} is specified, @var{number} is divided by @var{divisor}
|
|
750 before the floor is taken; this is the division operation that
|
|
751 corresponds to @code{mod}. An @code{arith-error} results if
|
|
752 @var{divisor} is 0.
|
|
753 @end defun
|
|
754
|
|
755 @defun ceiling number
|
|
756 This returns @var{number}, converted to an integer by rounding upward
|
|
757 (towards positive infinity).
|
|
758 @end defun
|
|
759
|
|
760 @defun round number
|
|
761 This returns @var{number}, converted to an integer by rounding towards the
|
|
762 nearest integer. Rounding a value equidistant between two integers
|
|
763 may choose the integer closer to zero, or it may prefer an even integer,
|
|
764 depending on your machine.
|
|
765 @end defun
|
|
766
|
|
767 @node Arithmetic Operations
|
|
768 @section Arithmetic Operations
|
|
769
|
|
770 XEmacs Lisp provides the traditional four arithmetic operations:
|
|
771 addition, subtraction, multiplication, and division. Remainder and modulus
|
|
772 functions supplement the division functions. The functions to
|
|
773 add or subtract 1 are provided because they are traditional in Lisp and
|
|
774 commonly used.
|
|
775
|
|
776 All of these functions except @code{%} return a floating point value
|
|
777 if any argument is floating.
|
|
778
|
|
779 It is important to note that in XEmacs Lisp, arithmetic functions
|
|
780 do not check for overflow. Thus @code{(1+ 134217727)} may evaluate to
|
|
781 @minus{}134217728, depending on your hardware.
|
|
782
|
444
|
783 @defun 1+ number
|
|
784 This function returns @var{number} plus one. @var{number} may be a
|
|
785 number, character or marker. Markers and characters are converted to
|
|
786 integers.
|
|
787
|
428
|
788 For example,
|
|
789
|
|
790 @example
|
|
791 (setq foo 4)
|
|
792 @result{} 4
|
|
793 (1+ foo)
|
|
794 @result{} 5
|
|
795 @end example
|
|
796
|
|
797 This function is not analogous to the C operator @code{++}---it does not
|
|
798 increment a variable. It just computes a sum. Thus, if we continue,
|
|
799
|
|
800 @example
|
|
801 foo
|
|
802 @result{} 4
|
|
803 @end example
|
|
804
|
|
805 If you want to increment the variable, you must use @code{setq},
|
|
806 like this:
|
|
807
|
|
808 @example
|
|
809 (setq foo (1+ foo))
|
|
810 @result{} 5
|
|
811 @end example
|
|
812
|
|
813 Now that the @code{cl} package is always available from lisp code, a
|
|
814 more convenient and natural way to increment a variable is
|
|
815 @w{@code{(incf foo)}}.
|
|
816 @end defun
|
|
817
|
444
|
818 @defun 1- number
|
|
819 This function returns @var{number} minus one. @var{number} may be a
|
|
820 number, character or marker. Markers and characters are converted to
|
|
821 integers.
|
428
|
822 @end defun
|
|
823
|
|
824 @defun abs number
|
|
825 This returns the absolute value of @var{number}.
|
|
826 @end defun
|
|
827
|
444
|
828 @defun + &rest numbers
|
428
|
829 This function adds its arguments together. When given no arguments,
|
|
830 @code{+} returns 0.
|
|
831
|
444
|
832 If any of the arguments are characters or markers, they are first
|
|
833 converted to integers.
|
|
834
|
428
|
835 @example
|
|
836 (+)
|
|
837 @result{} 0
|
|
838 (+ 1)
|
|
839 @result{} 1
|
|
840 (+ 1 2 3 4)
|
|
841 @result{} 10
|
|
842 @end example
|
|
843 @end defun
|
|
844
|
444
|
845 @defun - &optional number &rest other-numbers
|
428
|
846 The @code{-} function serves two purposes: negation and subtraction.
|
|
847 When @code{-} has a single argument, the value is the negative of the
|
|
848 argument. When there are multiple arguments, @code{-} subtracts each of
|
444
|
849 the @var{other-numbers} from @var{number}, cumulatively. If there are
|
|
850 no arguments, an error is signaled.
|
|
851
|
|
852 If any of the arguments are characters or markers, they are first
|
|
853 converted to integers.
|
428
|
854
|
|
855 @example
|
|
856 (- 10 1 2 3 4)
|
|
857 @result{} 0
|
|
858 (- 10)
|
|
859 @result{} -10
|
|
860 (-)
|
|
861 @result{} 0
|
|
862 @end example
|
|
863 @end defun
|
|
864
|
444
|
865 @defun * &rest numbers
|
428
|
866 This function multiplies its arguments together, and returns the
|
|
867 product. When given no arguments, @code{*} returns 1.
|
|
868
|
444
|
869 If any of the arguments are characters or markers, they are first
|
|
870 converted to integers.
|
|
871
|
428
|
872 @example
|
|
873 (*)
|
|
874 @result{} 1
|
|
875 (* 1)
|
|
876 @result{} 1
|
|
877 (* 1 2 3 4)
|
|
878 @result{} 24
|
|
879 @end example
|
|
880 @end defun
|
|
881
|
444
|
882 @defun / dividend &rest divisors
|
|
883 The @code{/} function serves two purposes: inversion and division. When
|
|
884 @code{/} has a single argument, the value is the inverse of the
|
|
885 argument. When there are multiple arguments, @code{/} divides
|
|
886 @var{dividend} by each of the @var{divisors}, cumulatively, returning
|
|
887 the quotient. If there are no arguments, an error is signaled.
|
428
|
888
|
444
|
889 If none of the arguments are floats, then the result is an integer.
|
428
|
890 This means the result has to be rounded. On most machines, the result
|
|
891 is rounded towards zero after each division, but some machines may round
|
|
892 differently with negative arguments. This is because the Lisp function
|
|
893 @code{/} is implemented using the C division operator, which also
|
|
894 permits machine-dependent rounding. As a practical matter, all known
|
|
895 machines round in the standard fashion.
|
|
896
|
444
|
897 If any of the arguments are characters or markers, they are first
|
|
898 converted to integers.
|
|
899
|
428
|
900 @cindex @code{arith-error} in division
|
|
901 If you divide by 0, an @code{arith-error} error is signaled.
|
|
902 (@xref{Errors}.)
|
|
903
|
|
904 @example
|
|
905 @group
|
|
906 (/ 6 2)
|
|
907 @result{} 3
|
|
908 @end group
|
|
909 (/ 5 2)
|
|
910 @result{} 2
|
|
911 (/ 25 3 2)
|
|
912 @result{} 4
|
444
|
913 (/ 3.0)
|
|
914 @result{} 0.3333333333333333
|
428
|
915 (/ -17 6)
|
|
916 @result{} -2
|
|
917 @end example
|
|
918
|
|
919 The result of @code{(/ -17 6)} could in principle be -3 on some
|
|
920 machines.
|
|
921 @end defun
|
|
922
|
|
923 @defun % dividend divisor
|
|
924 @cindex remainder
|
|
925 This function returns the integer remainder after division of @var{dividend}
|
|
926 by @var{divisor}. The arguments must be integers or markers.
|
|
927
|
|
928 For negative arguments, the remainder is in principle machine-dependent
|
|
929 since the quotient is; but in practice, all known machines behave alike.
|
|
930
|
|
931 An @code{arith-error} results if @var{divisor} is 0.
|
|
932
|
|
933 @example
|
|
934 (% 9 4)
|
|
935 @result{} 1
|
|
936 (% -9 4)
|
|
937 @result{} -1
|
|
938 (% 9 -4)
|
|
939 @result{} 1
|
|
940 (% -9 -4)
|
|
941 @result{} -1
|
|
942 @end example
|
|
943
|
|
944 For any two integers @var{dividend} and @var{divisor},
|
|
945
|
|
946 @example
|
|
947 @group
|
|
948 (+ (% @var{dividend} @var{divisor})
|
|
949 (* (/ @var{dividend} @var{divisor}) @var{divisor}))
|
|
950 @end group
|
|
951 @end example
|
|
952
|
|
953 @noindent
|
|
954 always equals @var{dividend}.
|
|
955 @end defun
|
|
956
|
|
957 @defun mod dividend divisor
|
|
958 @cindex modulus
|
|
959 This function returns the value of @var{dividend} modulo @var{divisor};
|
|
960 in other words, the remainder after division of @var{dividend}
|
|
961 by @var{divisor}, but with the same sign as @var{divisor}.
|
|
962 The arguments must be numbers or markers.
|
|
963
|
|
964 Unlike @code{%}, @code{mod} returns a well-defined result for negative
|
|
965 arguments. It also permits floating point arguments; it rounds the
|
|
966 quotient downward (towards minus infinity) to an integer, and uses that
|
|
967 quotient to compute the remainder.
|
|
968
|
|
969 An @code{arith-error} results if @var{divisor} is 0.
|
|
970
|
|
971 @example
|
|
972 @group
|
|
973 (mod 9 4)
|
|
974 @result{} 1
|
|
975 @end group
|
|
976 @group
|
|
977 (mod -9 4)
|
|
978 @result{} 3
|
|
979 @end group
|
|
980 @group
|
|
981 (mod 9 -4)
|
|
982 @result{} -3
|
|
983 @end group
|
|
984 @group
|
|
985 (mod -9 -4)
|
|
986 @result{} -1
|
|
987 @end group
|
|
988 @group
|
|
989 (mod 5.5 2.5)
|
|
990 @result{} .5
|
|
991 @end group
|
|
992 @end example
|
|
993
|
|
994 For any two numbers @var{dividend} and @var{divisor},
|
|
995
|
|
996 @example
|
|
997 @group
|
|
998 (+ (mod @var{dividend} @var{divisor})
|
|
999 (* (floor @var{dividend} @var{divisor}) @var{divisor}))
|
|
1000 @end group
|
|
1001 @end example
|
|
1002
|
|
1003 @noindent
|
|
1004 always equals @var{dividend}, subject to rounding error if either
|
|
1005 argument is floating point. For @code{floor}, see @ref{Numeric
|
|
1006 Conversions}.
|
|
1007 @end defun
|
|
1008
|
|
1009 @node Rounding Operations
|
|
1010 @section Rounding Operations
|
|
1011 @cindex rounding without conversion
|
|
1012
|
|
1013 The functions @code{ffloor}, @code{fceiling}, @code{fround} and
|
|
1014 @code{ftruncate} take a floating point argument and return a floating
|
|
1015 point result whose value is a nearby integer. @code{ffloor} returns the
|
|
1016 nearest integer below; @code{fceiling}, the nearest integer above;
|
|
1017 @code{ftruncate}, the nearest integer in the direction towards zero;
|
|
1018 @code{fround}, the nearest integer.
|
|
1019
|
444
|
1020 @defun ffloor number
|
|
1021 This function rounds @var{number} to the next lower integral value, and
|
428
|
1022 returns that value as a floating point number.
|
|
1023 @end defun
|
|
1024
|
444
|
1025 @defun fceiling number
|
|
1026 This function rounds @var{number} to the next higher integral value, and
|
428
|
1027 returns that value as a floating point number.
|
|
1028 @end defun
|
|
1029
|
444
|
1030 @defun ftruncate number
|
|
1031 This function rounds @var{number} towards zero to an integral value, and
|
428
|
1032 returns that value as a floating point number.
|
|
1033 @end defun
|
|
1034
|
444
|
1035 @defun fround number
|
|
1036 This function rounds @var{number} to the nearest integral value,
|
428
|
1037 and returns that value as a floating point number.
|
|
1038 @end defun
|
|
1039
|
|
1040 @node Bitwise Operations
|
|
1041 @section Bitwise Operations on Integers
|
|
1042
|
|
1043 In a computer, an integer is represented as a binary number, a
|
|
1044 sequence of @dfn{bits} (digits which are either zero or one). A bitwise
|
|
1045 operation acts on the individual bits of such a sequence. For example,
|
|
1046 @dfn{shifting} moves the whole sequence left or right one or more places,
|
|
1047 reproducing the same pattern ``moved over''.
|
|
1048
|
|
1049 The bitwise operations in XEmacs Lisp apply only to integers.
|
|
1050
|
|
1051 @defun lsh integer1 count
|
|
1052 @cindex logical shift
|
|
1053 @code{lsh}, which is an abbreviation for @dfn{logical shift}, shifts the
|
|
1054 bits in @var{integer1} to the left @var{count} places, or to the right
|
|
1055 if @var{count} is negative, bringing zeros into the vacated bits. If
|
|
1056 @var{count} is negative, @code{lsh} shifts zeros into the leftmost
|
|
1057 (most-significant) bit, producing a positive result even if
|
|
1058 @var{integer1} is negative. Contrast this with @code{ash}, below.
|
|
1059
|
|
1060 Here are two examples of @code{lsh}, shifting a pattern of bits one
|
|
1061 place to the left. We show only the low-order eight bits of the binary
|
|
1062 pattern; the rest are all zero.
|
|
1063
|
|
1064 @example
|
|
1065 @group
|
|
1066 (lsh 5 1)
|
|
1067 @result{} 10
|
|
1068 ;; @r{Decimal 5 becomes decimal 10.}
|
|
1069 00000101 @result{} 00001010
|
|
1070
|
|
1071 (lsh 7 1)
|
|
1072 @result{} 14
|
|
1073 ;; @r{Decimal 7 becomes decimal 14.}
|
|
1074 00000111 @result{} 00001110
|
|
1075 @end group
|
|
1076 @end example
|
|
1077
|
|
1078 @noindent
|
|
1079 As the examples illustrate, shifting the pattern of bits one place to
|
|
1080 the left produces a number that is twice the value of the previous
|
|
1081 number.
|
|
1082
|
|
1083 Shifting a pattern of bits two places to the left produces results
|
|
1084 like this (with 8-bit binary numbers):
|
|
1085
|
|
1086 @example
|
|
1087 @group
|
|
1088 (lsh 3 2)
|
|
1089 @result{} 12
|
|
1090 ;; @r{Decimal 3 becomes decimal 12.}
|
444
|
1091 00000011 @result{} 00001100
|
428
|
1092 @end group
|
|
1093 @end example
|
|
1094
|
|
1095 On the other hand, shifting one place to the right looks like this:
|
|
1096
|
|
1097 @example
|
|
1098 @group
|
|
1099 (lsh 6 -1)
|
|
1100 @result{} 3
|
|
1101 ;; @r{Decimal 6 becomes decimal 3.}
|
444
|
1102 00000110 @result{} 00000011
|
428
|
1103 @end group
|
|
1104
|
|
1105 @group
|
|
1106 (lsh 5 -1)
|
|
1107 @result{} 2
|
|
1108 ;; @r{Decimal 5 becomes decimal 2.}
|
444
|
1109 00000101 @result{} 00000010
|
428
|
1110 @end group
|
|
1111 @end example
|
|
1112
|
|
1113 @noindent
|
|
1114 As the example illustrates, shifting one place to the right divides the
|
|
1115 value of a positive integer by two, rounding downward.
|
|
1116
|
|
1117 The function @code{lsh}, like all XEmacs Lisp arithmetic functions, does
|
|
1118 not check for overflow, so shifting left can discard significant bits
|
|
1119 and change the sign of the number. For example, left shifting
|
|
1120 134,217,727 produces @minus{}2 on a 28-bit machine:
|
|
1121
|
|
1122 @example
|
|
1123 (lsh 134217727 1) ; @r{left shift}
|
|
1124 @result{} -2
|
|
1125 @end example
|
|
1126
|
|
1127 In binary, in the 28-bit implementation, the argument looks like this:
|
|
1128
|
|
1129 @example
|
|
1130 @group
|
|
1131 ;; @r{Decimal 134,217,727}
|
444
|
1132 0111 1111 1111 1111 1111 1111 1111
|
428
|
1133 @end group
|
|
1134 @end example
|
|
1135
|
|
1136 @noindent
|
|
1137 which becomes the following when left shifted:
|
|
1138
|
|
1139 @example
|
|
1140 @group
|
|
1141 ;; @r{Decimal @minus{}2}
|
444
|
1142 1111 1111 1111 1111 1111 1111 1110
|
428
|
1143 @end group
|
|
1144 @end example
|
|
1145 @end defun
|
|
1146
|
|
1147 @defun ash integer1 count
|
|
1148 @cindex arithmetic shift
|
|
1149 @code{ash} (@dfn{arithmetic shift}) shifts the bits in @var{integer1}
|
|
1150 to the left @var{count} places, or to the right if @var{count}
|
|
1151 is negative.
|
|
1152
|
|
1153 @code{ash} gives the same results as @code{lsh} except when
|
|
1154 @var{integer1} and @var{count} are both negative. In that case,
|
|
1155 @code{ash} puts ones in the empty bit positions on the left, while
|
|
1156 @code{lsh} puts zeros in those bit positions.
|
|
1157
|
|
1158 Thus, with @code{ash}, shifting the pattern of bits one place to the right
|
|
1159 looks like this:
|
|
1160
|
|
1161 @example
|
|
1162 @group
|
444
|
1163 (ash -6 -1) @result{} -3
|
428
|
1164 ;; @r{Decimal @minus{}6 becomes decimal @minus{}3.}
|
|
1165 1111 1111 1111 1111 1111 1111 1010
|
444
|
1166 @result{}
|
428
|
1167 1111 1111 1111 1111 1111 1111 1101
|
|
1168 @end group
|
|
1169 @end example
|
|
1170
|
|
1171 In contrast, shifting the pattern of bits one place to the right with
|
|
1172 @code{lsh} looks like this:
|
|
1173
|
|
1174 @example
|
|
1175 @group
|
|
1176 (lsh -6 -1) @result{} 134217725
|
|
1177 ;; @r{Decimal @minus{}6 becomes decimal 134,217,725.}
|
|
1178 1111 1111 1111 1111 1111 1111 1010
|
444
|
1179 @result{}
|
428
|
1180 0111 1111 1111 1111 1111 1111 1101
|
|
1181 @end group
|
|
1182 @end example
|
|
1183
|
|
1184 Here are other examples:
|
|
1185
|
|
1186 @c !!! Check if lined up in smallbook format! XDVI shows problem
|
|
1187 @c with smallbook but not with regular book! --rjc 16mar92
|
|
1188 @smallexample
|
|
1189 @group
|
|
1190 ; @r{ 28-bit binary values}
|
|
1191
|
|
1192 (lsh 5 2) ; 5 = @r{0000 0000 0000 0000 0000 0000 0101}
|
|
1193 @result{} 20 ; = @r{0000 0000 0000 0000 0000 0001 0100}
|
|
1194 @end group
|
|
1195 @group
|
|
1196 (ash 5 2)
|
|
1197 @result{} 20
|
|
1198 (lsh -5 2) ; -5 = @r{1111 1111 1111 1111 1111 1111 1011}
|
|
1199 @result{} -20 ; = @r{1111 1111 1111 1111 1111 1110 1100}
|
|
1200 (ash -5 2)
|
|
1201 @result{} -20
|
|
1202 @end group
|
|
1203 @group
|
|
1204 (lsh 5 -2) ; 5 = @r{0000 0000 0000 0000 0000 0000 0101}
|
|
1205 @result{} 1 ; = @r{0000 0000 0000 0000 0000 0000 0001}
|
|
1206 @end group
|
|
1207 @group
|
|
1208 (ash 5 -2)
|
|
1209 @result{} 1
|
|
1210 @end group
|
|
1211 @group
|
|
1212 (lsh -5 -2) ; -5 = @r{1111 1111 1111 1111 1111 1111 1011}
|
|
1213 @result{} 4194302 ; = @r{0011 1111 1111 1111 1111 1111 1110}
|
|
1214 @end group
|
|
1215 @group
|
|
1216 (ash -5 -2) ; -5 = @r{1111 1111 1111 1111 1111 1111 1011}
|
|
1217 @result{} -2 ; = @r{1111 1111 1111 1111 1111 1111 1110}
|
|
1218 @end group
|
|
1219 @end smallexample
|
|
1220 @end defun
|
|
1221
|
|
1222 @defun logand &rest ints-or-markers
|
|
1223 @cindex logical and
|
|
1224 @cindex bitwise and
|
|
1225 This function returns the ``logical and'' of the arguments: the
|
|
1226 @var{n}th bit is set in the result if, and only if, the @var{n}th bit is
|
|
1227 set in all the arguments. (``Set'' means that the value of the bit is 1
|
|
1228 rather than 0.)
|
|
1229
|
|
1230 For example, using 4-bit binary numbers, the ``logical and'' of 13 and
|
|
1231 12 is 12: 1101 combined with 1100 produces 1100.
|
|
1232 In both the binary numbers, the leftmost two bits are set (i.e., they
|
|
1233 are 1's), so the leftmost two bits of the returned value are set.
|
|
1234 However, for the rightmost two bits, each is zero in at least one of
|
|
1235 the arguments, so the rightmost two bits of the returned value are 0's.
|
|
1236
|
|
1237 @noindent
|
|
1238 Therefore,
|
|
1239
|
|
1240 @example
|
|
1241 @group
|
|
1242 (logand 13 12)
|
|
1243 @result{} 12
|
|
1244 @end group
|
|
1245 @end example
|
|
1246
|
|
1247 If @code{logand} is not passed any argument, it returns a value of
|
|
1248 @minus{}1. This number is an identity element for @code{logand}
|
|
1249 because its binary representation consists entirely of ones. If
|
|
1250 @code{logand} is passed just one argument, it returns that argument.
|
|
1251
|
|
1252 @smallexample
|
|
1253 @group
|
|
1254 ; @r{ 28-bit binary values}
|
|
1255
|
|
1256 (logand 14 13) ; 14 = @r{0000 0000 0000 0000 0000 0000 1110}
|
|
1257 ; 13 = @r{0000 0000 0000 0000 0000 0000 1101}
|
|
1258 @result{} 12 ; 12 = @r{0000 0000 0000 0000 0000 0000 1100}
|
|
1259 @end group
|
|
1260
|
|
1261 @group
|
|
1262 (logand 14 13 4) ; 14 = @r{0000 0000 0000 0000 0000 0000 1110}
|
|
1263 ; 13 = @r{0000 0000 0000 0000 0000 0000 1101}
|
|
1264 ; 4 = @r{0000 0000 0000 0000 0000 0000 0100}
|
|
1265 @result{} 4 ; 4 = @r{0000 0000 0000 0000 0000 0000 0100}
|
|
1266 @end group
|
|
1267
|
|
1268 @group
|
|
1269 (logand)
|
|
1270 @result{} -1 ; -1 = @r{1111 1111 1111 1111 1111 1111 1111}
|
|
1271 @end group
|
|
1272 @end smallexample
|
|
1273 @end defun
|
|
1274
|
|
1275 @defun logior &rest ints-or-markers
|
|
1276 @cindex logical inclusive or
|
|
1277 @cindex bitwise or
|
|
1278 This function returns the ``inclusive or'' of its arguments: the @var{n}th bit
|
|
1279 is set in the result if, and only if, the @var{n}th bit is set in at least
|
|
1280 one of the arguments. If there are no arguments, the result is zero,
|
|
1281 which is an identity element for this operation. If @code{logior} is
|
|
1282 passed just one argument, it returns that argument.
|
|
1283
|
|
1284 @smallexample
|
|
1285 @group
|
|
1286 ; @r{ 28-bit binary values}
|
|
1287
|
|
1288 (logior 12 5) ; 12 = @r{0000 0000 0000 0000 0000 0000 1100}
|
|
1289 ; 5 = @r{0000 0000 0000 0000 0000 0000 0101}
|
|
1290 @result{} 13 ; 13 = @r{0000 0000 0000 0000 0000 0000 1101}
|
|
1291 @end group
|
|
1292
|
|
1293 @group
|
|
1294 (logior 12 5 7) ; 12 = @r{0000 0000 0000 0000 0000 0000 1100}
|
|
1295 ; 5 = @r{0000 0000 0000 0000 0000 0000 0101}
|
|
1296 ; 7 = @r{0000 0000 0000 0000 0000 0000 0111}
|
|
1297 @result{} 15 ; 15 = @r{0000 0000 0000 0000 0000 0000 1111}
|
|
1298 @end group
|
|
1299 @end smallexample
|
|
1300 @end defun
|
|
1301
|
|
1302 @defun logxor &rest ints-or-markers
|
|
1303 @cindex bitwise exclusive or
|
|
1304 @cindex logical exclusive or
|
|
1305 This function returns the ``exclusive or'' of its arguments: the
|
|
1306 @var{n}th bit is set in the result if, and only if, the @var{n}th bit is
|
|
1307 set in an odd number of the arguments. If there are no arguments, the
|
|
1308 result is 0, which is an identity element for this operation. If
|
|
1309 @code{logxor} is passed just one argument, it returns that argument.
|
|
1310
|
|
1311 @smallexample
|
|
1312 @group
|
|
1313 ; @r{ 28-bit binary values}
|
|
1314
|
|
1315 (logxor 12 5) ; 12 = @r{0000 0000 0000 0000 0000 0000 1100}
|
|
1316 ; 5 = @r{0000 0000 0000 0000 0000 0000 0101}
|
|
1317 @result{} 9 ; 9 = @r{0000 0000 0000 0000 0000 0000 1001}
|
|
1318 @end group
|
|
1319
|
|
1320 @group
|
|
1321 (logxor 12 5 7) ; 12 = @r{0000 0000 0000 0000 0000 0000 1100}
|
|
1322 ; 5 = @r{0000 0000 0000 0000 0000 0000 0101}
|
|
1323 ; 7 = @r{0000 0000 0000 0000 0000 0000 0111}
|
|
1324 @result{} 14 ; 14 = @r{0000 0000 0000 0000 0000 0000 1110}
|
|
1325 @end group
|
|
1326 @end smallexample
|
|
1327 @end defun
|
|
1328
|
|
1329 @defun lognot integer
|
|
1330 @cindex logical not
|
|
1331 @cindex bitwise not
|
|
1332 This function returns the logical complement of its argument: the @var{n}th
|
|
1333 bit is one in the result if, and only if, the @var{n}th bit is zero in
|
|
1334 @var{integer}, and vice-versa.
|
|
1335
|
|
1336 @example
|
444
|
1337 (lognot 5)
|
428
|
1338 @result{} -6
|
|
1339 ;; 5 = @r{0000 0000 0000 0000 0000 0000 0101}
|
|
1340 ;; @r{becomes}
|
|
1341 ;; -6 = @r{1111 1111 1111 1111 1111 1111 1010}
|
|
1342 @end example
|
|
1343 @end defun
|
|
1344
|
|
1345 @node Math Functions
|
|
1346 @section Standard Mathematical Functions
|
|
1347 @cindex transcendental functions
|
|
1348 @cindex mathematical functions
|
|
1349
|
|
1350 These mathematical functions are available if floating point is
|
|
1351 supported (which is the normal state of affairs). They allow integers
|
|
1352 as well as floating point numbers as arguments.
|
|
1353
|
444
|
1354 @defun sin number
|
|
1355 @defunx cos number
|
|
1356 @defunx tan number
|
428
|
1357 These are the ordinary trigonometric functions, with argument measured
|
|
1358 in radians.
|
|
1359 @end defun
|
|
1360
|
444
|
1361 @defun asin number
|
|
1362 The value of @code{(asin @var{number})} is a number between @minus{}pi/2
|
|
1363 and pi/2 (inclusive) whose sine is @var{number}; if, however, @var{number}
|
428
|
1364 is out of range (outside [-1, 1]), then the result is a NaN.
|
|
1365 @end defun
|
|
1366
|
444
|
1367 @defun acos number
|
|
1368 The value of @code{(acos @var{number})} is a number between 0 and pi
|
|
1369 (inclusive) whose cosine is @var{number}; if, however, @var{number}
|
428
|
1370 is out of range (outside [-1, 1]), then the result is a NaN.
|
|
1371 @end defun
|
|
1372
|
444
|
1373 @defun atan number &optional number2
|
|
1374 The value of @code{(atan @var{number})} is a number between @minus{}pi/2
|
|
1375 and pi/2 (exclusive) whose tangent is @var{number}.
|
|
1376
|
|
1377 If optional argument @var{number2} is supplied, the function returns
|
|
1378 @code{atan2(@var{number},@var{number2})}.
|
428
|
1379 @end defun
|
|
1380
|
444
|
1381 @defun sinh number
|
|
1382 @defunx cosh number
|
|
1383 @defunx tanh number
|
428
|
1384 These are the ordinary hyperbolic trigonometric functions.
|
|
1385 @end defun
|
|
1386
|
444
|
1387 @defun asinh number
|
|
1388 @defunx acosh number
|
|
1389 @defunx atanh number
|
428
|
1390 These are the inverse hyperbolic trigonometric functions.
|
|
1391 @end defun
|
|
1392
|
444
|
1393 @defun exp number
|
428
|
1394 This is the exponential function; it returns @i{e} to the power
|
444
|
1395 @var{number}. @i{e} is a fundamental mathematical constant also called the
|
428
|
1396 base of natural logarithms.
|
|
1397 @end defun
|
|
1398
|
444
|
1399 @defun log number &optional base
|
|
1400 This function returns the logarithm of @var{number}, with base @var{base}.
|
1738
|
1401 If you don't specify @var{base}, the base @code{e} is used. If @var{number}
|
428
|
1402 is negative, the result is a NaN.
|
|
1403 @end defun
|
|
1404
|
444
|
1405 @defun log10 number
|
|
1406 This function returns the logarithm of @var{number}, with base 10. If
|
|
1407 @var{number} is negative, the result is a NaN. @code{(log10 @var{x})}
|
428
|
1408 @equiv{} @code{(log @var{x} 10)}, at least approximately.
|
|
1409 @end defun
|
|
1410
|
|
1411 @defun expt x y
|
|
1412 This function returns @var{x} raised to power @var{y}. If both
|
|
1413 arguments are integers and @var{y} is positive, the result is an
|
|
1414 integer; in this case, it is truncated to fit the range of possible
|
|
1415 integer values.
|
|
1416 @end defun
|
|
1417
|
444
|
1418 @defun sqrt number
|
|
1419 This returns the square root of @var{number}. If @var{number} is negative,
|
428
|
1420 the value is a NaN.
|
|
1421 @end defun
|
|
1422
|
444
|
1423 @defun cube-root number
|
|
1424 This returns the cube root of @var{number}.
|
428
|
1425 @end defun
|
|
1426
|
|
1427 @node Random Numbers
|
|
1428 @section Random Numbers
|
|
1429 @cindex random numbers
|
|
1430
|
|
1431 A deterministic computer program cannot generate true random numbers.
|
|
1432 For most purposes, @dfn{pseudo-random numbers} suffice. A series of
|
|
1433 pseudo-random numbers is generated in a deterministic fashion. The
|
|
1434 numbers are not truly random, but they have certain properties that
|
|
1435 mimic a random series. For example, all possible values occur equally
|
|
1436 often in a pseudo-random series.
|
|
1437
|
|
1438 In XEmacs, pseudo-random numbers are generated from a ``seed'' number.
|
|
1439 Starting from any given seed, the @code{random} function always
|
|
1440 generates the same sequence of numbers. XEmacs always starts with the
|
|
1441 same seed value, so the sequence of values of @code{random} is actually
|
|
1442 the same in each XEmacs run! For example, in one operating system, the
|
|
1443 first call to @code{(random)} after you start XEmacs always returns
|
|
1444 -1457731, and the second one always returns -7692030. This
|
|
1445 repeatability is helpful for debugging.
|
|
1446
|
|
1447 If you want truly unpredictable random numbers, execute @code{(random
|
|
1448 t)}. This chooses a new seed based on the current time of day and on
|
|
1449 XEmacs's process @sc{id} number.
|
|
1450
|
|
1451 @defun random &optional limit
|
|
1452 This function returns a pseudo-random integer. Repeated calls return a
|
|
1453 series of pseudo-random integers.
|
|
1454
|
|
1455 If @var{limit} is a positive integer, the value is chosen to be
|
|
1456 nonnegative and less than @var{limit}.
|
|
1457
|
|
1458 If @var{limit} is @code{t}, it means to choose a new seed based on the
|
|
1459 current time of day and on XEmacs's process @sc{id} number.
|
|
1460 @c "XEmacs'" is incorrect usage!
|
|
1461
|
|
1462 On some machines, any integer representable in Lisp may be the result
|
|
1463 of @code{random}. On other machines, the result can never be larger
|
|
1464 than a certain maximum or less than a certain (negative) minimum.
|
|
1465 @end defun
|