Mercurial > hg > xemacs-beta
comparison man/lispref/numbers.texi @ 2090:e52c5a5c6a7d
[xemacs-hg @ 2004-05-21 01:21:08 by stephent]
improve bignum docs <871xled19j.fsf_-_@tleepslib.sk.tsukuba.ac.jp>
author | stephent |
---|---|
date | Fri, 21 May 2004 01:21:10 +0000 |
parents | a7b2d995287f |
children | 0221e454fe63 |
comparison
equal
deleted
inserted
replaced
2089:7457bc074bb3 | 2090:e52c5a5c6a7d |
---|---|
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc. | 3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc. |
4 @c See the file lispref.texi for copying conditions. | 4 @c See the file lispref.texi for copying conditions. |
5 @setfilename ../../info/numbers.info | 5 @setfilename ../../info/numbers.info |
6 @node Numbers, Strings and Characters, Lisp Data Types, Top | 6 @node Numbers, Strings and Characters, Lisp Data Types, Top |
7 @chapter Numbers | 7 @chapter Numbers |
8 @c #### Improve the indexing in this file!!!! | |
8 @cindex integers | 9 @cindex integers |
9 @cindex numbers | 10 @cindex numbers |
10 | 11 |
11 XEmacs supports two to five numeric data types. @dfn{Integers} and | 12 XEmacs supports two to five numeric data types. @dfn{Integers} and |
12 @dfn{floating point numbers} are always supported. As a build-time | 13 @dfn{floating point numbers} are always supported. As a build-time |
31 are notated in the same way as other integers (fixnums). XEmacs | 32 are notated in the same way as other integers (fixnums). XEmacs |
32 automatically converts results of computations from fixnum to bignum, | 33 automatically converts results of computations from fixnum to bignum, |
33 and back, depending on the storage required to represent the number. | 34 and back, depending on the storage required to represent the number. |
34 Thus use of bignums are entirely transparent to the user, except for a | 35 Thus use of bignums are entirely transparent to the user, except for a |
35 few special applications that expect overflows. Ratios are rational | 36 few special applications that expect overflows. Ratios are rational |
36 numbers with arbitrary precision. (In theory fixed-size rationals could | 37 numbers with arbitrary precision. They are notated in the |
37 be supported, but for almost all applications floats are a reasonable | 38 usual way with the solidus, for example 5/3 or @minus{}22/7. |
38 substitute for fixed-precision rationals.) They are notated in the | 39 |
39 usual way with the solidus, for example 5/3 or @minus{}22/7. Bigfloats | 40 Bigfloats are floating point numbers with arbitrary precision, which |
40 are floating point numbers with arbitrary precision. Unlike integers, | 41 may be specified by the user (and may be different for different |
41 which are always infinitely precise if they can be represented, floating | 42 bigfloats at the same time). Unlike integers, which are always |
42 point numbers are inherently imprecise. Therefore XEmacs automatically | 43 infinitely precise if they can be represented, floating point numbers |
43 converts @emph{from float to bigfloat} when floats and bigfloats are | 44 are inherently imprecise. This means that choice of precision can be a |
44 mixed in an expression, but a bigfloat will never be converted to a | 45 very delicate issue. XEmacs automatically converts @emph{from float to |
45 float unless the user explicitly coerces the value. Nor will the result | 46 bigfloat} when floats and bigfloats are mixed in an expression, but a |
46 of a float operation be converted to bigfloat, except for ``contagion'' | 47 bigfloat will never be converted to a float unless the user explicitly |
47 from another operand that is already a bigfloat. | 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}). | |
48 | 54 |
49 Note that the term ``integer'' is used throughout the XEmacs | 55 Note that the term ``integer'' is used throughout the XEmacs |
50 documentation and code to mean ``fixnum''. This is inconsistent with | 56 documentation and code to mean ``fixnum''. This is inconsistent with |
51 Common Lisp, and likely to cause confusion. Similarly, ``float'' is | 57 Common Lisp, and likely to cause confusion. Similarly, ``float'' is |
52 used to mean ``fixed precision floating point number'', and the Common | 58 used to mean ``fixed precision floating point number'', and the Common |
53 Lisp distinctions among @dfn{short-floats}, @dfn{long-floats}, and | 59 Lisp distinctions among @dfn{short-floats}, @dfn{long-floats}, |
54 bigfloats are not reflected in XEmacs terminology. | 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.) | |
55 | 63 |
56 @menu | 64 @menu |
57 * Integer Basics:: Representation and range of integers. | 65 * Integer Basics:: Representation and range of integers. |
58 * Rational Basics:: Representation and range of rational numbers. | 66 * Rational Basics:: Representation and range of rational numbers. |
59 * Float Basics:: Representation and range of floating point. | 67 * Float Basics:: Representation and range of floating point. |
71 @node Integer Basics | 79 @node Integer Basics |
72 @section Integer Basics | 80 @section Integer Basics |
73 | 81 |
74 The range of values for an integer depends on the machine. If a | 82 The range of values for an integer depends on the machine. If a |
75 multiple-precision arithmetic library is available on your platform, | 83 multiple-precision arithmetic library is available on your platform, |
76 support for bignums, that is, integers with arbitrary precision, maybe | 84 support for bignums, that is, integers with arbitrary precision, may be |
77 compiled in to your XEmacs. The rest of this section assumes that the | 85 compiled in to your XEmacs. The rest of this section assumes that the |
78 bignum extension is @emph{not} available. The bignum extension and the | 86 bignum extension is @emph{not} available. The bignum extension and the |
79 user-visible differences in normal integer arithmetic are discussed in a | 87 user-visible differences in normal integer arithmetic are discussed in a |
80 separate section @ref{The Bignum Extension}. | 88 separate section @ref{The Bignum Extension}. |
81 | 89 |
196 @section Rational Basics | 204 @section Rational Basics |
197 | 205 |
198 Ratios (built-in rational numbers) are available only when the bignum | 206 Ratios (built-in rational numbers) are available only when the bignum |
199 extension is built into your XEmacs. This facility is new and | 207 extension is built into your XEmacs. This facility is new and |
200 experimental. It is discussed in a separate section for convenience of | 208 experimental. It is discussed in a separate section for convenience of |
201 updating the documentation @ref{The Bignum Extension}. | 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 | |
202 | 225 |
203 | 226 |
204 @node Float Basics | 227 @node Float Basics |
205 @section Floating Point Basics | 228 @section Floating Point Basics |
206 | 229 |
207 XEmacs supports floating point numbers. The precise range of floating | 230 XEmacs supports floating point numbers. The precise range of floating |
208 point numbers is machine-specific; it is the same as the range of the C | 231 point numbers is machine-specific; it is the same as the range of the C |
209 data type @code{double} on the machine in question. If a | 232 data type @code{double} on the machine in question. If a |
210 multiple-precision arithmetic library is available on your platform, | 233 multiple-precision arithmetic library is available on your platform, |
211 support for bigfloats, that is, floating point numbers with arbitrary | 234 support for bigfloats, that is, floating point numbers with arbitrary |
212 precision, maybe compiled in to your XEmacs. The rest of this section | 235 precision, may be compiled in to your XEmacs. The rest of this section |
213 assumes that the bignum extension is @emph{not} available. The bigfloat | 236 assumes that the bignum extension is @emph{not} available. The bigfloat |
214 extension and the user-visible differences in normal float arithmetic | 237 extension and the user-visible differences in normal float arithmetic |
215 are discussed in a separate section @ref{The Bignum Extension}. | 238 are discussed in a separate section @ref{The Bignum Extension}. |
216 | 239 |
217 The printed representation for floating point numbers requires either | 240 The printed representation for floating point numbers requires either |
314 numerical implementations are quite delicate and sensitive to | 337 numerical implementations are quite delicate and sensitive to |
315 optimization. If the library was poorly optimized for your hardware, as | 338 optimization. If the library was poorly optimized for your hardware, as |
316 is often the case with Linux distributions for 80x86, you may achieve | 339 is often the case with Linux distributions for 80x86, you may achieve |
317 gains of @emph{several orders of magnitude} by rebuilding the MP | 340 gains of @emph{several orders of magnitude} by rebuilding the MP |
318 library. See @uref{http://www.swox.com/gmp/gmp-speed.html}.) The MP | 341 library. See @uref{http://www.swox.com/gmp/gmp-speed.html}.) The MP |
319 implementation provides arbitrary precision integers, but ratios were | 342 implementation provides arbitrary precision integers. Ratios and arbitrary |
320 implemented by the XEmacs implementer, Jerry James, who is not a | 343 precision floats are not available with MP. |
321 numerical analyst. Arbitrary precision floats are not available with | |
322 MP. | |
323 | 344 |
324 If your code needs to run correctly whether or not the feature is | 345 If your code needs to run correctly whether or not the feature is |
325 provided, you may test for the features @code{bignum}, @code{ratio}, and | 346 provided, you may test for the features @code{bignum}, @code{ratio}, and |
326 @code{bigfloat}. | 347 @code{bigfloat}. |
327 | 348 |
328 The XEmacs bignum facility implements the Common Lisp notion of | 349 The XEmacs bignum facility implements the Common Lisp notions of |
329 @dfn{contagion}, so that integers are always represented using the | 350 @dfn{canonicalization} and @dfn{contagion}. Canonicalization means that |
330 ``smallest'' representation that is exact, and integral ratios are | 351 in exact (integer and ratio) arithmetic, a result of an operation is |
331 converted to integers. Since floating point arithmetic is inherently | 352 always converted to the ``smallest'' type that can represent it |
332 imprecise, numbers are implicitly coerced to bigfloats only if other | 353 exactly. For exact numbers, the user only cares if efficiency is |
333 operands in the expression are bigfloat, and bigfloats are only coerced | 354 extremely important; Lisp does not try to determine an order of |
334 to other numerical types by explicit calls to the function @code{coerce}. | 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}. | |
335 | 364 |
336 Bignum support is incomplete. If you would like to help with bignum | 365 Bignum support is incomplete. If you would like to help with bignum |
337 support, especially on BSD MP, please subscribe to the | 366 support, especially on BSD MP, please subscribe to the |
338 @uref{http://www.xemacs.org/Lists/#xemacs-beta, XEmacs Beta mailing | 367 @uref{http://www.xemacs.org/Lists/#xemacs-beta, XEmacs Beta mailing |
339 list}, and book up on @file{number-gmp.h} and @file{number-mp.h}. Jerry | 368 list}, and book up on @file{number-gmp.h} and @file{number-mp.h}. Jerry |
343 | 372 |
344 @menu | 373 @menu |
345 * Bignum Basics:: Representation and range of integers. | 374 * Bignum Basics:: Representation and range of integers. |
346 * Ratio Basics:: Representation and range of rational numbers. | 375 * Ratio Basics:: Representation and range of rational numbers. |
347 * Bigfloat Basics:: Representation and range of floating point. | 376 * Bigfloat Basics:: Representation and range of floating point. |
348 * Contagion and Canonicalization:: Automatic coercion to other types. | 377 * Canonicalization and Contagion:: Automatic coercion to other types. |
349 * Compatibility Issues:: Changes in fixed-precision arithmetic. | 378 * Compatibility Issues:: Changes in fixed-precision arithmetic. |
350 @end menu | 379 @end menu |
351 | 380 |
352 | 381 |
353 @node Bignum Basics | 382 @node Bignum Basics |
378 | 407 |
379 Bigfloats, when available, have the same read syntax and print | 408 Bigfloats, when available, have the same read syntax and print |
380 representations as fixed-precision floats. | 409 representations as fixed-precision floats. |
381 | 410 |
382 | 411 |
383 @node Contagion and Canonicalization | 412 @node Canonicalization and Contagion |
384 @subsection 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. | |
385 | 447 |
386 @dfn{Contagion} is one way to address the requirement that an arithmetic | 448 @dfn{Contagion} is one way to address the requirement that an arithmetic |
387 operation should not fail because of differing types of the operands, or | 449 operation should not fail because of differing types of the operands. |
388 insufficient precision in the representation. With bignum support, we | 450 Contagion is the idea that less precise operands are converted to the |
389 can represent @code{(+ most-positive-fixnum most-positive-fixnum)} (or | 451 more precise type, and then the operation is performed. While changing |
390 @code{(* most-positive-fixnum most-positive-fixnum)}, for that matter) | 452 precision is a delicate issue, contagion is so useful that XEmacs |
391 @emph{exactly}. There should be no overflow or ``wrap-around,'' and the | 453 performs it automatically. |
392 computation should not be interrupted by an error. Contagion is the | |
393 idea that less precise operands are converted to the more precise type, | |
394 and then the operation is performed. This involves no loss of | |
395 information, and therefore it is safe to do it automatically. | |
396 | 454 |
397 In XEmacs, the following rules of contagion are used: | 455 In XEmacs, the following rules of contagion are used: |
398 | 456 |
399 @c #### this probably wants names for each rule | 457 @c #### this probably wants names for each rule |
400 @enumerate | 458 @enumerate |
401 @item | 459 @item |
402 If a fixnum operation would overflow or underflow, the operands are | 460 If an expression mixes an integral type with a ratio, then the usual |
403 promoted to bignums and the operation is performed. | 461 rules of rational arithmetic apply. (If the result of the expression |
404 | 462 happens to be an integer, it will be canonicalized to integer.) |
405 @item | 463 |
406 If a fixnum and a bignum are the operands, the fixnum is promoted to | |
407 bignum, and the operation is performed. | |
408 | |
409 @c #### seems plausible | |
410 @item | |
411 If a float operation would overflow or underflow (@emph{i.e.}, produce | |
412 an unrepresentably small but non-zero result), the operands are | |
413 converted to bigfloats and the operation performed. | |
414 | |
415 @c #### seems likely.... | |
416 @item | 464 @item |
417 If an expression mixes a rational type (fixnum, bignum, or ratio) with a | 465 If an expression mixes a rational type (fixnum, bignum, or ratio) with a |
418 float, the rational operand is converted to a float and the operation | 466 float, the rational operand is converted to a float and the operation |
419 performed if the result would fit in a float, otherwise both operands | 467 performed if the result would fit in a float, otherwise both operands |
420 are promoted to bigfloat, and the operation performed. | 468 are promoted to bigfloat, and the operation performed. |
421 | 469 |
422 @item | 470 @item |
423 If an expression mixes any other type with a bigfloat, the other operand | 471 If an expression mixes any other type with a bigfloat, the other operand |
424 is converted to bigfloat and the operation performed. | 472 is converted to bigfloat and the operation performed. |
425 @end enumerate | 473 |
426 | |
427 Note that there is @emph{no} contagion with ratios. Integer-to-integer | |
428 arithmetic with truncation or rounding is useful, and familiar. | |
429 Therefore instead of converting the result of @code{/} to ratio if it is | |
430 non-integral, the traditional definition is maintained, and a new | |
431 function @code{div} is provided to give a ratio result if a division | |
432 does not come out evenly. | |
433 | |
434 On the other hand, the representation of arbitrary precision numbers is | |
435 inefficient in both space and time. The principle of | |
436 @dfn{canonicalization} addresses this issue. The idea of | |
437 canonicalization is that when no information is lost, the representation | |
438 should be demoted to the more efficient (smaller) representation. Note | |
439 that the inefficiency is likely to be greater than you might think. | |
440 Experience with numerical analysis shows that in very precise | |
441 calculations, precision tends to increase. Thus it is typically wasted | |
442 effort to attempt to convert to smaller representations, as the number | |
443 is often reused and requires a larger representation. However, XEmacs | |
444 Lisp presumes that calculations using bignums are the exception, so it | |
445 applies canonicalization. The rules are | |
446 | |
447 @enumerate | |
448 @item | 474 @item |
449 If a ratio is integral, demote it to a bignum. (In XEmacs Lisp all | 475 If bigfloats of different precision are mixed, all are converted to the |
450 ratios are arbitrary precision numbers.) | 476 @emph{highest} precision, and the operation performed. |
451 | |
452 @item | |
453 If a bignum is small enough to fit in a fixnum, demote it to fixnum. | |
454 @end enumerate | 477 @end enumerate |
455 | 478 |
456 Note that there are no rules to canonicalize floats or bigfloats. This | 479 Note that there are no rules to canonicalize floats or bigfloats. This |
457 might seem surprising, but in both cases information will be lost. Any | 480 might seem surprising, but in both cases information will be lost. Any |
458 floating point representation is implicitly approximate. A conversion | 481 floating point representation is implicitly approximate. A conversion |
469 according to the above rules using | 492 according to the above rules using |
470 | 493 |
471 @defun canonicalize-number number | 494 @defun canonicalize-number number |
472 Return the canonical form of @var{number}. | 495 Return the canonical form of @var{number}. |
473 @end defun | 496 @end defun |
497 | |
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. | |
474 | 501 |
475 | 502 |
476 @node Compatibility Issues | 503 @node Compatibility Issues |
477 @subsection Compatibility Issues | 504 @subsection Compatibility Issues |
478 | 505 |
540 | 567 |
541 @node Predicates on Numbers | 568 @node Predicates on Numbers |
542 @section Type Predicates for Numbers | 569 @section Type Predicates for Numbers |
543 | 570 |
544 The functions in this section test whether the argument is a number or | 571 The functions in this section test whether the argument is a number or |
545 whether it is a certain sort of number. The functions @code{integerp} | 572 whether it is a certain sort of number. The functions which test for |
546 and @code{floatp} can take any type of Lisp object as argument (the | 573 type can take any type of Lisp object as argument (the more general |
547 predicates would not be of much use otherwise); but the @code{zerop} | 574 predicates would not be of much use otherwise). However, the |
548 predicate requires a number as its argument. See also | 575 @code{zerop} predicate requires a number as its argument, and the |
549 @code{integer-or-marker-p}, @code{integer-char-or-marker-p}, | 576 @code{evenp}, and @code{oddp} predicates require integers as their |
550 @code{number-or-marker-p} and @code{number-char-or-marker-p}, in | 577 arguments. See also @code{integer-or-marker-p}, |
551 @ref{Predicates on Markers}. | 578 @code{integer-char-or-marker-p}, @code{number-or-marker-p} and |
552 | 579 @code{number-char-or-marker-p}, in @ref{Predicates on Markers}. |
553 @defun floatp object | 580 |
554 This predicate tests whether its argument is a floating point | 581 @defun numberp object |
555 number and returns @code{t} if so, @code{nil} otherwise. | 582 This predicate tests whether its argument is a number (either integer or |
556 | 583 floating point), and returns @code{t} if so, @code{nil} otherwise. |
557 @code{floatp} does not exist in Emacs versions 18 and earlier. | 584 @end defun |
585 | |
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)}. | |
558 @end defun | 598 @end defun |
559 | 599 |
560 @defun integerp object | 600 @defun integerp object |
561 This predicate tests whether its argument is an integer, and returns | 601 This predicate tests whether its argument is an integer, and returns |
562 @code{t} if so, @code{nil} otherwise. | 602 @code{t} if so, @code{nil} otherwise. |
563 @end defun | 603 @end defun |
564 | 604 |
565 @defun numberp object | 605 @defun oddp integer |
566 This predicate tests whether its argument is a number (either integer or | 606 @cindex integers |
567 floating point), and returns @code{t} if so, @code{nil} otherwise. | 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. | |
568 @end defun | 617 @end defun |
569 | 618 |
570 @defun natnump object | 619 @defun natnump object |
571 @cindex natural numbers | 620 @cindex natural numbers |
572 The @code{natnump} predicate (whose name comes from the phrase | 621 The @code{natnump} predicate (whose name comes from the phrase |
573 ``natural-number-p'') tests to see whether its argument is a nonnegative | 622 ``natural-number-p'') tests to see whether its argument is a nonnegative |
574 integer, and returns @code{t} if so, @code{nil} otherwise. 0 is | 623 integer, and returns @code{t} if so, @code{nil} otherwise. 0 is |
575 considered non-negative. | 624 considered non-negative. |
576 @end defun | 625 @end defun |
577 | 626 |
578 @defun zerop number | 627 @defun fixnump object |
579 This predicate tests whether its argument is zero, and returns @code{t} | 628 @cindex integers |
580 if so, @code{nil} otherwise. The argument must be a number. | 629 The @code{} predicate tests to see whether its argument is an integer |
581 | 630 represented as a fixnum, and returns @code{t} if so, @code{nil} |
582 These two forms are equivalent: @code{(zerop x)} @equiv{} @code{(= x 0)}. | 631 otherwise. |
583 @end defun | 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 | |
646 | |
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. | |
659 @end defun | |
660 | |
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 | |
673 integer represented as a fixnum, and returns @code{t} if so, @code{nil} | |
674 otherwise. | |
675 @end defun | |
676 | |
584 | 677 |
585 @node Comparison of Numbers | 678 @node Comparison of Numbers |
586 @section Comparison of Numbers | 679 @section Comparison of Numbers |
587 @cindex number equality | 680 @cindex number equality |
588 | 681 |
589 To test numbers for numerical equality, you should normally use | 682 To test numbers for numerical equality, you should normally use |
590 @code{=}, not @code{eq}. There can be many distinct floating point | 683 @code{=}, not @code{eq}. There can be many distinct floating point, |
591 number objects with the same numeric value. If you use @code{eq} to | 684 bignum, and ratio number objects with the same numeric value. If you |
592 compare them, then you test whether two values are the same | 685 use @code{eq} to compare them, then you test whether two values are the |
593 @emph{object}. By contrast, @code{=} compares only the numeric values | 686 same @emph{object}. By contrast, @code{=} compares only the numeric |
594 of the objects. | 687 values of the objects. |
595 | 688 |
596 In versions before 21.5.18, each integer value had a unique Lisp | 689 In versions before 21.5.18, each integer value had a unique Lisp |
597 object in XEmacs Lisp. Therefore, @code{eq} was equivalent to @code{=} | 690 object in XEmacs Lisp. Therefore, @code{eq} was equivalent to @code{=} |
598 where integers are concerned. Even with the introduction of bignums, it | 691 where integers are concerned. Even with the introduction of bignums, it |
599 is sometimes convenient to use @code{eq} for comparing an unknown value | 692 is sometimes convenient to use @code{eq} for comparing an unknown value |
602 contrast, @code{=} signals an error if the arguments are not numbers or | 695 contrast, @code{=} signals an error if the arguments are not numbers or |
603 markers. However, it is a good idea to use @code{=} if you can, even | 696 markers. However, it is a good idea to use @code{=} if you can, even |
604 for comparing exact values, because two bignums or ratios with the same | 697 for comparing exact values, because two bignums or ratios with the same |
605 value will often not be the same object. | 698 value will often not be the same object. |
606 | 699 |
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 | |
709 comparing. | |
710 | |
711 @example | |
712 (eql 2 (string-match "ere" "there")) | |
713 @result{} t | |
714 | |
715 (eql 2 (string-match "ere" "three")) | |
716 @result{} nil | |
717 | |
718 (eql 2 2.0) | |
719 @result{} nil | |
720 | |
721 (= 2 (string-match "ere" "there")) | |
722 @result{} t | |
723 | |
724 (= 2 (string-match "ere" "three")) | |
725 @error{} Wrong type argument: number-char-or-marker-p, nil | |
726 | |
727 (= 2 2.0) | |
728 @result{} t | |
729 @end example | |
730 | |
731 | |
732 | |
607 There is another wrinkle: because floating point arithmetic is not | 733 There is another wrinkle: because floating point arithmetic is not |
608 exact, it is often a bad idea to check for equality of two floating | 734 exact, it is often a bad idea to check for equality of two floating |
609 point values. Usually it is better to test for approximate equality. | 735 point values. Usually it is better to test for approximate equality. |
610 Here's a function to do this: | 736 Here's a function to do this: |
611 | 737 |
1442 the same in each XEmacs run! For example, in one operating system, the | 1568 the same in each XEmacs run! For example, in one operating system, the |
1443 first call to @code{(random)} after you start XEmacs always returns | 1569 first call to @code{(random)} after you start XEmacs always returns |
1444 -1457731, and the second one always returns -7692030. This | 1570 -1457731, and the second one always returns -7692030. This |
1445 repeatability is helpful for debugging. | 1571 repeatability is helpful for debugging. |
1446 | 1572 |
1447 If you want truly unpredictable random numbers, execute @code{(random | 1573 If you want reasonably unpredictable random numbers, execute |
1448 t)}. This chooses a new seed based on the current time of day and on | 1574 @code{(random t)}. This chooses a new seed based on the current time of |
1449 XEmacs's process @sc{id} number. | 1575 day and on XEmacs's process @sc{id} number. (This is not |
1576 cryptographically strong, it's just hard for a @emph{human} to | |
1577 anticipate.) | |
1450 | 1578 |
1451 @defun random &optional limit | 1579 @defun random &optional limit |
1452 This function returns a pseudo-random integer. Repeated calls return a | 1580 This function returns a pseudo-random integer. Repeated calls return a |
1453 series of pseudo-random integers. | 1581 series of pseudo-random integers. |
1454 | 1582 |
1456 nonnegative and less than @var{limit}. | 1584 nonnegative and less than @var{limit}. |
1457 | 1585 |
1458 If @var{limit} is @code{t}, it means to choose a new seed based on the | 1586 If @var{limit} is @code{t}, it means to choose a new seed based on the |
1459 current time of day and on XEmacs's process @sc{id} number. | 1587 current time of day and on XEmacs's process @sc{id} number. |
1460 @c "XEmacs'" is incorrect usage! | 1588 @c "XEmacs'" is incorrect usage! |
1461 | 1589 @end defun |
1462 On some machines, any integer representable in Lisp may be the result | 1590 |
1463 of @code{random}. On other machines, the result can never be larger | 1591 The range of random is implementation-dependent. On any machine, the |
1464 than a certain maximum or less than a certain (negative) minimum. | 1592 result of @code{(random)} is an arbitrary fixnum, so on 32-bit |
1465 @end defun | 1593 architectures it is normally in the range -2^30 (inclusive) to +2^30 |
1594 (exclusive). With the optional integer argument @var{limit}, the result | |
1595 is in the range 0 (inclusive) to @var{limit} (exclusive). Note this is | |
1596 regardless of the presence of the bignum extension. | |
1597 |