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