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