annotate man/lispref/numbers.texi @ 5917:ffb5abc8dc4e

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