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
2182
+ − 411 It is possible to make bigfloat the default floating point format by
+ − 412 setting @code{default-float-precision} to a non-zero value. Precision
+ − 413 is given in bits, with a maximum precision of @code{bigfloat-max-prec}.
+ − 414 @c #### is this true?
+ − 415 Bigfloats are created automatically when a number with yes
+ − 416
+ − 417
2028
+ − 418
2090
+ − 419 @node Canonicalization and Contagion
+ − 420 @subsection Canonicalization and Contagion
+ − 421
+ − 422 @dfn{Canonicalization} is a rule intended to enhance the time and space
+ − 423 efficiency of exact arithmetic. Because bignums and ratios are
+ − 424 implemented as record objects, they take up much more space than
+ − 425 fixnums, which are implemented as an immediate object. Conversions and
+ − 426 calls to the MP library also take time. So the implementation always
+ − 427 converts the result of exact arithmetic to the smallest representation
+ − 428 that can exactly represent the quantity.
+ − 429
+ − 430 @example
+ − 431 (+ 3/4 5)
+ − 432 @result{} 23/4
+ − 433
+ − 434 (+ 3/4 1/4 2)
+ − 435 @result{} 3
+ − 436 @end example
+ − 437
+ − 438 Conversely, if an integer (read or computed) cannot be represented as a
+ − 439 fixnum, a bignum will be used. Integer division is a somewhat
+ − 440 exceptional case. Because it is useful and is the historical meaning of
+ − 441 the function @code{/}, a separate function @code{div} is provided.
+ − 442 @code{div} is identical to @code{/} except that when the rational result
+ − 443 is not an integer, it is represented exactly as a ratio. In both cases
+ − 444 if a rational result is an integer, it is automatically converted to the
+ − 445 appropriate integral representation.
+ − 446
+ − 447 Note that the efficiency gain from canonicalization is likely to be
+ − 448 less than you might think. Experience with numerical analysis shows that
+ − 449 in very precise calculations, the required precision tends to increase.
+ − 450 Thus it is typically wasted effort to attempt to convert to smaller
+ − 451 representations, as the number is often reused and requires a larger
+ − 452 representation. However, XEmacs Lisp presumes that calculations using
+ − 453 bignums are the exception, so it applies canonicalization.
2028
+ − 454
+ − 455 @dfn{Contagion} is one way to address the requirement that an arithmetic
2090
+ − 456 operation should not fail because of differing types of the operands.
+ − 457 Contagion is the idea that less precise operands are converted to the
+ − 458 more precise type, and then the operation is performed. While changing
+ − 459 precision is a delicate issue, contagion is so useful that XEmacs
+ − 460 performs it automatically.
2028
+ − 461
+ − 462 In XEmacs, the following rules of contagion are used:
+ − 463
+ − 464 @c #### this probably wants names for each rule
+ − 465 @enumerate
+ − 466 @item
2090
+ − 467 If an expression mixes an integral type with a ratio, then the usual
+ − 468 rules of rational arithmetic apply. (If the result of the expression
+ − 469 happens to be an integer, it will be canonicalized to integer.)
2028
+ − 470
+ − 471 @item
+ − 472 If an expression mixes a rational type (fixnum, bignum, or ratio) with a
+ − 473 float, the rational operand is converted to a float and the operation
+ − 474 performed if the result would fit in a float, otherwise both operands
+ − 475 are promoted to bigfloat, and the operation performed.
+ − 476
+ − 477 @item
+ − 478 If an expression mixes any other type with a bigfloat, the other operand
+ − 479 is converted to bigfloat and the operation performed.
+ − 480
+ − 481 @item
2090
+ − 482 If bigfloats of different precision are mixed, all are converted to the
+ − 483 @emph{highest} precision, and the operation performed.
2028
+ − 484 @end enumerate
+ − 485
+ − 486 Note that there are no rules to canonicalize floats or bigfloats. This
+ − 487 might seem surprising, but in both cases information will be lost. Any
+ − 488 floating point representation is implicitly approximate. A conversion
+ − 489 to a rational type, even if it seems exact, loses this information.
+ − 490 More subtly, demoting a bigfloat to a smaller bigfloat or to a float
+ − 491 would lose information about the precision of the result, and thus some
+ − 492 information about the accuracy. Thus floating point numbers are always
+ − 493 already in canonical form.
+ − 494
+ − 495 Of course the programmer can explicitly request canonicalization, or
+ − 496 more coercion to another type. Coercion uses the Common Lisp
+ − 497 compatibility function @code{coerce} from the @file{cl-extra.el}
+ − 498 library. A number can be explicitly converted to canonical form
+ − 499 according to the above rules using
+ − 500
+ − 501 @defun canonicalize-number number
+ − 502 Return the canonical form of @var{number}.
+ − 503 @end defun
+ − 504
2090
+ − 505 However, if we've done our job properly, this is always a no-op. That
+ − 506 is, if you find a number in un-canonicalized form, please report it as a
+ − 507 bug.
+ − 508
2028
+ − 509
+ − 510 @node Compatibility Issues
+ − 511 @subsection Compatibility Issues
+ − 512
+ − 513 @emph{Surgeon General's Warning}: The automatic conversions cannot be
+ − 514 disabled at runtime. Old functions will not produce ratios unless there
+ − 515 is a ratio operand, so there should be few surprises with type
+ − 516 conflicts (the contagion rules are quite natural for Lisp programmers
+ − 517 used to the behavior of integers and floats in pre-21.5.18 XEmacsen),
+ − 518 but they can't be ruled out. Also, if you work with extremely large
+ − 519 numbers, your machine may arbitrarily decide to hand you an unpleasant
+ − 520 surprise rather than a bignum.
+ − 521
+ − 522 User-visible changes in behavior include (in probable order of annoyance)
+ − 523
+ − 524 @itemize
+ − 525 @item
+ − 526 Arithmetic can cause a segfault, depending on your MP library.
+ − 527
+ − 528 GMP by default allocates temporaries on the stack. If you run out of
+ − 529 stack space, you're dead; there is no way that we know of to reliably
+ − 530 detect this condition, because @samp{alloca} is typically implemented to
+ − 531 be @emph{fast} rather than robust. If you just need a little more
+ − 532 oomph, use a bigger stack (@emph{e.g.}, the @file{ulimit -s} command in
+ − 533 bash(1)). If you want robustness at the cost of speed, configure GMP
+ − 534 with @samp{--disable-alloca} and rebuild the GMP library.
+ − 535
+ − 536 We do not know whether BSD MP uses @samp{alloca} or not. Please send
+ − 537 any information you have as a bug report (@kbd{M-x report-xemacs-bug
+ − 538 @key{RET}}), which will give us platform information. (We do know that
+ − 539 BSD MP implementations vary across vendors, but how much, we do not know
+ − 540 yet.)
+ − 541
+ − 542 @item
+ − 543 Terminology is not Common-Lisp-conforming. For example, ``integer'' for
+ − 544 Emacs Lisp means what Common Lisp calls ``fixnum''. This issue is being
+ − 545 investigated, but the use of ``integer'' for fixnum is pervasive and may
+ − 546 cause backward-compatibility and GNU-Emacs-compatibility problems.
+ − 547 There are similar issues for floating point numbers. Since Emacs Lisp
+ − 548 has not had a ratio type before, there should be no problems there.
+ − 549
+ − 550 @item
+ − 551 An atom with ratio read syntax now returns a number, not a symbol.
+ − 552
+ − 553 @item
+ − 554 Many operations that used to cause a range error now succeed, with
+ − 555 intermediate results and return values coerced to bignums as needed.
+ − 556
+ − 557 @item
+ − 558 The @samp{%u} format conversion will now give an error if its argument
+ − 559 is negative. (Without MP, it prints a number which Lisp can't read.)
+ − 560 @end itemize
+ − 561
+ − 562 This is not a compatibility issue in the sense of specification, but
+ − 563 careless programmers who have taken advantage of the immediate
+ − 564 representation for numbers and written @code{(eq x y)} are in for a
+ − 565 surprise. This doesn't work with bignums, even if both arguments are
+ − 566 bignums! Arbitrary precision obviously requires consing new objects
+ − 567 because the objects are ``large'' and of variable size, and the
+ − 568 definition of @samp{eq} does not permit different objects to compare as
+ − 569 equal. Instead of @code{eq}, use @code{eql}, in which numbers of the
+ − 570 same type which have equal values compare equal, or @code{=}, which does
+ − 571 any necessary type coercions before comparing for equality
+ − 572 @ref{Comparison of Numbers}.
+ − 573
+ − 574
428
+ − 575 @node Predicates on Numbers
+ − 576 @section Type Predicates for Numbers
+ − 577
+ − 578 The functions in this section test whether the argument is a number or
2090
+ − 579 whether it is a certain sort of number. The functions which test for
+ − 580 type can take any type of Lisp object as argument (the more general
+ − 581 predicates would not be of much use otherwise). However, the
+ − 582 @code{zerop} predicate requires a number as its argument, and the
+ − 583 @code{evenp}, and @code{oddp} predicates require integers as their
+ − 584 arguments. See also @code{integer-or-marker-p},
+ − 585 @code{integer-char-or-marker-p}, @code{number-or-marker-p} and
+ − 586 @code{number-char-or-marker-p}, in @ref{Predicates on Markers}.
428
+ − 587
2090
+ − 588 @defun numberp object
+ − 589 This predicate tests whether its argument is a number (either integer or
+ − 590 floating point), and returns @code{t} if so, @code{nil} otherwise.
+ − 591 @end defun
428
+ − 592
2090
+ − 593 @defun realp object
+ − 594 @cindex numbers
+ − 595 The @code{realp} predicate tests to see whether @var{object} is a
+ − 596 rational or floating point number, and returns @code{t} if so,
+ − 597 @code{nil} otherwise. Currently equivalent to @code{numberp}.
+ − 598 @end defun
+ − 599
+ − 600 @defun zerop number
+ − 601 This predicate tests whether its argument is zero, and returns @code{t}
+ − 602 if so, @code{nil} otherwise. The argument must be a number.
+ − 603
+ − 604 These two forms are equivalent: @code{(zerop x)} @equiv{} @code{(= x 0)}.
428
+ − 605 @end defun
+ − 606
+ − 607 @defun integerp object
+ − 608 This predicate tests whether its argument is an integer, and returns
+ − 609 @code{t} if so, @code{nil} otherwise.
+ − 610 @end defun
+ − 611
2090
+ − 612 @defun oddp integer
+ − 613 @cindex integers
+ − 614 The @code{oddp} predicate tests to see whether @var{integer} is odd, and
+ − 615 returns @code{t} if so, @code{nil} otherwise. @var{integer} must be an
+ − 616 integer.
+ − 617 @end defun
+ − 618
+ − 619 @defun evenp integer
+ − 620 @cindex integers
+ − 621 The @code{evenp} predicate tests to see whether @var{integer} is even,
+ − 622 and returns @code{t} if so, @code{nil} otherwise. @var{integer} must be
+ − 623 an integer.
428
+ − 624 @end defun
+ − 625
+ − 626 @defun natnump object
+ − 627 @cindex natural numbers
+ − 628 The @code{natnump} predicate (whose name comes from the phrase
+ − 629 ``natural-number-p'') tests to see whether its argument is a nonnegative
+ − 630 integer, and returns @code{t} if so, @code{nil} otherwise. 0 is
+ − 631 considered non-negative.
+ − 632 @end defun
+ − 633
2090
+ − 634 @defun fixnump object
+ − 635 @cindex integers
+ − 636 The @code{} predicate tests to see whether its argument is an integer
+ − 637 represented as a fixnum, and returns @code{t} if so, @code{nil}
+ − 638 otherwise.
+ − 639 @end defun
+ − 640
+ − 641 @defun bignump object
+ − 642 @cindex integers
+ − 643 The @code{bignump} predicate tests to see whether @var{object} is an
+ − 644 integer represented as a bignum, and returns @code{t} if so, @code{nil}
+ − 645 otherwise.
+ − 646 @end defun
+ − 647
+ − 648 @defun rationalp object
+ − 649 @cindex numbers
+ − 650 The @code{rationalp} predicate tests to see whether @var{object} is a
+ − 651 rational number, and returns @code{t} if so, @code{nil} otherwise.
+ − 652 @end defun
428
+ − 653
2090
+ − 654 @defun ratiop object
+ − 655 @cindex ratios
+ − 656 The @code{ratiop} predicate tests to see whether @var{object} is a
+ − 657 number represented as a ratio, and returns @code{t} if so, @code{nil}
+ − 658 otherwise.
+ − 659 @end defun
+ − 660
+ − 661 @defun floatingp object
+ − 662 @cindex floats
+ − 663 The @code{floatingp} predicate tests to see whether @var{object} is a
+ − 664 floating point number represented as a float or a bigfloat, and returns
+ − 665 @code{t} if so, @code{nil} otherwise.
428
+ − 666 @end defun
+ − 667
2090
+ − 668 @defun floatp object
+ − 669 @cindex floats
+ − 670 This predicate tests whether its argument is a floating point
+ − 671 number and returns @code{t} if so, @code{nil} otherwise.
+ − 672
+ − 673 @code{floatp} does not exist in Emacs versions 18 and earlier. If the
+ − 674 bignum extension is present, it returns @code{nil} for a bigfloat.
+ − 675 @end defun
+ − 676
+ − 677 @defun bigfloatp object
+ − 678 @cindex floats
+ − 679 The @code{bigfloatp} predicate tests to see whether @var{object} is an
2091
+ − 680 floating point number represented as a bigfloat, and returns @code{t} if
+ − 681 so, @code{nil} otherwise.
2090
+ − 682 @end defun
+ − 683
+ − 684
428
+ − 685 @node Comparison of Numbers
+ − 686 @section Comparison of Numbers
+ − 687 @cindex number equality
+ − 688
+ − 689 To test numbers for numerical equality, you should normally use
2090
+ − 690 @code{=}, not @code{eq}. There can be many distinct floating point,
+ − 691 bignum, and ratio number objects with the same numeric value. If you
+ − 692 use @code{eq} to compare them, then you test whether two values are the
+ − 693 same @emph{object}. By contrast, @code{=} compares only the numeric
+ − 694 values of the objects.
428
+ − 695
2028
+ − 696 In versions before 21.5.18, each integer value had a unique Lisp
+ − 697 object in XEmacs Lisp. Therefore, @code{eq} was equivalent to @code{=}
+ − 698 where integers are concerned. Even with the introduction of bignums, it
+ − 699 is sometimes convenient to use @code{eq} for comparing an unknown value
+ − 700 with an integer, because @code{eq} does not report an error if the
+ − 701 unknown value is not a number---it accepts arguments of any type. By
+ − 702 contrast, @code{=} signals an error if the arguments are not numbers or
+ − 703 markers. However, it is a good idea to use @code{=} if you can, even
+ − 704 for comparing exact values, because two bignums or ratios with the same
+ − 705 value will often not be the same object.
428
+ − 706
2090
+ − 707 On the other hand, some functions, such as the string- and
+ − 708 buffer-searching functions, will return an integer on success, but
+ − 709 something else (usually @code{nil}) on failure. If it is known what the
+ − 710 numerical subtype (float, bigfloat, or exact) of the returned object
+ − 711 will be if it is a number, then the predicate @code{eql} can be used for
+ − 712 comparison without signaling an error on some expected return values.
+ − 713 Because of canonicalization, @code{eql} can be used to compare a fixnum
+ − 714 value to something that might be a ratio; if the potential ratio value
+ − 715 is representable as a fixnum, it will be canonicalized to fixnum before
2091
+ − 716 comparing. However, although floats and bigfloats are of different
+ − 717 types for the purpose of comparisons via @code{eql}, two bigfloats of
+ − 718 different @emph{precision} that are @code{=} will always be @code{eql}.
2090
+ − 719
+ − 720 @example
+ − 721 (eql 2 (string-match "ere" "there"))
+ − 722 @result{} t
+ − 723
+ − 724 (eql 2 (string-match "ere" "three"))
+ − 725 @result{} nil
+ − 726
+ − 727 (eql 2 2.0)
+ − 728 @result{} nil
+ − 729
+ − 730 (= 2 (string-match "ere" "there"))
+ − 731 @result{} t
+ − 732
+ − 733 (= 2 (string-match "ere" "three"))
+ − 734 @error{} Wrong type argument: number-char-or-marker-p, nil
+ − 735
+ − 736 (= 2 2.0)
+ − 737 @result{} t
+ − 738 @end example
+ − 739
+ − 740
+ − 741
428
+ − 742 There is another wrinkle: because floating point arithmetic is not
+ − 743 exact, it is often a bad idea to check for equality of two floating
+ − 744 point values. Usually it is better to test for approximate equality.
+ − 745 Here's a function to do this:
+ − 746
+ − 747 @example
+ − 748 (defconst fuzz-factor 1.0e-6)
+ − 749 (defun approx-equal (x y)
+ − 750 (or (and (= x 0) (= y 0))
+ − 751 (< (/ (abs (- x y))
+ − 752 (max (abs x) (abs y)))
+ − 753 fuzz-factor)))
+ − 754 @end example
+ − 755
+ − 756 @cindex CL note---integers vrs @code{eq}
+ − 757 @quotation
+ − 758 @b{Common Lisp note:} Comparing numbers in Common Lisp always requires
+ − 759 @code{=} because Common Lisp implements multi-word integers, and two
+ − 760 distinct integer objects can have the same numeric value. XEmacs Lisp
+ − 761 can have just one integer object for any given value because it has a
+ − 762 limited range of integer values.
+ − 763 @end quotation
+ − 764
+ − 765 In addition to numbers, all of the following functions also accept
+ − 766 characters and markers as arguments, and treat them as their number
+ − 767 equivalents.
+ − 768
+ − 769 @defun = number &rest more-numbers
+ − 770 This function returns @code{t} if all of its arguments are numerically
+ − 771 equal, @code{nil} otherwise.
+ − 772
+ − 773 @example
+ − 774 (= 5)
+ − 775 @result{} t
+ − 776 (= 5 6)
+ − 777 @result{} nil
+ − 778 (= 5 5.0)
+ − 779 @result{} t
+ − 780 (= 5 5 6)
+ − 781 @result{} nil
+ − 782 @end example
+ − 783 @end defun
+ − 784
+ − 785 @defun /= number &rest more-numbers
+ − 786 This function returns @code{t} if no two arguments are numerically
+ − 787 equal, @code{nil} otherwise.
+ − 788
+ − 789 @example
+ − 790 (/= 5 6)
+ − 791 @result{} t
+ − 792 (/= 5 5 6)
+ − 793 @result{} nil
+ − 794 (/= 5 6 1)
+ − 795 @result{} t
+ − 796 @end example
+ − 797 @end defun
+ − 798
+ − 799 @defun < number &rest more-numbers
+ − 800 This function returns @code{t} if the sequence of its arguments is
+ − 801 monotonically increasing, @code{nil} otherwise.
+ − 802
+ − 803 @example
+ − 804 (< 5 6)
+ − 805 @result{} t
+ − 806 (< 5 6 6)
+ − 807 @result{} nil
+ − 808 (< 5 6 7)
+ − 809 @result{} t
+ − 810 @end example
+ − 811 @end defun
+ − 812
+ − 813 @defun <= number &rest more-numbers
+ − 814 This function returns @code{t} if the sequence of its arguments is
+ − 815 monotonically nondecreasing, @code{nil} otherwise.
+ − 816
+ − 817 @example
+ − 818 (<= 5 6)
+ − 819 @result{} t
+ − 820 (<= 5 6 6)
+ − 821 @result{} t
+ − 822 (<= 5 6 5)
+ − 823 @result{} nil
+ − 824 @end example
+ − 825 @end defun
+ − 826
+ − 827 @defun > number &rest more-numbers
+ − 828 This function returns @code{t} if the sequence of its arguments is
+ − 829 monotonically decreasing, @code{nil} otherwise.
+ − 830 @end defun
+ − 831
+ − 832 @defun >= number &rest more-numbers
+ − 833 This function returns @code{t} if the sequence of its arguments is
+ − 834 monotonically nonincreasing, @code{nil} otherwise.
+ − 835 @end defun
+ − 836
+ − 837 @defun max number &rest more-numbers
+ − 838 This function returns the largest of its arguments.
+ − 839
+ − 840 @example
+ − 841 (max 20)
+ − 842 @result{} 20
+ − 843 (max 1 2.5)
+ − 844 @result{} 2.5
+ − 845 (max 1 3 2.5)
+ − 846 @result{} 3
+ − 847 @end example
+ − 848 @end defun
+ − 849
+ − 850 @defun min number &rest more-numbers
+ − 851 This function returns the smallest of its arguments.
+ − 852
+ − 853 @example
+ − 854 (min -4 1)
+ − 855 @result{} -4
+ − 856 @end example
+ − 857 @end defun
+ − 858
+ − 859 @node Numeric Conversions
+ − 860 @section Numeric Conversions
+ − 861 @cindex rounding in conversions
+ − 862
+ − 863 To convert an integer to floating point, use the function @code{float}.
+ − 864
+ − 865 @defun float number
+ − 866 This returns @var{number} converted to floating point.
+ − 867 If @var{number} is already a floating point number, @code{float} returns
+ − 868 it unchanged.
+ − 869 @end defun
+ − 870
+ − 871 There are four functions to convert floating point numbers to integers;
+ − 872 they differ in how they round. These functions accept integer arguments
+ − 873 also, and return such arguments unchanged.
+ − 874
+ − 875 @defun truncate number
+ − 876 This returns @var{number}, converted to an integer by rounding towards
+ − 877 zero.
+ − 878 @end defun
+ − 879
+ − 880 @defun floor number &optional divisor
+ − 881 This returns @var{number}, converted to an integer by rounding downward
+ − 882 (towards negative infinity).
+ − 883
+ − 884 If @var{divisor} is specified, @var{number} is divided by @var{divisor}
+ − 885 before the floor is taken; this is the division operation that
+ − 886 corresponds to @code{mod}. An @code{arith-error} results if
+ − 887 @var{divisor} is 0.
+ − 888 @end defun
+ − 889
+ − 890 @defun ceiling number
+ − 891 This returns @var{number}, converted to an integer by rounding upward
+ − 892 (towards positive infinity).
+ − 893 @end defun
+ − 894
+ − 895 @defun round number
+ − 896 This returns @var{number}, converted to an integer by rounding towards the
+ − 897 nearest integer. Rounding a value equidistant between two integers
+ − 898 may choose the integer closer to zero, or it may prefer an even integer,
+ − 899 depending on your machine.
+ − 900 @end defun
+ − 901
+ − 902 @node Arithmetic Operations
+ − 903 @section Arithmetic Operations
+ − 904
+ − 905 XEmacs Lisp provides the traditional four arithmetic operations:
+ − 906 addition, subtraction, multiplication, and division. Remainder and modulus
+ − 907 functions supplement the division functions. The functions to
+ − 908 add or subtract 1 are provided because they are traditional in Lisp and
+ − 909 commonly used.
+ − 910
+ − 911 All of these functions except @code{%} return a floating point value
+ − 912 if any argument is floating.
+ − 913
+ − 914 It is important to note that in XEmacs Lisp, arithmetic functions
+ − 915 do not check for overflow. Thus @code{(1+ 134217727)} may evaluate to
+ − 916 @minus{}134217728, depending on your hardware.
+ − 917
444
+ − 918 @defun 1+ number
+ − 919 This function returns @var{number} plus one. @var{number} may be a
+ − 920 number, character or marker. Markers and characters are converted to
+ − 921 integers.
+ − 922
428
+ − 923 For example,
+ − 924
+ − 925 @example
+ − 926 (setq foo 4)
+ − 927 @result{} 4
+ − 928 (1+ foo)
+ − 929 @result{} 5
+ − 930 @end example
+ − 931
+ − 932 This function is not analogous to the C operator @code{++}---it does not
+ − 933 increment a variable. It just computes a sum. Thus, if we continue,
+ − 934
+ − 935 @example
+ − 936 foo
+ − 937 @result{} 4
+ − 938 @end example
+ − 939
+ − 940 If you want to increment the variable, you must use @code{setq},
+ − 941 like this:
+ − 942
+ − 943 @example
+ − 944 (setq foo (1+ foo))
+ − 945 @result{} 5
+ − 946 @end example
+ − 947
+ − 948 Now that the @code{cl} package is always available from lisp code, a
+ − 949 more convenient and natural way to increment a variable is
+ − 950 @w{@code{(incf foo)}}.
+ − 951 @end defun
+ − 952
444
+ − 953 @defun 1- number
+ − 954 This function returns @var{number} minus one. @var{number} may be a
+ − 955 number, character or marker. Markers and characters are converted to
+ − 956 integers.
428
+ − 957 @end defun
+ − 958
+ − 959 @defun abs number
+ − 960 This returns the absolute value of @var{number}.
+ − 961 @end defun
+ − 962
444
+ − 963 @defun + &rest numbers
428
+ − 964 This function adds its arguments together. When given no arguments,
+ − 965 @code{+} returns 0.
+ − 966
444
+ − 967 If any of the arguments are characters or markers, they are first
+ − 968 converted to integers.
+ − 969
428
+ − 970 @example
+ − 971 (+)
+ − 972 @result{} 0
+ − 973 (+ 1)
+ − 974 @result{} 1
+ − 975 (+ 1 2 3 4)
+ − 976 @result{} 10
+ − 977 @end example
+ − 978 @end defun
+ − 979
444
+ − 980 @defun - &optional number &rest other-numbers
428
+ − 981 The @code{-} function serves two purposes: negation and subtraction.
+ − 982 When @code{-} has a single argument, the value is the negative of the
+ − 983 argument. When there are multiple arguments, @code{-} subtracts each of
444
+ − 984 the @var{other-numbers} from @var{number}, cumulatively. If there are
+ − 985 no arguments, an error is signaled.
+ − 986
+ − 987 If any of the arguments are characters or markers, they are first
+ − 988 converted to integers.
428
+ − 989
+ − 990 @example
+ − 991 (- 10 1 2 3 4)
+ − 992 @result{} 0
+ − 993 (- 10)
+ − 994 @result{} -10
+ − 995 (-)
+ − 996 @result{} 0
+ − 997 @end example
+ − 998 @end defun
+ − 999
444
+ − 1000 @defun * &rest numbers
428
+ − 1001 This function multiplies its arguments together, and returns the
+ − 1002 product. When given no arguments, @code{*} returns 1.
+ − 1003
444
+ − 1004 If any of the arguments are characters or markers, they are first
+ − 1005 converted to integers.
+ − 1006
428
+ − 1007 @example
+ − 1008 (*)
+ − 1009 @result{} 1
+ − 1010 (* 1)
+ − 1011 @result{} 1
+ − 1012 (* 1 2 3 4)
+ − 1013 @result{} 24
+ − 1014 @end example
+ − 1015 @end defun
+ − 1016
444
+ − 1017 @defun / dividend &rest divisors
+ − 1018 The @code{/} function serves two purposes: inversion and division. When
+ − 1019 @code{/} has a single argument, the value is the inverse of the
+ − 1020 argument. When there are multiple arguments, @code{/} divides
+ − 1021 @var{dividend} by each of the @var{divisors}, cumulatively, returning
+ − 1022 the quotient. If there are no arguments, an error is signaled.
428
+ − 1023
444
+ − 1024 If none of the arguments are floats, then the result is an integer.
428
+ − 1025 This means the result has to be rounded. On most machines, the result
+ − 1026 is rounded towards zero after each division, but some machines may round
+ − 1027 differently with negative arguments. This is because the Lisp function
+ − 1028 @code{/} is implemented using the C division operator, which also
+ − 1029 permits machine-dependent rounding. As a practical matter, all known
+ − 1030 machines round in the standard fashion.
+ − 1031
444
+ − 1032 If any of the arguments are characters or markers, they are first
+ − 1033 converted to integers.
+ − 1034
428
+ − 1035 @cindex @code{arith-error} in division
+ − 1036 If you divide by 0, an @code{arith-error} error is signaled.
+ − 1037 (@xref{Errors}.)
+ − 1038
+ − 1039 @example
+ − 1040 @group
+ − 1041 (/ 6 2)
+ − 1042 @result{} 3
+ − 1043 @end group
+ − 1044 (/ 5 2)
+ − 1045 @result{} 2
+ − 1046 (/ 25 3 2)
+ − 1047 @result{} 4
444
+ − 1048 (/ 3.0)
+ − 1049 @result{} 0.3333333333333333
428
+ − 1050 (/ -17 6)
+ − 1051 @result{} -2
+ − 1052 @end example
+ − 1053
+ − 1054 The result of @code{(/ -17 6)} could in principle be -3 on some
+ − 1055 machines.
+ − 1056 @end defun
+ − 1057
+ − 1058 @defun % dividend divisor
+ − 1059 @cindex remainder
+ − 1060 This function returns the integer remainder after division of @var{dividend}
+ − 1061 by @var{divisor}. The arguments must be integers or markers.
+ − 1062
+ − 1063 For negative arguments, the remainder is in principle machine-dependent
+ − 1064 since the quotient is; but in practice, all known machines behave alike.
+ − 1065
+ − 1066 An @code{arith-error} results if @var{divisor} is 0.
+ − 1067
+ − 1068 @example
+ − 1069 (% 9 4)
+ − 1070 @result{} 1
+ − 1071 (% -9 4)
+ − 1072 @result{} -1
+ − 1073 (% 9 -4)
+ − 1074 @result{} 1
+ − 1075 (% -9 -4)
+ − 1076 @result{} -1
+ − 1077 @end example
+ − 1078
+ − 1079 For any two integers @var{dividend} and @var{divisor},
+ − 1080
+ − 1081 @example
+ − 1082 @group
+ − 1083 (+ (% @var{dividend} @var{divisor})
+ − 1084 (* (/ @var{dividend} @var{divisor}) @var{divisor}))
+ − 1085 @end group
+ − 1086 @end example
+ − 1087
+ − 1088 @noindent
+ − 1089 always equals @var{dividend}.
+ − 1090 @end defun
+ − 1091
+ − 1092 @defun mod dividend divisor
+ − 1093 @cindex modulus
+ − 1094 This function returns the value of @var{dividend} modulo @var{divisor};
+ − 1095 in other words, the remainder after division of @var{dividend}
+ − 1096 by @var{divisor}, but with the same sign as @var{divisor}.
+ − 1097 The arguments must be numbers or markers.
+ − 1098
+ − 1099 Unlike @code{%}, @code{mod} returns a well-defined result for negative
+ − 1100 arguments. It also permits floating point arguments; it rounds the
+ − 1101 quotient downward (towards minus infinity) to an integer, and uses that
+ − 1102 quotient to compute the remainder.
+ − 1103
+ − 1104 An @code{arith-error} results if @var{divisor} is 0.
+ − 1105
+ − 1106 @example
+ − 1107 @group
+ − 1108 (mod 9 4)
+ − 1109 @result{} 1
+ − 1110 @end group
+ − 1111 @group
+ − 1112 (mod -9 4)
+ − 1113 @result{} 3
+ − 1114 @end group
+ − 1115 @group
+ − 1116 (mod 9 -4)
+ − 1117 @result{} -3
+ − 1118 @end group
+ − 1119 @group
+ − 1120 (mod -9 -4)
+ − 1121 @result{} -1
+ − 1122 @end group
+ − 1123 @group
+ − 1124 (mod 5.5 2.5)
+ − 1125 @result{} .5
+ − 1126 @end group
+ − 1127 @end example
+ − 1128
+ − 1129 For any two numbers @var{dividend} and @var{divisor},
+ − 1130
+ − 1131 @example
+ − 1132 @group
+ − 1133 (+ (mod @var{dividend} @var{divisor})
+ − 1134 (* (floor @var{dividend} @var{divisor}) @var{divisor}))
+ − 1135 @end group
+ − 1136 @end example
+ − 1137
+ − 1138 @noindent
+ − 1139 always equals @var{dividend}, subject to rounding error if either
+ − 1140 argument is floating point. For @code{floor}, see @ref{Numeric
+ − 1141 Conversions}.
+ − 1142 @end defun
+ − 1143
+ − 1144 @node Rounding Operations
+ − 1145 @section Rounding Operations
+ − 1146 @cindex rounding without conversion
+ − 1147
+ − 1148 The functions @code{ffloor}, @code{fceiling}, @code{fround} and
+ − 1149 @code{ftruncate} take a floating point argument and return a floating
+ − 1150 point result whose value is a nearby integer. @code{ffloor} returns the
+ − 1151 nearest integer below; @code{fceiling}, the nearest integer above;
+ − 1152 @code{ftruncate}, the nearest integer in the direction towards zero;
+ − 1153 @code{fround}, the nearest integer.
+ − 1154
444
+ − 1155 @defun ffloor number
+ − 1156 This function rounds @var{number} to the next lower integral value, and
428
+ − 1157 returns that value as a floating point number.
+ − 1158 @end defun
+ − 1159
444
+ − 1160 @defun fceiling number
+ − 1161 This function rounds @var{number} to the next higher integral value, and
428
+ − 1162 returns that value as a floating point number.
+ − 1163 @end defun
+ − 1164
444
+ − 1165 @defun ftruncate number
+ − 1166 This function rounds @var{number} towards zero to an integral value, and
428
+ − 1167 returns that value as a floating point number.
+ − 1168 @end defun
+ − 1169
444
+ − 1170 @defun fround number
+ − 1171 This function rounds @var{number} to the nearest integral value,
428
+ − 1172 and returns that value as a floating point number.
+ − 1173 @end defun
+ − 1174
+ − 1175 @node Bitwise Operations
+ − 1176 @section Bitwise Operations on Integers
+ − 1177
+ − 1178 In a computer, an integer is represented as a binary number, a
+ − 1179 sequence of @dfn{bits} (digits which are either zero or one). A bitwise
+ − 1180 operation acts on the individual bits of such a sequence. For example,
+ − 1181 @dfn{shifting} moves the whole sequence left or right one or more places,
+ − 1182 reproducing the same pattern ``moved over''.
+ − 1183
+ − 1184 The bitwise operations in XEmacs Lisp apply only to integers.
+ − 1185
+ − 1186 @defun lsh integer1 count
+ − 1187 @cindex logical shift
+ − 1188 @code{lsh}, which is an abbreviation for @dfn{logical shift}, shifts the
+ − 1189 bits in @var{integer1} to the left @var{count} places, or to the right
+ − 1190 if @var{count} is negative, bringing zeros into the vacated bits. If
+ − 1191 @var{count} is negative, @code{lsh} shifts zeros into the leftmost
+ − 1192 (most-significant) bit, producing a positive result even if
+ − 1193 @var{integer1} is negative. Contrast this with @code{ash}, below.
+ − 1194
+ − 1195 Here are two examples of @code{lsh}, shifting a pattern of bits one
+ − 1196 place to the left. We show only the low-order eight bits of the binary
+ − 1197 pattern; the rest are all zero.
+ − 1198
+ − 1199 @example
+ − 1200 @group
+ − 1201 (lsh 5 1)
+ − 1202 @result{} 10
+ − 1203 ;; @r{Decimal 5 becomes decimal 10.}
+ − 1204 00000101 @result{} 00001010
+ − 1205
+ − 1206 (lsh 7 1)
+ − 1207 @result{} 14
+ − 1208 ;; @r{Decimal 7 becomes decimal 14.}
+ − 1209 00000111 @result{} 00001110
+ − 1210 @end group
+ − 1211 @end example
+ − 1212
+ − 1213 @noindent
+ − 1214 As the examples illustrate, shifting the pattern of bits one place to
+ − 1215 the left produces a number that is twice the value of the previous
+ − 1216 number.
+ − 1217
+ − 1218 Shifting a pattern of bits two places to the left produces results
+ − 1219 like this (with 8-bit binary numbers):
+ − 1220
+ − 1221 @example
+ − 1222 @group
+ − 1223 (lsh 3 2)
+ − 1224 @result{} 12
+ − 1225 ;; @r{Decimal 3 becomes decimal 12.}
444
+ − 1226 00000011 @result{} 00001100
428
+ − 1227 @end group
+ − 1228 @end example
+ − 1229
+ − 1230 On the other hand, shifting one place to the right looks like this:
+ − 1231
+ − 1232 @example
+ − 1233 @group
+ − 1234 (lsh 6 -1)
+ − 1235 @result{} 3
+ − 1236 ;; @r{Decimal 6 becomes decimal 3.}
444
+ − 1237 00000110 @result{} 00000011
428
+ − 1238 @end group
+ − 1239
+ − 1240 @group
+ − 1241 (lsh 5 -1)
+ − 1242 @result{} 2
+ − 1243 ;; @r{Decimal 5 becomes decimal 2.}
444
+ − 1244 00000101 @result{} 00000010
428
+ − 1245 @end group
+ − 1246 @end example
+ − 1247
+ − 1248 @noindent
+ − 1249 As the example illustrates, shifting one place to the right divides the
+ − 1250 value of a positive integer by two, rounding downward.
+ − 1251
+ − 1252 The function @code{lsh}, like all XEmacs Lisp arithmetic functions, does
+ − 1253 not check for overflow, so shifting left can discard significant bits
+ − 1254 and change the sign of the number. For example, left shifting
+ − 1255 134,217,727 produces @minus{}2 on a 28-bit machine:
+ − 1256
+ − 1257 @example
+ − 1258 (lsh 134217727 1) ; @r{left shift}
+ − 1259 @result{} -2
+ − 1260 @end example
+ − 1261
+ − 1262 In binary, in the 28-bit implementation, the argument looks like this:
+ − 1263
+ − 1264 @example
+ − 1265 @group
+ − 1266 ;; @r{Decimal 134,217,727}
444
+ − 1267 0111 1111 1111 1111 1111 1111 1111
428
+ − 1268 @end group
+ − 1269 @end example
+ − 1270
+ − 1271 @noindent
+ − 1272 which becomes the following when left shifted:
+ − 1273
+ − 1274 @example
+ − 1275 @group
+ − 1276 ;; @r{Decimal @minus{}2}
444
+ − 1277 1111 1111 1111 1111 1111 1111 1110
428
+ − 1278 @end group
+ − 1279 @end example
+ − 1280 @end defun
+ − 1281
+ − 1282 @defun ash integer1 count
+ − 1283 @cindex arithmetic shift
+ − 1284 @code{ash} (@dfn{arithmetic shift}) shifts the bits in @var{integer1}
+ − 1285 to the left @var{count} places, or to the right if @var{count}
+ − 1286 is negative.
+ − 1287
+ − 1288 @code{ash} gives the same results as @code{lsh} except when
+ − 1289 @var{integer1} and @var{count} are both negative. In that case,
+ − 1290 @code{ash} puts ones in the empty bit positions on the left, while
+ − 1291 @code{lsh} puts zeros in those bit positions.
+ − 1292
+ − 1293 Thus, with @code{ash}, shifting the pattern of bits one place to the right
+ − 1294 looks like this:
+ − 1295
+ − 1296 @example
+ − 1297 @group
444
+ − 1298 (ash -6 -1) @result{} -3
428
+ − 1299 ;; @r{Decimal @minus{}6 becomes decimal @minus{}3.}
+ − 1300 1111 1111 1111 1111 1111 1111 1010
444
+ − 1301 @result{}
428
+ − 1302 1111 1111 1111 1111 1111 1111 1101
+ − 1303 @end group
+ − 1304 @end example
+ − 1305
+ − 1306 In contrast, shifting the pattern of bits one place to the right with
+ − 1307 @code{lsh} looks like this:
+ − 1308
+ − 1309 @example
+ − 1310 @group
+ − 1311 (lsh -6 -1) @result{} 134217725
+ − 1312 ;; @r{Decimal @minus{}6 becomes decimal 134,217,725.}
+ − 1313 1111 1111 1111 1111 1111 1111 1010
444
+ − 1314 @result{}
428
+ − 1315 0111 1111 1111 1111 1111 1111 1101
+ − 1316 @end group
+ − 1317 @end example
+ − 1318
+ − 1319 Here are other examples:
+ − 1320
+ − 1321 @c !!! Check if lined up in smallbook format! XDVI shows problem
+ − 1322 @c with smallbook but not with regular book! --rjc 16mar92
+ − 1323 @smallexample
+ − 1324 @group
+ − 1325 ; @r{ 28-bit binary values}
+ − 1326
+ − 1327 (lsh 5 2) ; 5 = @r{0000 0000 0000 0000 0000 0000 0101}
+ − 1328 @result{} 20 ; = @r{0000 0000 0000 0000 0000 0001 0100}
+ − 1329 @end group
+ − 1330 @group
+ − 1331 (ash 5 2)
+ − 1332 @result{} 20
+ − 1333 (lsh -5 2) ; -5 = @r{1111 1111 1111 1111 1111 1111 1011}
+ − 1334 @result{} -20 ; = @r{1111 1111 1111 1111 1111 1110 1100}
+ − 1335 (ash -5 2)
+ − 1336 @result{} -20
+ − 1337 @end group
+ − 1338 @group
+ − 1339 (lsh 5 -2) ; 5 = @r{0000 0000 0000 0000 0000 0000 0101}
+ − 1340 @result{} 1 ; = @r{0000 0000 0000 0000 0000 0000 0001}
+ − 1341 @end group
+ − 1342 @group
+ − 1343 (ash 5 -2)
+ − 1344 @result{} 1
+ − 1345 @end group
+ − 1346 @group
+ − 1347 (lsh -5 -2) ; -5 = @r{1111 1111 1111 1111 1111 1111 1011}
+ − 1348 @result{} 4194302 ; = @r{0011 1111 1111 1111 1111 1111 1110}
+ − 1349 @end group
+ − 1350 @group
+ − 1351 (ash -5 -2) ; -5 = @r{1111 1111 1111 1111 1111 1111 1011}
+ − 1352 @result{} -2 ; = @r{1111 1111 1111 1111 1111 1111 1110}
+ − 1353 @end group
+ − 1354 @end smallexample
+ − 1355 @end defun
+ − 1356
+ − 1357 @defun logand &rest ints-or-markers
+ − 1358 @cindex logical and
+ − 1359 @cindex bitwise and
+ − 1360 This function returns the ``logical and'' of the arguments: the
+ − 1361 @var{n}th bit is set in the result if, and only if, the @var{n}th bit is
+ − 1362 set in all the arguments. (``Set'' means that the value of the bit is 1
+ − 1363 rather than 0.)
+ − 1364
+ − 1365 For example, using 4-bit binary numbers, the ``logical and'' of 13 and
+ − 1366 12 is 12: 1101 combined with 1100 produces 1100.
+ − 1367 In both the binary numbers, the leftmost two bits are set (i.e., they
+ − 1368 are 1's), so the leftmost two bits of the returned value are set.
+ − 1369 However, for the rightmost two bits, each is zero in at least one of
+ − 1370 the arguments, so the rightmost two bits of the returned value are 0's.
+ − 1371
+ − 1372 @noindent
+ − 1373 Therefore,
+ − 1374
+ − 1375 @example
+ − 1376 @group
+ − 1377 (logand 13 12)
+ − 1378 @result{} 12
+ − 1379 @end group
+ − 1380 @end example
+ − 1381
+ − 1382 If @code{logand} is not passed any argument, it returns a value of
+ − 1383 @minus{}1. This number is an identity element for @code{logand}
+ − 1384 because its binary representation consists entirely of ones. If
+ − 1385 @code{logand} is passed just one argument, it returns that argument.
+ − 1386
+ − 1387 @smallexample
+ − 1388 @group
+ − 1389 ; @r{ 28-bit binary values}
+ − 1390
+ − 1391 (logand 14 13) ; 14 = @r{0000 0000 0000 0000 0000 0000 1110}
+ − 1392 ; 13 = @r{0000 0000 0000 0000 0000 0000 1101}
+ − 1393 @result{} 12 ; 12 = @r{0000 0000 0000 0000 0000 0000 1100}
+ − 1394 @end group
+ − 1395
+ − 1396 @group
+ − 1397 (logand 14 13 4) ; 14 = @r{0000 0000 0000 0000 0000 0000 1110}
+ − 1398 ; 13 = @r{0000 0000 0000 0000 0000 0000 1101}
+ − 1399 ; 4 = @r{0000 0000 0000 0000 0000 0000 0100}
+ − 1400 @result{} 4 ; 4 = @r{0000 0000 0000 0000 0000 0000 0100}
+ − 1401 @end group
+ − 1402
+ − 1403 @group
+ − 1404 (logand)
+ − 1405 @result{} -1 ; -1 = @r{1111 1111 1111 1111 1111 1111 1111}
+ − 1406 @end group
+ − 1407 @end smallexample
+ − 1408 @end defun
+ − 1409
+ − 1410 @defun logior &rest ints-or-markers
+ − 1411 @cindex logical inclusive or
+ − 1412 @cindex bitwise or
+ − 1413 This function returns the ``inclusive or'' of its arguments: the @var{n}th bit
+ − 1414 is set in the result if, and only if, the @var{n}th bit is set in at least
+ − 1415 one of the arguments. If there are no arguments, the result is zero,
+ − 1416 which is an identity element for this operation. If @code{logior} is
+ − 1417 passed just one argument, it returns that argument.
+ − 1418
+ − 1419 @smallexample
+ − 1420 @group
+ − 1421 ; @r{ 28-bit binary values}
+ − 1422
+ − 1423 (logior 12 5) ; 12 = @r{0000 0000 0000 0000 0000 0000 1100}
+ − 1424 ; 5 = @r{0000 0000 0000 0000 0000 0000 0101}
+ − 1425 @result{} 13 ; 13 = @r{0000 0000 0000 0000 0000 0000 1101}
+ − 1426 @end group
+ − 1427
+ − 1428 @group
+ − 1429 (logior 12 5 7) ; 12 = @r{0000 0000 0000 0000 0000 0000 1100}
+ − 1430 ; 5 = @r{0000 0000 0000 0000 0000 0000 0101}
+ − 1431 ; 7 = @r{0000 0000 0000 0000 0000 0000 0111}
+ − 1432 @result{} 15 ; 15 = @r{0000 0000 0000 0000 0000 0000 1111}
+ − 1433 @end group
+ − 1434 @end smallexample
+ − 1435 @end defun
+ − 1436
+ − 1437 @defun logxor &rest ints-or-markers
+ − 1438 @cindex bitwise exclusive or
+ − 1439 @cindex logical exclusive or
+ − 1440 This function returns the ``exclusive or'' of its arguments: the
+ − 1441 @var{n}th bit is set in the result if, and only if, the @var{n}th bit is
+ − 1442 set in an odd number of the arguments. If there are no arguments, the
+ − 1443 result is 0, which is an identity element for this operation. If
+ − 1444 @code{logxor} is passed just one argument, it returns that argument.
+ − 1445
+ − 1446 @smallexample
+ − 1447 @group
+ − 1448 ; @r{ 28-bit binary values}
+ − 1449
+ − 1450 (logxor 12 5) ; 12 = @r{0000 0000 0000 0000 0000 0000 1100}
+ − 1451 ; 5 = @r{0000 0000 0000 0000 0000 0000 0101}
+ − 1452 @result{} 9 ; 9 = @r{0000 0000 0000 0000 0000 0000 1001}
+ − 1453 @end group
+ − 1454
+ − 1455 @group
+ − 1456 (logxor 12 5 7) ; 12 = @r{0000 0000 0000 0000 0000 0000 1100}
+ − 1457 ; 5 = @r{0000 0000 0000 0000 0000 0000 0101}
+ − 1458 ; 7 = @r{0000 0000 0000 0000 0000 0000 0111}
+ − 1459 @result{} 14 ; 14 = @r{0000 0000 0000 0000 0000 0000 1110}
+ − 1460 @end group
+ − 1461 @end smallexample
+ − 1462 @end defun
+ − 1463
+ − 1464 @defun lognot integer
+ − 1465 @cindex logical not
+ − 1466 @cindex bitwise not
+ − 1467 This function returns the logical complement of its argument: the @var{n}th
+ − 1468 bit is one in the result if, and only if, the @var{n}th bit is zero in
+ − 1469 @var{integer}, and vice-versa.
+ − 1470
+ − 1471 @example
444
+ − 1472 (lognot 5)
428
+ − 1473 @result{} -6
+ − 1474 ;; 5 = @r{0000 0000 0000 0000 0000 0000 0101}
+ − 1475 ;; @r{becomes}
+ − 1476 ;; -6 = @r{1111 1111 1111 1111 1111 1111 1010}
+ − 1477 @end example
+ − 1478 @end defun
+ − 1479
+ − 1480 @node Math Functions
+ − 1481 @section Standard Mathematical Functions
+ − 1482 @cindex transcendental functions
+ − 1483 @cindex mathematical functions
+ − 1484
+ − 1485 These mathematical functions are available if floating point is
+ − 1486 supported (which is the normal state of affairs). They allow integers
+ − 1487 as well as floating point numbers as arguments.
+ − 1488
444
+ − 1489 @defun sin number
+ − 1490 @defunx cos number
+ − 1491 @defunx tan number
428
+ − 1492 These are the ordinary trigonometric functions, with argument measured
+ − 1493 in radians.
+ − 1494 @end defun
+ − 1495
444
+ − 1496 @defun asin number
+ − 1497 The value of @code{(asin @var{number})} is a number between @minus{}pi/2
+ − 1498 and pi/2 (inclusive) whose sine is @var{number}; if, however, @var{number}
428
+ − 1499 is out of range (outside [-1, 1]), then the result is a NaN.
+ − 1500 @end defun
+ − 1501
444
+ − 1502 @defun acos number
+ − 1503 The value of @code{(acos @var{number})} is a number between 0 and pi
+ − 1504 (inclusive) whose cosine is @var{number}; if, however, @var{number}
428
+ − 1505 is out of range (outside [-1, 1]), then the result is a NaN.
+ − 1506 @end defun
+ − 1507
444
+ − 1508 @defun atan number &optional number2
+ − 1509 The value of @code{(atan @var{number})} is a number between @minus{}pi/2
+ − 1510 and pi/2 (exclusive) whose tangent is @var{number}.
+ − 1511
+ − 1512 If optional argument @var{number2} is supplied, the function returns
+ − 1513 @code{atan2(@var{number},@var{number2})}.
428
+ − 1514 @end defun
+ − 1515
444
+ − 1516 @defun sinh number
+ − 1517 @defunx cosh number
+ − 1518 @defunx tanh number
428
+ − 1519 These are the ordinary hyperbolic trigonometric functions.
+ − 1520 @end defun
+ − 1521
444
+ − 1522 @defun asinh number
+ − 1523 @defunx acosh number
+ − 1524 @defunx atanh number
428
+ − 1525 These are the inverse hyperbolic trigonometric functions.
+ − 1526 @end defun
+ − 1527
444
+ − 1528 @defun exp number
428
+ − 1529 This is the exponential function; it returns @i{e} to the power
444
+ − 1530 @var{number}. @i{e} is a fundamental mathematical constant also called the
428
+ − 1531 base of natural logarithms.
+ − 1532 @end defun
+ − 1533
444
+ − 1534 @defun log number &optional base
+ − 1535 This function returns the logarithm of @var{number}, with base @var{base}.
1738
+ − 1536 If you don't specify @var{base}, the base @code{e} is used. If @var{number}
428
+ − 1537 is negative, the result is a NaN.
+ − 1538 @end defun
+ − 1539
444
+ − 1540 @defun log10 number
+ − 1541 This function returns the logarithm of @var{number}, with base 10. If
+ − 1542 @var{number} is negative, the result is a NaN. @code{(log10 @var{x})}
428
+ − 1543 @equiv{} @code{(log @var{x} 10)}, at least approximately.
+ − 1544 @end defun
+ − 1545
+ − 1546 @defun expt x y
+ − 1547 This function returns @var{x} raised to power @var{y}. If both
+ − 1548 arguments are integers and @var{y} is positive, the result is an
+ − 1549 integer; in this case, it is truncated to fit the range of possible
+ − 1550 integer values.
+ − 1551 @end defun
+ − 1552
444
+ − 1553 @defun sqrt number
+ − 1554 This returns the square root of @var{number}. If @var{number} is negative,
428
+ − 1555 the value is a NaN.
+ − 1556 @end defun
+ − 1557
444
+ − 1558 @defun cube-root number
+ − 1559 This returns the cube root of @var{number}.
428
+ − 1560 @end defun
+ − 1561
+ − 1562 @node Random Numbers
+ − 1563 @section Random Numbers
+ − 1564 @cindex random numbers
+ − 1565
+ − 1566 A deterministic computer program cannot generate true random numbers.
+ − 1567 For most purposes, @dfn{pseudo-random numbers} suffice. A series of
+ − 1568 pseudo-random numbers is generated in a deterministic fashion. The
+ − 1569 numbers are not truly random, but they have certain properties that
+ − 1570 mimic a random series. For example, all possible values occur equally
+ − 1571 often in a pseudo-random series.
+ − 1572
+ − 1573 In XEmacs, pseudo-random numbers are generated from a ``seed'' number.
+ − 1574 Starting from any given seed, the @code{random} function always
+ − 1575 generates the same sequence of numbers. XEmacs always starts with the
+ − 1576 same seed value, so the sequence of values of @code{random} is actually
+ − 1577 the same in each XEmacs run! For example, in one operating system, the
+ − 1578 first call to @code{(random)} after you start XEmacs always returns
+ − 1579 -1457731, and the second one always returns -7692030. This
+ − 1580 repeatability is helpful for debugging.
+ − 1581
2090
+ − 1582 If you want reasonably unpredictable random numbers, execute
+ − 1583 @code{(random t)}. This chooses a new seed based on the current time of
+ − 1584 day and on XEmacs's process @sc{id} number. (This is not
+ − 1585 cryptographically strong, it's just hard for a @emph{human} to
+ − 1586 anticipate.)
428
+ − 1587
+ − 1588 @defun random &optional limit
+ − 1589 This function returns a pseudo-random integer. Repeated calls return a
+ − 1590 series of pseudo-random integers.
+ − 1591
+ − 1592 If @var{limit} is a positive integer, the value is chosen to be
+ − 1593 nonnegative and less than @var{limit}.
+ − 1594
+ − 1595 If @var{limit} is @code{t}, it means to choose a new seed based on the
+ − 1596 current time of day and on XEmacs's process @sc{id} number.
+ − 1597 @c "XEmacs'" is incorrect usage!
2090
+ − 1598 @end defun
428
+ − 1599
2090
+ − 1600 The range of random is implementation-dependent. On any machine, the
+ − 1601 result of @code{(random)} is an arbitrary fixnum, so on 32-bit
+ − 1602 architectures it is normally in the range -2^30 (inclusive) to +2^30
+ − 1603 (exclusive). With the optional integer argument @var{limit}, the result
+ − 1604 is in the range 0 (inclusive) to @var{limit} (exclusive). Note this is
+ − 1605 regardless of the presence of the bignum extension.
+ − 1606